summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThe Android Open Source Project2009-03-03 21:31:44 -0600
committerAlex Ray2013-07-30 15:56:49 -0500
commitcbb1011c95e0c25c29e40e203a6a31bccd029da3 (patch)
tree4697a3a86867bf33c2d9dd4e4a2743d3eb947523 /include/utils/SharedBuffer.h
parentcf59fa8dc7ddca5a172860223b06afed5d4ec0e0 (diff)
downloadplatform-system-core-cbb1011c95e0c25c29e40e203a6a31bccd029da3.tar.gz
platform-system-core-cbb1011c95e0c25c29e40e203a6a31bccd029da3.tar.xz
platform-system-core-cbb1011c95e0c25c29e40e203a6a31bccd029da3.zip
auto import from //depot/cupcake/@135843
Diffstat (limited to 'include/utils/SharedBuffer.h')
-rw-r--r--include/utils/SharedBuffer.h146
1 files changed, 146 insertions, 0 deletions
diff --git a/include/utils/SharedBuffer.h b/include/utils/SharedBuffer.h
new file mode 100644
index 000000000..24508b0f7
--- /dev/null
+++ b/include/utils/SharedBuffer.h
@@ -0,0 +1,146 @@
1/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SHARED_BUFFER_H
18#define ANDROID_SHARED_BUFFER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23// ---------------------------------------------------------------------------
24
25namespace android {
26
27class SharedBuffer
28{
29public:
30
31 /* flags to use with release() */
32 enum {
33 eKeepStorage = 0x00000001
34 };
35
36 /*! allocate a buffer of size 'size' and acquire() it.
37 * call release() to free it.
38 */
39 static SharedBuffer* alloc(size_t size);
40
41 /*! free the memory associated with the SharedBuffer.
42 * Fails if there are any users associated with this SharedBuffer.
43 * In other words, the buffer must have been release by all its
44 * users.
45 */
46 static ssize_t dealloc(const SharedBuffer* released);
47
48 //! get the SharedBuffer from the data pointer
49 static inline const SharedBuffer* sharedBuffer(const void* data);
50
51 //! access the data for read
52 inline const void* data() const;
53
54 //! access the data for read/write
55 inline void* data();
56
57 //! get size of the buffer
58 inline size_t size() const;
59
60 //! get back a SharedBuffer object from its data
61 static inline SharedBuffer* bufferFromData(void* data);
62
63 //! get back a SharedBuffer object from its data
64 static inline const SharedBuffer* bufferFromData(const void* data);
65
66 //! get the size of a SharedBuffer object from its data
67 static inline size_t sizeFromData(const void* data);
68
69 //! edit the buffer (get a writtable, or non-const, version of it)
70 SharedBuffer* edit() const;
71
72 //! edit the buffer, resizing if needed
73 SharedBuffer* editResize(size_t size) const;
74
75 //! like edit() but fails if a copy is required
76 SharedBuffer* attemptEdit() const;
77
78 //! resize and edit the buffer, loose it's content.
79 SharedBuffer* reset(size_t size) const;
80
81 //! acquire/release a reference on this buffer
82 void acquire() const;
83
84 /*! release a reference on this buffer, with the option of not
85 * freeing the memory associated with it if it was the last reference
86 * returns the previous reference count
87 */
88 int32_t release(uint32_t flags = 0) const;
89
90 //! returns wether or not we're the only owner
91 inline bool onlyOwner() const;
92
93
94private:
95 inline SharedBuffer() { }
96 inline ~SharedBuffer() { }
97 inline SharedBuffer(const SharedBuffer&);
98
99 // 16 bytes. must be sized to preserve correct alingment.
100 mutable int32_t mRefs;
101 size_t mSize;
102 uint32_t mReserved[2];
103};
104
105// ---------------------------------------------------------------------------
106
107const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) {
108 return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0;
109}
110
111const void* SharedBuffer::data() const {
112 return this + 1;
113}
114
115void* SharedBuffer::data() {
116 return this + 1;
117}
118
119size_t SharedBuffer::size() const {
120 return mSize;
121}
122
123SharedBuffer* SharedBuffer::bufferFromData(void* data)
124{
125 return ((SharedBuffer*)data)-1;
126}
127
128const SharedBuffer* SharedBuffer::bufferFromData(const void* data)
129{
130 return ((const SharedBuffer*)data)-1;
131}
132
133size_t SharedBuffer::sizeFromData(const void* data)
134{
135 return (((const SharedBuffer*)data)-1)->mSize;
136}
137
138bool SharedBuffer::onlyOwner() const {
139 return (mRefs == 1);
140}
141
142}; // namespace android
143
144// ---------------------------------------------------------------------------
145
146#endif // ANDROID_VECTOR_H