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 * DEPRECATED. DO NOT USE FOR NEW CODE.
18 */
19 #ifndef ANDROID_SHARED_BUFFER_H
20 #define ANDROID_SHARED_BUFFER_H
21 #include <atomic>
22 #include <stdint.h>
23 #include <sys/types.h>
24 // ---------------------------------------------------------------------------
25 namespace android {
26 class SharedBuffer
27 {
28 public:
29 /* flags to use with release() */
30 enum {
31 eKeepStorage = 0x00000001
32 };
33 /*! allocate a buffer of size 'size' and acquire() it.
34 * call release() to free it.
35 */
36 static SharedBuffer* alloc(size_t size);
37
38 /*! free the memory associated with the SharedBuffer.
39 * Fails if there are any users associated with this SharedBuffer.
40 * In other words, the buffer must have been release by all its
41 * users.
42 */
43 static void dealloc(const SharedBuffer* released);
44 //! access the data for read
45 inline const void* data() const;
46
47 //! access the data for read/write
48 inline void* data();
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 //! get the size of a SharedBuffer object from its data
58 static inline size_t sizeFromData(const void* data);
59
60 //! edit the buffer (get a writtable, or non-const, version of it)
61 SharedBuffer* edit() const;
62 //! edit the buffer, resizing if needed
63 SharedBuffer* editResize(size_t size) const;
64 //! like edit() but fails if a copy is required
65 SharedBuffer* attemptEdit() const;
66
67 //! resize and edit the buffer, loose it's content.
68 SharedBuffer* reset(size_t size) const;
69 //! acquire/release a reference on this buffer
70 void acquire() const;
71
72 /*! release a reference on this buffer, with the option of not
73 * freeing the memory associated with it if it was the last reference
74 * returns the previous reference count
75 */
76 int32_t release(uint32_t flags = 0) const;
77
78 //! returns wether or not we're the only owner
79 inline bool onlyOwner() const;
80
81 private:
SharedBuffer()82 inline SharedBuffer() { }
~SharedBuffer()83 inline ~SharedBuffer() { }
84 SharedBuffer(const SharedBuffer&);
85 SharedBuffer& operator = (const SharedBuffer&);
86
87 // Must be sized to preserve correct alignment.
88 mutable std::atomic<int32_t> mRefs;
89 size_t mSize;
90 uint32_t mReserved[2];
91 };
92 static_assert(sizeof(SharedBuffer) % 8 == 0
93 && (sizeof(size_t) > 4 || sizeof(SharedBuffer) == 16),
94 "SharedBuffer has unexpected size");
95 // ---------------------------------------------------------------------------
data()96 const void* SharedBuffer::data() const {
97 return this + 1;
98 }
data()99 void* SharedBuffer::data() {
100 return this + 1;
101 }
size()102 size_t SharedBuffer::size() const {
103 return mSize;
104 }
bufferFromData(void * data)105 SharedBuffer* SharedBuffer::bufferFromData(void* data) {
106 return data ? static_cast<SharedBuffer *>(data)-1 : nullptr;
107 }
108
bufferFromData(const void * data)109 const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
110 return data ? static_cast<const SharedBuffer *>(data)-1 : nullptr;
111 }
sizeFromData(const void * data)112 size_t SharedBuffer::sizeFromData(const void* data) {
113 return data ? bufferFromData(data)->mSize : 0;
114 }
onlyOwner()115 bool SharedBuffer::onlyOwner() const {
116 return (mRefs.load(std::memory_order_acquire) == 1);
117 }
118 } // namespace android
119 // ---------------------------------------------------------------------------
120 #endif // ANDROID_VECTOR_H
121