15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
6e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#define MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "mojo/public/cpp/bindings/lib/buffer.h"
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "mojo/public/cpp/system/macros.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace mojo {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace internal {
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// FixedBuffer provides a simple way to allocate objects within a fixed chunk
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// of memory. Objects are allocated by calling the |Allocate| method, which
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// extends the buffer accordingly. Objects allocated in this way are not freed
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// explicitly. Instead, they remain valid so long as the FixedBuffer remains
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// valid.  The Leak method may be used to steal the underlying memory from the
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// FixedBuffer.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Typical usage:
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     FixedBuffer buf(8 + 8);
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     int* a = static_cast<int*>(buf->Allocate(sizeof(int)));
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     *a = 2;
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     double* b = static_cast<double*>(buf->Allocate(sizeof(double)));
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     *b = 3.14f;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     void* data = buf.Leak();
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     Process(data);
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     free(data);
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   }
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class FixedBuffer : public Buffer {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit FixedBuffer(size_t size);
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~FixedBuffer();
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Grows the buffer by |num_bytes| and returns a pointer to the start of the
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // addition. The resulting address is 8-byte aligned, and the content of the
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // memory is zero-filled.
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void* Allocate(size_t num_bytes) MOJO_OVERRIDE;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t size() const { return size_; }
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the internal memory owned by the Buffer to the caller. The Buffer
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // relinquishes its pointer, effectively resetting the state of the Buffer
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // and leaving the caller responsible for freeing the returned memory address
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // when no longer needed.
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void* Leak();
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  char* ptr_;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t cursor_;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t size_;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBuffer);
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace internal
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace mojo
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
68