| 54 | } |
| 55 | |
| 56 | void* ScopedAllocator::AllocateRaw(int32 field_index, size_t num_bytes) { |
| 57 | VLOG(1) << "ScopedAllocator index " << id_ << " AllocateRaw " |
| 58 | << "field " << field_index << " num_bytes " << num_bytes; |
| 59 | mutex_lock l(mu_); |
| 60 | if (expected_call_count_ <= 0) { |
| 61 | LOG(ERROR) << "Scoped allocator " << name_ |
| 62 | << " could not satisfy request for " << num_bytes |
| 63 | << " bytes, expected uses exhausted. "; |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | int32_t num_fields = static_cast<int32>(fields_.size()); |
| 68 | if (field_index >= num_fields) { |
| 69 | LOG(ERROR) << "ScopedAllocator " << name_ |
| 70 | << " received unexpected field number " << field_index; |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | const Field& f = fields_[field_index]; |
| 75 | if (num_bytes != f.bytes) { |
| 76 | LOG(ERROR) << "ScopedAllocator " << name_ << " got request for " |
| 77 | << num_bytes << " bytes from field " << field_index |
| 78 | << " which has precalculated size " << f.bytes << " and offset " |
| 79 | << f.offset; |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
| 83 | void* ptr = static_cast<void*>((tbuf_->template base<char>() + f.offset)); |
| 84 | |
| 85 | ++live_alloc_count_; |
| 86 | --expected_call_count_; |
| 87 | if (0 == expected_call_count_) { |
| 88 | for (auto& f : fields_) { |
| 89 | container_->Drop(f.scope_id, this); |
| 90 | } |
| 91 | container_->Drop(id_, this); |
| 92 | container_->Unref(); |
| 93 | container_ = nullptr; |
| 94 | } |
| 95 | VLOG(1) << "AllocateRaw returning " << ptr; |
| 96 | return ptr; |
| 97 | } |
| 98 | |
| 99 | void ScopedAllocator::DeallocateRaw(void* p) { |
| 100 | CHECK(VerifyPointer(p)); |