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 #ifndef MOJO_PUBLIC_BINDINGS_LIB_BUFFER_H_ |
| 6 #define MOJO_PUBLIC_BINDINGS_LIB_BUFFER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "mojo/public/system/macros.h" |
| 11 |
| 12 namespace mojo { |
| 13 |
| 14 class Buffer { |
| 15 public: |
| 16 virtual ~Buffer() {} |
| 17 virtual void* Allocate(uint32_t num_bytes) = 0; |
| 18 }; |
| 19 |
| 20 // The following class is designed to be allocated on the stack. If necessary, |
| 21 // it will failover to allocating objects on the heap. |
| 22 class StackBuffer : public Buffer { |
| 23 public: |
| 24 StackBuffer(); |
| 25 virtual ~StackBuffer(); |
| 26 |
| 27 virtual void* Allocate(uint32_t num_bytes) MOJO_OVERRIDE; |
| 28 |
| 29 private: |
| 30 static const uint32_t kMinSegmentSize = 512; |
| 31 |
| 32 struct Segment { |
| 33 Segment* next; |
| 34 char* cursor; |
| 35 char* end; |
| 36 }; |
| 37 |
| 38 void* AllocateInSegment(Segment* segment, uint32_t num_bytes); |
| 39 void AddOverflowSegment(uint32_t delta); |
| 40 |
| 41 char fixed_data_[kMinSegmentSize]; |
| 42 Segment fixed_; |
| 43 Segment* overflow_; |
| 44 |
| 45 // Not implemented. |
| 46 StackBuffer(const StackBuffer&); |
| 47 void operator=(const StackBuffer&); |
| 48 }; |
| 49 |
| 50 // ContiguousBuffer provides a simple way to allocate objects within a |
| 51 // contiguous chunk of memory. Objects are allocated by calling the |Allocate| |
| 52 // method, which extends the buffer accordingly. Objects allocated in this way |
| 53 // are not freed explicitly. Instead, they remain valid so long as the |
| 54 // ContiguousBuffer remains valid. The Leak method may be used to steal the |
| 55 // underlying memory from the ContiguousBuffer. |
| 56 // |
| 57 // Typical usage: |
| 58 // |
| 59 // { |
| 60 // ContiguousBuffer buf; |
| 61 // |
| 62 // int* a = static_cast<int*>(buf.Allocate(sizeof(int))); |
| 63 // *a = 2; |
| 64 // |
| 65 // double* b = static_cast<double*>(buf.Grow(sizeof(double))); |
| 66 // *b = 3.14f; |
| 67 // |
| 68 // void* data = buf.Leak(); |
| 69 // Process(data); |
| 70 // |
| 71 // free(data); |
| 72 // } |
| 73 // |
| 74 class ContiguousBuffer : public Buffer { |
| 75 public: |
| 76 ContiguousBuffer(); |
| 77 virtual ~ContiguousBuffer(); |
| 78 |
| 79 // Grows the buffer by |num_bytes| and returns a pointer to the start of the |
| 80 // addition. The resulting address is 8-byte aligned, and the contents of the |
| 81 // memory is zero-filled. |
| 82 virtual void* Allocate(uint32_t num_bytes) MOJO_OVERRIDE; |
| 83 |
| 84 uint32_t size() const { return size_; } |
| 85 |
| 86 // Returns the internal memory owned by the Buffer to the caller. The Buffer |
| 87 // relinquishes its pointer, effectively resetting the state of the Buffer |
| 88 // and leaving the caller responsible for freeing the returned memory address |
| 89 // when no longer needed. |
| 90 void* Leak(); |
| 91 |
| 92 private: |
| 93 char* ptr_; |
| 94 uint32_t size_; |
| 95 uint32_t capacity_; |
| 96 |
| 97 // Not implemented. |
| 98 ContiguousBuffer(const ContiguousBuffer&); |
| 99 void operator=(const ContiguousBuffer&); |
| 100 }; |
| 101 |
| 102 } // namespace mojo |
| 103 |
| 104 #endif // MOJO_PUBLIC_BINDINGS_LIB_BUFFER_H_ |
OLD | NEW |