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
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
8 #include "mojo/public/cpp/bindings/lib/buffer.h"
9 #include "mojo/public/cpp/system/macros.h"
14 // FixedBuffer provides a simple way to allocate objects within a fixed chunk
15 // of memory. Objects are allocated by calling the |Allocate| method, which
16 // extends the buffer accordingly. Objects allocated in this way are not freed
17 // explicitly. Instead, they remain valid so long as the FixedBuffer remains
18 // valid. The Leak method may be used to steal the underlying memory from the
24 // FixedBuffer buf(8 + 8);
26 // int* a = static_cast<int*>(buf->Allocate(sizeof(int)));
29 // double* b = static_cast<double*>(buf->Allocate(sizeof(double)));
32 // void* data = buf.Leak();
38 class FixedBuffer : public Buffer {
40 explicit FixedBuffer(size_t size);
41 ~FixedBuffer() override;
43 // Grows the buffer by |num_bytes| and returns a pointer to the start of the
44 // addition. The resulting address is 8-byte aligned, and the content of the
45 // memory is zero-filled.
46 void* Allocate(size_t num_bytes) override;
48 size_t size() const { return size_; }
50 // Returns the internal memory owned by the Buffer to the caller. The Buffer
51 // relinquishes its pointer, effectively resetting the state of the Buffer
52 // and leaving the caller responsible for freeing the returned memory address
53 // when no longer needed.
61 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBuffer);
64 } // namespace internal
67 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_