OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/public/bindings/lib/buffer.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 #include <string.h> |
| 9 |
| 10 #include <algorithm> |
| 11 |
| 12 namespace mojo { |
| 13 |
| 14 const size_t kAlignment = 8U; |
| 15 |
| 16 //----------------------------------------------------------------------------- |
| 17 |
| 18 StackBuffer::StackBuffer() |
| 19 : overflow_(NULL) { |
| 20 fixed_.next = NULL; |
| 21 fixed_.cursor = fixed_data_; |
| 22 fixed_.end = fixed_data_ + kMinSegmentSize; |
| 23 } |
| 24 |
| 25 StackBuffer::~StackBuffer() { |
| 26 while (overflow_) { |
| 27 Segment* doomed = overflow_; |
| 28 overflow_ = overflow_->next; |
| 29 free(doomed); |
| 30 } |
| 31 } |
| 32 |
| 33 void* StackBuffer::Allocate(size_t delta) { |
| 34 if (delta % kAlignment) |
| 35 delta += (kAlignment - (delta % kAlignment)); |
| 36 |
| 37 void* result = |
| 38 AllocateInSegment((overflow_ != NULL) ? overflow_ : &fixed_, delta); |
| 39 if (result) |
| 40 return result; |
| 41 |
| 42 AddOverflowSegment(delta); |
| 43 return Allocate(delta); |
| 44 } |
| 45 |
| 46 void* StackBuffer::AllocateInSegment(Segment* segment, size_t delta) { |
| 47 void* result; |
| 48 if (static_cast<size_t>(segment->end - segment->cursor) >= delta) { |
| 49 result = segment->cursor; |
| 50 memset(result, 0, delta); |
| 51 segment->cursor += delta; |
| 52 } else { |
| 53 result = NULL; |
| 54 } |
| 55 return result; |
| 56 } |
| 57 |
| 58 void StackBuffer::AddOverflowSegment(size_t delta) { |
| 59 if (delta < kMinSegmentSize) |
| 60 delta = kMinSegmentSize; |
| 61 Segment* segment = static_cast<Segment*>(malloc(sizeof(Segment) + delta)); |
| 62 segment->next = overflow_; |
| 63 segment->cursor = reinterpret_cast<char*>(segment + 1); |
| 64 segment->end = segment->cursor + delta; |
| 65 overflow_ = segment; |
| 66 } |
| 67 |
| 68 //----------------------------------------------------------------------------- |
| 69 |
| 70 ContiguousBuffer::ContiguousBuffer() |
| 71 : ptr_(NULL), |
| 72 size_(0), |
| 73 capacity_(0) { |
| 74 } |
| 75 |
| 76 ContiguousBuffer::~ContiguousBuffer() { |
| 77 free(ptr_); |
| 78 } |
| 79 |
| 80 void* ContiguousBuffer::Allocate(size_t delta) { |
| 81 size_t old_size = size_; |
| 82 size_t new_size = old_size + delta; |
| 83 |
| 84 if (new_size % kAlignment) |
| 85 new_size += (kAlignment - (new_size % kAlignment)); |
| 86 |
| 87 const size_t kInitialCapacity = 512U; |
| 88 size_t new_capacity = std::max(capacity_, kInitialCapacity); |
| 89 while (new_capacity < new_size) |
| 90 new_capacity <<= 1; |
| 91 |
| 92 if (new_capacity != capacity_) { |
| 93 ptr_ = static_cast<char*>(realloc(ptr_, new_capacity)); |
| 94 // TODO: only memset what gets used |
| 95 memset(ptr_ + capacity_, 0, new_capacity - capacity_); |
| 96 capacity_ = new_capacity; |
| 97 } |
| 98 |
| 99 size_ = new_size; |
| 100 return ptr_ + old_size; |
| 101 } |
| 102 |
| 103 void* ContiguousBuffer::Leak() { |
| 104 char* ptr = ptr_; |
| 105 ptr_ = NULL; |
| 106 size_ = 0; |
| 107 return ptr; |
| 108 } |
| 109 |
| 110 } // namespace mojo |
OLD | NEW |