| Index: mojo/public/cpp/bindings/lib/buffer.h
|
| diff --git a/mojo/public/cpp/bindings/lib/buffer.h b/mojo/public/cpp/bindings/lib/buffer.h
|
| index c3b570e7767d5aee8a2e3593fa9a2d849d3e4179..213a44590f60838403f31f534fa83bc3c45682e5 100644
|
| --- a/mojo/public/cpp/bindings/lib/buffer.h
|
| +++ b/mojo/public/cpp/bindings/lib/buffer.h
|
| @@ -7,15 +7,61 @@
|
|
|
| #include <stddef.h>
|
|
|
| +#include "base/logging.h"
|
| +#include "base/macros.h"
|
| +#include "mojo/public/cpp/bindings/lib/bindings_internal.h"
|
| +
|
| namespace mojo {
|
| namespace internal {
|
|
|
| -// Buffer provides a way to allocate memory. Allocations are 8-byte aligned and
|
| -// zero-initialized. Allocations remain valid for the lifetime of the Buffer.
|
| +// Buffer provides an interface to allocate memory blocks which are 8-byte
|
| +// aligned and zero-initialized. It doesn't own the underlying memory. Users
|
| +// must ensure that the memory stays valid while using the allocated blocks from
|
| +// Buffer.
|
| class Buffer {
|
| public:
|
| - virtual ~Buffer() {}
|
| - virtual void* Allocate(size_t num_bytes) = 0;
|
| + Buffer() {}
|
| +
|
| + // The memory must have been zero-initialized. |data| must be 8-byte
|
| + // aligned.
|
| + void Initialize(void* data, size_t size) {
|
| + DCHECK(IsAligned(data));
|
| +
|
| + data_ = data;
|
| + size_ = size;
|
| + cursor_ = reinterpret_cast<uintptr_t>(data);
|
| + data_end_ = cursor_ + size;
|
| + }
|
| +
|
| + size_t size() const { return size_; }
|
| +
|
| + void* data() const { return data_; }
|
| +
|
| + // Allocates |num_bytes| from the buffer and returns a pointer to the start of
|
| + // the allocated block.
|
| + // The resulting address is 8-byte aligned, and the content of the memory is
|
| + // zero-filled.
|
| + void* Allocate(size_t num_bytes) {
|
| + num_bytes = Align(num_bytes);
|
| + uintptr_t result = cursor_;
|
| + cursor_ += num_bytes;
|
| + if (cursor_ > data_end_ || cursor_ < result) {
|
| + NOTREACHED();
|
| + cursor_ -= num_bytes;
|
| + return nullptr;
|
| + }
|
| +
|
| + return reinterpret_cast<void*>(result);
|
| + }
|
| +
|
| + private:
|
| + void* data_ = nullptr;
|
| + size_t size_ = 0;
|
| +
|
| + uintptr_t cursor_ = 0;
|
| + uintptr_t data_end_ = 0;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(Buffer);
|
| };
|
|
|
| } // namespace internal
|
|
|