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 class Buffer { | |
15 public: | |
16 virtual ~Buffer() {} | |
17 virtual void* Allocate(size_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 { | |
darin (slow to review)
2013/10/09 17:05:32
I was thinking this class might be better named Sc
viettrungluu
2013/10/09 20:54:26
I think I like ScratchBuffer more. (To me, ScopedB
| |
23 public: | |
24 StackBuffer(); | |
25 virtual ~StackBuffer(); | |
26 | |
27 virtual void* Allocate(size_t num_bytes) MOJO_OVERRIDE; | |
28 | |
29 private: | |
30 static const size_t kMinSegmentSize = 512; | |
viettrungluu
2013/10/09 17:00:24
Note that C++ requires that you declare storage fo
darin (slow to review)
2013/10/09 17:05:32
OK
| |
31 | |
32 struct Segment { | |
33 Segment* next; | |
34 char* cursor; | |
35 char* end; | |
36 }; | |
37 | |
38 void* AllocateInSegment(Segment* segment, size_t num_bytes); | |
39 void AddOverflowSegment(size_t delta); | |
40 | |
41 char fixed_data_[kMinSegmentSize]; | |
42 Segment fixed_; | |
43 Segment* overflow_; | |
44 | |
45 // Not implemented. | |
46 StackBuffer(const StackBuffer&); | |
viettrungluu
2013/10/09 17:00:24
I wonder if we should have a MOJO_DISALLOW_COPY_AN
darin (slow to review)
2013/10/09 17:05:32
Yes, I wanted to introduce that. Now that we have
| |
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))); | |
viettrungluu
2013/10/09 17:00:24
There's no |Grow()| anymore....
darin (slow to review)
2013/10/09 17:05:32
Oops, yes. I renamed it.
| |
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(size_t num_bytes) MOJO_OVERRIDE; | |
viettrungluu
2013/10/09 17:00:24
So it seems problematic that it might invalidate p
darin (slow to review)
2013/10/09 17:05:32
Oh, good catch. I need to think about that.
| |
83 | |
84 size_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 size_t size_; | |
95 size_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 |