| 120 | } |
| 121 | |
| 122 | void TrackingAllocator::DeallocateRaw(void* ptr) { |
| 123 | // freeing a null ptr is a no-op |
| 124 | if (nullptr == ptr) { |
| 125 | return; |
| 126 | } |
| 127 | bool should_delete; |
| 128 | // fetch the following outside the lock in case the call to |
| 129 | // AllocatedSize is slow |
| 130 | bool tracks_allocation_sizes = allocator_->TracksAllocationSizes(); |
| 131 | size_t allocated_bytes = 0; |
| 132 | if (tracks_allocation_sizes) { |
| 133 | allocated_bytes = allocator_->AllocatedSize(ptr); |
| 134 | } else if (track_sizes_locally_) { |
| 135 | mutex_lock lock(mu_); |
| 136 | auto itr = in_use_.find(ptr); |
| 137 | if (itr != in_use_.end()) { |
| 138 | tracks_allocation_sizes = true; |
| 139 | allocated_bytes = (*itr).second.allocated_size; |
| 140 | in_use_.erase(itr); |
| 141 | } |
| 142 | } |
| 143 | Allocator* allocator = allocator_; |
| 144 | { |
| 145 | mutex_lock lock(mu_); |
| 146 | if (tracks_allocation_sizes) { |
| 147 | CHECK_GE(allocated_, allocated_bytes); |
| 148 | allocated_ -= allocated_bytes; |
| 149 | allocations_.emplace_back(-allocated_bytes, Env::Default()->NowMicros()); |
| 150 | } |
| 151 | should_delete = UnRef(); |
| 152 | } |
| 153 | allocator->DeallocateRaw(ptr); |
| 154 | if (should_delete) { |
| 155 | delete this; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void TrackingAllocator::DeallocateRawAsync(void* ptr) { |
| 160 | // freeing a null ptr is a no-op |