summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Giro2015-09-23 10:22:59 -0500
committerSergio Giro2015-09-23 10:22:59 -0500
commitd2529f28308ba7583f4d8baaa937218d81cbe8bd (patch)
tree08e7ec119fa22c98d69f649e7ffa05f544a7d9f0 /libutils/SharedBuffer.h
parent1d758c90959225c76ed4d318290ad1bf11532409 (diff)
downloadplatform-system-core-d2529f28308ba7583f4d8baaa937218d81cbe8bd.tar.gz
platform-system-core-d2529f28308ba7583f4d8baaa937218d81cbe8bd.tar.xz
platform-system-core-d2529f28308ba7583f4d8baaa937218d81cbe8bd.zip
libutils: hide SharedBuffer by moving SharedBuffer.h to the implementation directory
Some methods in header files of classes using SharedBuffer need to be moved to the implementation files accordingly Change-Id: I891f3ace2b940ab219e4e449040bfed71c0547db
Diffstat (limited to 'libutils/SharedBuffer.h')
-rw-r--r--libutils/SharedBuffer.h137
1 files changed, 137 insertions, 0 deletions
diff --git a/libutils/SharedBuffer.h b/libutils/SharedBuffer.h
new file mode 100644
index 000000000..b6709537e
--- /dev/null
+++ b/libutils/SharedBuffer.h
@@ -0,0 +1,137 @@
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 //! access the data for read
49 inline const void* data() const;
50
51 //! access the data for read/write
52 inline void* data();
53
54 //! get size of the buffer
55 inline size_t size() const;
56
57 //! get back a SharedBuffer object from its data
58 static inline SharedBuffer* bufferFromData(void* data);
59
60 //! get back a SharedBuffer object from its data
61 static inline const SharedBuffer* bufferFromData(const void* data);
62
63 //! get the size of a SharedBuffer object from its data
64 static inline size_t sizeFromData(const void* data);
65
66 //! edit the buffer (get a writtable, or non-const, version of it)
67 SharedBuffer* edit() const;
68
69 //! edit the buffer, resizing if needed
70 SharedBuffer* editResize(size_t size) const;
71
72 //! like edit() but fails if a copy is required
73 SharedBuffer* attemptEdit() const;
74
75 //! resize and edit the buffer, loose it's content.
76 SharedBuffer* reset(size_t size) const;
77
78 //! acquire/release a reference on this buffer
79 void acquire() const;
80
81 /*! release a reference on this buffer, with the option of not
82 * freeing the memory associated with it if it was the last reference
83 * returns the previous reference count
84 */
85 int32_t release(uint32_t flags = 0) const;
86
87 //! returns wether or not we're the only owner
88 inline bool onlyOwner() const;
89
90
91private:
92 inline SharedBuffer() { }
93 inline ~SharedBuffer() { }
94 SharedBuffer(const SharedBuffer&);
95 SharedBuffer& operator = (const SharedBuffer&);
96
97 // 16 bytes. must be sized to preserve correct alignment.
98 mutable int32_t mRefs;
99 size_t mSize;
100 uint32_t mReserved[2];
101};
102
103// ---------------------------------------------------------------------------
104
105const void* SharedBuffer::data() const {
106 return this + 1;
107}
108
109void* SharedBuffer::data() {
110 return this + 1;
111}
112
113size_t SharedBuffer::size() const {
114 return mSize;
115}
116
117SharedBuffer* SharedBuffer::bufferFromData(void* data) {
118 return data ? static_cast<SharedBuffer *>(data)-1 : 0;
119}
120
121const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
122 return data ? static_cast<const SharedBuffer *>(data)-1 : 0;
123}
124
125size_t SharedBuffer::sizeFromData(const void* data) {
126 return data ? bufferFromData(data)->mSize : 0;
127}
128
129bool SharedBuffer::onlyOwner() const {
130 return (mRefs == 1);
131}
132
133}; // namespace android
134
135// ---------------------------------------------------------------------------
136
137#endif // ANDROID_VECTOR_H