blob: 9f631213a08694ba9621898a97ad9f9785d2cb22 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * SharedBuffer.h
3 * Android
4 *
5 * Copyright 2005 The Android Open Source Project
6 *
7 */
8
9#ifndef ANDROID_SHARED_BUFFER_H
10#define ANDROID_SHARED_BUFFER_H
11
12#include <stdint.h>
13#include <sys/types.h>
14
15// ---------------------------------------------------------------------------
16
17namespace android {
18
19class SharedBuffer
20{
21public:
22
23 /* flags to use with release() */
24 enum {
25 eKeepStorage = 0x00000001
26 };
27
28 /*! allocate a buffer of size 'size' and acquire() it.
29 * call release() to free it.
30 */
31 static SharedBuffer* alloc(size_t size);
32
33 /*! free the memory associated with the SharedBuffer.
34 * Fails if there are any users associated with this SharedBuffer.
35 * In other words, the buffer must have been release by all its
36 * users.
37 */
38 static ssize_t dealloc(const SharedBuffer* released);
39
40 //! get the SharedBuffer from the data pointer
41 static inline const SharedBuffer* sharedBuffer(const void* data);
42
43 //! access the data for read
44 inline const void* data() const;
45
46 //! access the data for read/write
47 inline void* data();
48
49 //! get size of the buffer
50 inline size_t size() const;
51
52 //! get back a SharedBuffer object from its data
53 static inline SharedBuffer* bufferFromData(void* data);
54
55 //! get back a SharedBuffer object from its data
56 static inline const SharedBuffer* bufferFromData(const void* data);
57
58 //! get the size of a SharedBuffer object from its data
59 static inline size_t sizeFromData(const void* data);
60
61 //! edit the buffer (get a writtable, or non-const, version of it)
62 SharedBuffer* edit() const;
63
64 //! edit the buffer, resizing if needed
65 SharedBuffer* editResize(size_t size) const;
66
67 //! like edit() but fails if a copy is required
68 SharedBuffer* attemptEdit() const;
69
70 //! resize and edit the buffer, loose it's content.
71 SharedBuffer* reset(size_t size) const;
72
73 //! acquire/release a reference on this buffer
74 void acquire() const;
75
76 /*! release a reference on this buffer, with the option of not
77 * freeing the memory associated with it if it was the last reference
78 * returns the previous reference count
79 */
80 int32_t release(uint32_t flags = 0) const;
81
82 //! returns wether or not we're the only owner
83 inline bool onlyOwner() const;
84
85
86private:
87 inline SharedBuffer() { }
88 inline ~SharedBuffer() { }
89 inline SharedBuffer(const SharedBuffer&);
90
91 // 16 bytes. must be sized to preserve correct alingment.
92 mutable int32_t mRefs;
93 size_t mSize;
94 uint32_t mReserved[2];
95};
96
97// ---------------------------------------------------------------------------
98
99const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) {
100 return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0;
101}
102
103const void* SharedBuffer::data() const {
104 return this + 1;
105}
106
107void* SharedBuffer::data() {
108 return this + 1;
109}
110
111size_t SharedBuffer::size() const {
112 return mSize;
113}
114
115SharedBuffer* SharedBuffer::bufferFromData(void* data)
116{
117 return ((SharedBuffer*)data)-1;
118}
119
120const SharedBuffer* SharedBuffer::bufferFromData(const void* data)
121{
122 return ((const SharedBuffer*)data)-1;
123}
124
125size_t SharedBuffer::sizeFromData(const void* data)
126{
127 return (((const SharedBuffer*)data)-1)->mSize;
128}
129
130bool SharedBuffer::onlyOwner() const {
131 return (mRefs == 1);
132}
133
134}; // namespace android
135
136// ---------------------------------------------------------------------------
137
138#endif // ANDROID_VECTOR_H