| 34 | BufferedAllocator::~BufferedAllocator() { FreeCache(-1UL); } |
| 35 | |
| 36 | void BufferedAllocator::FreeCache(size_t size) { |
| 37 | platform::LockGuardPtr<std::mutex> guard(mtx_); |
| 38 | if (UNLIKELY(size == 0)) return; |
| 39 | size_t cur = 0; |
| 40 | while (!allocations_.empty()) { // free the largest |
| 41 | auto it = --allocations_.end(); |
| 42 | cur += it->second->size(); |
| 43 | underlying_allocator_->Free(it->second.release()); |
| 44 | allocations_.erase(it); |
| 45 | if (cur >= size) return; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | bool BufferedAllocator::IsAllocThreadSafe() const { return mtx_ != nullptr; } |
| 50 | |