| Index: cc/base/contiguous_container.cc
|
| diff --git a/cc/base/contiguous_container.cc b/cc/base/contiguous_container.cc
|
| index d6280b43290b8d4d9547f56a1a62b070caf63e66..b81db9a04e904a3a012a0ce357f069b7cbd5bf20 100644
|
| --- a/cc/base/contiguous_container.cc
|
| +++ b/cc/base/contiguous_container.cc
|
| @@ -16,18 +16,22 @@ static const unsigned kDefaultInitialBufferSize = 32;
|
|
|
| class ContiguousContainerBase::Buffer {
|
| public:
|
| - explicit Buffer(size_t buffer_size)
|
| - : data_(new char[buffer_size]), end_(begin()), capacity_(buffer_size) {}
|
| + explicit Buffer(size_t buffer_size) : end_(nullptr), capacity_(buffer_size) {}
|
|
|
| ~Buffer() {}
|
|
|
| size_t Capacity() const { return capacity_; }
|
| size_t UsedCapacity() const { return end_ - begin(); }
|
| size_t UnusedCapacity() const { return Capacity() - UsedCapacity(); }
|
| + size_t MemoryUsage() const { return begin() ? capacity_ : 0; }
|
| bool empty() const { return UsedCapacity() == 0; }
|
|
|
| void* Allocate(size_t object_size) {
|
| DCHECK_GE(UnusedCapacity(), object_size);
|
| + if (!data_) {
|
| + data_.reset(new char[capacity_]);
|
| + end_ = begin();
|
| + }
|
| void* result = end_;
|
| end_ += object_size;
|
| return result;
|
| @@ -40,8 +44,8 @@ class ContiguousContainerBase::Buffer {
|
| }
|
|
|
| private:
|
| - char* begin() { return &data_[0]; }
|
| - const char* begin() const { return &data_[0]; }
|
| + char* begin() { return data_.get(); }
|
| + const char* begin() const { return data_.get(); }
|
|
|
| // begin() <= end_ <= begin() + capacity_
|
| std::unique_ptr<char[]> data_;
|
| @@ -76,7 +80,10 @@ size_t ContiguousContainerBase::UsedCapacityInBytes() const {
|
| }
|
|
|
| size_t ContiguousContainerBase::MemoryUsageInBytes() const {
|
| - return sizeof(*this) + GetCapacityInBytes() +
|
| + size_t memory_usage = 0;
|
| + for (const auto& buffer : buffers_)
|
| + memory_usage += buffer->MemoryUsage();
|
| + return sizeof(*this) + memory_usage +
|
| elements_.capacity() * sizeof(elements_[0]);
|
| }
|
|
|
|
|