| 73 | } |
| 74 | |
| 75 | size_t TrackingAllocator::BatchAllocateRaw(size_t num, size_t alignment, |
| 76 | size_t num_bytes, void** ret) { |
| 77 | size_t allocated_num = allocator_->BatchAllocateRaw(num, alignment, |
| 78 | num_bytes, ret); |
| 79 | |
| 80 | if (allocator_->TracksAllocationSizes()) { |
| 81 | for (size_t i = 0; i < allocated_num; ++i) { |
| 82 | auto ptr = ret[i]; |
| 83 | size_t allocated_bytes = allocator_->AllocatedSize(ptr); |
| 84 | { |
| 85 | mutex_lock lock(mu_); |
| 86 | allocated_ += allocated_bytes; |
| 87 | high_watermark_ = std::max(high_watermark_, allocated_); |
| 88 | total_bytes_ += allocated_bytes; |
| 89 | allocations_.emplace_back(allocated_bytes, Env::Default()->NowMicros()); |
| 90 | ++ref_; |
| 91 | } |
| 92 | } |
| 93 | } else if (track_sizes_locally_) { |
| 94 | // Call the underlying allocator to try to get the allocated size |
| 95 | // whenever possible, even when it might be slow. If this fails, |
| 96 | // use the requested size as an approximation. |
| 97 | for (size_t i = 0; i < allocated_num; ++i) { |
| 98 | auto ptr = ret[i]; |
| 99 | size_t allocated_bytes = allocator_->AllocatedSizeSlow(ptr); |
| 100 | allocated_bytes = std::max(num_bytes, allocated_bytes); |
| 101 | mutex_lock lock(mu_); |
| 102 | next_allocation_id_ += 1; |
| 103 | Chunk chunk = {num_bytes, allocated_bytes, next_allocation_id_}; |
| 104 | in_use_.emplace(std::make_pair(ptr, chunk)); |
| 105 | allocated_ += allocated_bytes; |
| 106 | high_watermark_ = std::max(high_watermark_, allocated_); |
| 107 | total_bytes_ += allocated_bytes; |
| 108 | allocations_.emplace_back(allocated_bytes, Env::Default()->NowMicros()); |
| 109 | ++ref_; |
| 110 | } |
| 111 | } else { |
| 112 | mutex_lock lock(mu_); |
| 113 | total_bytes_ += num_bytes * allocated_num; |
| 114 | for (size_t i = 0; i < allocated_num; ++i) { |
| 115 | allocations_.emplace_back(num_bytes, Env::Default()->NowMicros()); |
| 116 | ++ref_; |
| 117 | } |
| 118 | } |
| 119 | return allocated_num; |
| 120 | } |
| 121 | |
| 122 | void TrackingAllocator::DeallocateRaw(void* ptr) { |
| 123 | // freeing a null ptr is a no-op |
nothing calls this directly
no test coverage detected