Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Side by Side Diff: mojo/public/cpp/bindings/lib/fixed_buffer.h

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
7
8 #include "mojo/public/cpp/bindings/lib/buffer.h"
9 #include "mojo/public/cpp/system/macros.h"
10
11 namespace mojo {
12 namespace internal {
13
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
19 // FixedBuffer.
20 //
21 // Typical usage:
22 //
23 // {
24 // FixedBuffer buf(8 + 8);
25 //
26 // int* a = static_cast<int*>(buf->Allocate(sizeof(int)));
27 // *a = 2;
28 //
29 // double* b = static_cast<double*>(buf->Allocate(sizeof(double)));
30 // *b = 3.14f;
31 //
32 // void* data = buf.Leak();
33 // Process(data);
34 //
35 // free(data);
36 // }
37
38 class FixedBuffer : public Buffer {
39 public:
40 FixedBuffer();
41
42 // |size| should be aligned using internal::Align.
43 void Initialize(void* memory, size_t size);
44
45 size_t size() const { return size_; }
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
51 // Grows the buffer by |num_bytes| and returns a pointer to the start of the
52 // addition. The resulting address is 8-byte aligned, and the content of the
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.
58 void* Allocate(size_t num_bytes) override;
59
60 protected:
61 char* ptr_;
62 size_t cursor_;
63 size_t size_;
64
65 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBuffer);
66 };
67
68 class FixedBufferForTesting : public FixedBuffer {
69 public:
70 explicit FixedBufferForTesting(size_t size);
71 ~FixedBufferForTesting() override;
72
73 // Returns the internal memory owned by the Buffer to the caller. The Buffer
74 // relinquishes its pointer, effectively resetting the state of the Buffer
75 // and leaving the caller responsible for freeing the returned memory address
76 // when no longer needed.
77 void* Leak();
78
79 private:
80 MOJO_DISALLOW_COPY_AND_ASSIGN(FixedBufferForTesting);
81 };
82
83 } // namespace internal
84 } // namespace mojo
85
86 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_FIXED_BUFFER_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/control_message_proxy.cc ('k') | mojo/public/cpp/bindings/lib/fixed_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698