Chromium Code Reviews| Index: mojo/public/bindings/lib/buffer.cc |
| diff --git a/mojo/public/bindings/lib/buffer.cc b/mojo/public/bindings/lib/buffer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c153522949305c36a12bb834e96d6039464870ba |
| --- /dev/null |
| +++ b/mojo/public/bindings/lib/buffer.cc |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/public/bindings/lib/buffer.h" |
| + |
| +#include <stdlib.h> |
| +#include <string.h> |
| + |
| +#include <algorithm> |
| + |
| +namespace mojo { |
| + |
| +namespace { |
|
viettrungluu
2013/10/09 03:44:06
(Note that the anonymous namespace is redundant, s
|
| +const uint32_t kAlignment = 8U; |
| +} // namespace |
| + |
| +//----------------------------------------------------------------------------- |
| + |
| +StackBuffer::StackBuffer() |
| + : overflow_(NULL) { |
| + fixed_.next = NULL; |
| + fixed_.cursor = fixed_data_; |
| + fixed_.end = fixed_data_ + kMinSegmentSize; |
| +} |
| + |
| +StackBuffer::~StackBuffer() { |
| + while (overflow_) { |
| + Segment* doomed = overflow_; |
| + overflow_ = overflow_->next; |
| + free(doomed); |
| + } |
| +} |
| + |
| +void* StackBuffer::Allocate(uint32_t delta) { |
| + if (delta % kAlignment) |
| + delta += (kAlignment - (delta % kAlignment)); |
| + |
| + void* result = |
| + AllocateInSegment((overflow_ != NULL) ? overflow_ : &fixed_, delta); |
| + if (result) |
| + return result; |
| + |
| + AddOverflowSegment(delta); |
| + return Allocate(delta); |
| +} |
| + |
| +void* StackBuffer::AllocateInSegment(Segment* segment, uint32_t delta) { |
| + void* result; |
| + if ((segment->end - segment->cursor) >= delta) { |
| + result = segment->cursor; |
| + memset(result, 0, delta); |
| + segment->cursor += delta; |
| + } else { |
| + result = NULL; |
| + } |
| + return result; |
| +} |
| + |
| +void StackBuffer::AddOverflowSegment(uint32_t delta) { |
| + if (delta < kMinSegmentSize) |
| + delta = kMinSegmentSize; |
| + Segment* segment = static_cast<Segment*>(malloc(sizeof(Segment) + delta)); |
| + segment->next = overflow_; |
| + segment->cursor = reinterpret_cast<char*>(segment + 1); |
| + segment->end = segment->cursor + delta; |
| + overflow_ = segment; |
| +} |
| + |
| +//----------------------------------------------------------------------------- |
| + |
| +ContiguousBuffer::ContiguousBuffer() |
| + : ptr_(NULL), |
| + size_(0), |
| + capacity_(0) { |
| +} |
| + |
| +ContiguousBuffer::~ContiguousBuffer() { |
| + free(ptr_); |
| +} |
| + |
| +void* ContiguousBuffer::Allocate(uint32_t delta) { |
| + uint32_t old_size = size_; |
| + uint32_t new_size = old_size + delta; |
| + |
| + if (new_size % kAlignment) |
| + new_size += (kAlignment - (new_size % kAlignment)); |
| + |
| + const uint32_t kInitialCapacity = 512U; |
| + uint32_t new_capacity = std::max(capacity_, kInitialCapacity); |
| + while (new_capacity < new_size) |
| + new_capacity <<= 1; |
| + |
| + if (new_capacity != capacity_) { |
| + ptr_ = static_cast<char*>(realloc(ptr_, new_capacity)); |
| + // TODO: only memset what gets used |
| + memset(ptr_ + capacity_, 0, new_capacity - capacity_); |
| + capacity_ = new_capacity; |
| + } |
| + |
| + size_ = new_size; |
| + return ptr_ + old_size; |
| +} |
| + |
| +void* ContiguousBuffer::Leak() { |
| + char* ptr = ptr_; |
| + ptr_ = NULL; |
| + size_ = 0; |
| + return ptr; |
| +} |
| + |
| +} // namespace mojo |