| 457 | } |
| 458 | |
| 459 | void buffer::ptr::release() |
| 460 | { |
| 461 | // BE CAREFUL: this is called also for hypercombined ptr_node. After |
| 462 | // freeing underlying raw, `*this` can become inaccessible as well! |
| 463 | // |
| 464 | // cache the pointer to avoid unncecessary reloads and repeated |
| 465 | // checks. |
| 466 | if (auto* const cached_raw = std::exchange(_raw, nullptr); |
| 467 | cached_raw) { |
| 468 | bdout << "ptr " << this << " release " << cached_raw << bendl; |
| 469 | // optimize the common case where a particular `buffer::raw` has |
| 470 | // only a single reference. Altogether with initializing `nref` of |
| 471 | // freshly fabricated one with `1` through the std::atomic's ctor |
| 472 | // (which doesn't impose a memory barrier on the strongly-ordered |
| 473 | // x86), this allows to avoid all atomical operations in such case. |
| 474 | const bool last_one = \ |
| 475 | (1 == cached_raw->nref.load(std::memory_order_acquire)); |
| 476 | if (likely(last_one) || --cached_raw->nref == 0) { |
| 477 | bdout << "deleting raw " << static_cast<void*>(cached_raw) |
| 478 | << " len " << cached_raw->get_len() << bendl; |
| 479 | ANNOTATE_HAPPENS_AFTER(&cached_raw->nref); |
| 480 | ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&cached_raw->nref); |
| 481 | delete cached_raw; // dealloc old (if any) |
| 482 | } else { |
| 483 | ANNOTATE_HAPPENS_BEFORE(&cached_raw->nref); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | int buffer::ptr::get_mempool() const { |
| 489 | if (_raw) { |
no test coverage detected