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