OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_ |
7 | 7 |
8 #include "mojo/public/cpp/bindings/lib/buffer.h" | 8 #include "mojo/public/cpp/bindings/lib/buffer.h" |
9 #include "mojo/public/cpp/system/macros.h" | 9 #include "mojo/public/cpp/system/macros.h" |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 class FixedBuffer : public Buffer { | 38 class FixedBuffer : public Buffer { |
39 public: | 39 public: |
40 FixedBuffer(); | 40 FixedBuffer(); |
41 | 41 |
42 // |size| should be aligned using internal::Align. | 42 // |size| should be aligned using internal::Align. |
43 void Initialize(void* memory, size_t size); | 43 void Initialize(void* memory, size_t size); |
44 | 44 |
45 size_t size() const { return size_; } | 45 size_t size() const { return size_; } |
46 | 46 |
| 47 // Returns the number of bytes used so far. |
| 48 // TODO(vardhan): Introduce this method in |Buffer|? Doesn't seem necessary. |
| 49 size_t BytesUsed() const { return cursor_; } |
| 50 |
47 // Grows the buffer by |num_bytes| and returns a pointer to the start of the | 51 // Grows the buffer by |num_bytes| and returns a pointer to the start of the |
48 // addition. The resulting address is 8-byte aligned, and the content of the | 52 // addition. The resulting address is 8-byte aligned, and the content of the |
49 // memory is zero-filled. | 53 // memory is zero-filled. |
| 54 // TODO(vardhan): Allocate() should safely fail if we run out of buffer space. |
| 55 // This will allow us to, e.g, fail when trying to consume a buffer to |
| 56 // serialize into, and return an insufficient space error. Currently, there |
| 57 // are consumers of FixedBuffer that rely on it CHECK-failing. |
50 void* Allocate(size_t num_bytes) override; | 58 void* Allocate(size_t num_bytes) override; |
51 | 59 |
52 protected: | 60 protected: |
53 char* ptr_; | 61 char* ptr_; |
54 size_t cursor_; | 62 size_t cursor_; |
55 size_t size_; | 63 size_t size_; |
56 | 64 |
57 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBuffer); | 65 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBuffer); |
58 }; | 66 }; |
59 | 67 |
60 class FixedBufferForTesting : public FixedBuffer { | 68 class FixedBufferForTesting : public FixedBuffer { |
61 public: | 69 public: |
62 explicit FixedBufferForTesting(size_t size); | 70 explicit FixedBufferForTesting(size_t size); |
63 ~FixedBufferForTesting() override; | 71 ~FixedBufferForTesting() override; |
64 | 72 |
65 // Returns the internal memory owned by the Buffer to the caller. The Buffer | 73 // Returns the internal memory owned by the Buffer to the caller. The Buffer |
66 // relinquishes its pointer, effectively resetting the state of the Buffer | 74 // relinquishes its pointer, effectively resetting the state of the Buffer |
67 // and leaving the caller responsible for freeing the returned memory address | 75 // and leaving the caller responsible for freeing the returned memory address |
68 // when no longer needed. | 76 // when no longer needed. |
69 void* Leak(); | 77 void* Leak(); |
70 | 78 |
71 private: | 79 private: |
72 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBufferForTesting); | 80 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBufferForTesting); |
73 }; | 81 }; |
74 | 82 |
75 } // namespace internal | 83 } // namespace internal |
76 } // namespace mojo | 84 } // namespace mojo |
77 | 85 |
78 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_ | 86 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_ |
OLD | NEW |