live += delta, never wrapping below zero: a mismatched class on free (the * one way a caller can get this wrong) must not turn a small drift into a * colossal bogus number that looks like a leak. Returns the new value. */
| 112 | * one way a caller can get this wrong) must not turn a small drift into a |
| 113 | * colossal bogus number that looks like a leak. Returns the new value. */ |
| 114 | static size_t apply_signed(atomic_size_t *counter, long delta) { |
| 115 | if (delta >= 0) { |
| 116 | return atomic_fetch_add_explicit(counter, (size_t)delta, memory_order_relaxed) + |
| 117 | (size_t)delta; |
| 118 | } |
| 119 | size_t sub = (size_t)(-delta); |
| 120 | size_t seen = atomic_load_explicit(counter, memory_order_relaxed); |
| 121 | while (true) { |
| 122 | size_t want = sub > seen ? 0 : seen - sub; |
| 123 | if (atomic_compare_exchange_weak_explicit(counter, &seen, want, memory_order_relaxed, |
| 124 | memory_order_relaxed)) { |
| 125 | return want; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static void class_flush_one(cbm_mem_class_t cls) { |
| 131 | mem_delta_t *d = &tl_delta[cls]; |