| 65 | int64_t num_allocations() const { return num_allocs_.load(std::memory_order_acquire); } |
| 66 | |
| 67 | inline void DidAllocateBytes(int64_t size) { |
| 68 | // Issue the load before everything else. max_memory_ is monotonically increasing, |
| 69 | // so we can use a relaxed load before the read-modify-write. |
| 70 | auto max_memory = max_memory_.load(std::memory_order_relaxed); |
| 71 | const auto old_bytes_allocated = |
| 72 | bytes_allocated_.fetch_add(size, std::memory_order_acq_rel); |
| 73 | // Issue store operations on values that we don't depend on to proceed |
| 74 | // with execution. When done, max_memory and old_bytes_allocated have |
| 75 | // a higher chance of being available on CPU registers. This also has the |
| 76 | // nice side-effect of putting 3 atomic stores close to each other in the |
| 77 | // instruction stream. |
| 78 | total_allocated_bytes_.fetch_add(size, std::memory_order_acq_rel); |
| 79 | num_allocs_.fetch_add(1, std::memory_order_acq_rel); |
| 80 | |
| 81 | // If other threads are updating max_memory_ concurrently we leave the loop without |
| 82 | // updating knowing that it already reached a value even higher than ours. |
| 83 | const auto allocated = old_bytes_allocated + size; |
| 84 | while (max_memory < allocated && !max_memory_.compare_exchange_weak( |
| 85 | /*expected=*/max_memory, /*desired=*/allocated, |
| 86 | std::memory_order_acq_rel)) { |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | inline void DidReallocateBytes(int64_t old_size, int64_t new_size) { |
| 91 | if (new_size > old_size) { |