blob: 48358cddcd820087ccb8bf1b654d67b81dcba6d3 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
Hans Boehm3e4c0762016-05-18 10:09:24 -070017/*
18 * DEPRECATED. DO NOT USE FOR NEW CODE.
19 */
20
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021#ifndef ANDROID_SHARED_BUFFER_H
22#define ANDROID_SHARED_BUFFER_H
23
Hans Boehm3e4c0762016-05-18 10:09:24 -070024#include <atomic>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025#include <stdint.h>
26#include <sys/types.h>
27
28// ---------------------------------------------------------------------------
29
30namespace android {
31
32class SharedBuffer
33{
34public:
35
36 /* flags to use with release() */
37 enum {
38 eKeepStorage = 0x00000001
39 };
40
41 /*! allocate a buffer of size 'size' and acquire() it.
42 * call release() to free it.
43 */
44 static SharedBuffer* alloc(size_t size);
45
46 /*! free the memory associated with the SharedBuffer.
47 * Fails if there are any users associated with this SharedBuffer.
48 * In other words, the buffer must have been release by all its
49 * users.
50 */
Hans Boehm3e4c0762016-05-18 10:09:24 -070051 static void dealloc(const SharedBuffer* released);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052
53 //! access the data for read
54 inline const void* data() const;
55
56 //! access the data for read/write
57 inline void* data();
58
59 //! get size of the buffer
60 inline size_t size() const;
61
62 //! get back a SharedBuffer object from its data
63 static inline SharedBuffer* bufferFromData(void* data);
64
65 //! get back a SharedBuffer object from its data
66 static inline const SharedBuffer* bufferFromData(const void* data);
67
68 //! get the size of a SharedBuffer object from its data
69 static inline size_t sizeFromData(const void* data);
70
71 //! edit the buffer (get a writtable, or non-const, version of it)
72 SharedBuffer* edit() const;
73
74 //! edit the buffer, resizing if needed
75 SharedBuffer* editResize(size_t size) const;
76
77 //! like edit() but fails if a copy is required
78 SharedBuffer* attemptEdit() const;
79
80 //! resize and edit the buffer, loose it's content.
81 SharedBuffer* reset(size_t size) const;
82
83 //! acquire/release a reference on this buffer
84 void acquire() const;
85
86 /*! release a reference on this buffer, with the option of not
87 * freeing the memory associated with it if it was the last reference
88 * returns the previous reference count
89 */
90 int32_t release(uint32_t flags = 0) const;
91
92 //! returns wether or not we're the only owner
93 inline bool onlyOwner() const;
94
95
96private:
97 inline SharedBuffer() { }
98 inline ~SharedBuffer() { }
Mathias Agopiane79aadd2012-08-31 16:20:23 -070099 SharedBuffer(const SharedBuffer&);
100 SharedBuffer& operator = (const SharedBuffer&);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800101
Hans Boehm3e4c0762016-05-18 10:09:24 -0700102 // Must be sized to preserve correct alignment.
103 mutable std::atomic<int32_t> mRefs;
104 size_t mSize;
105 uint32_t mReserved[2];
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800106};
107
Hans Boehm3e4c0762016-05-18 10:09:24 -0700108static_assert(sizeof(SharedBuffer) % 8 == 0
109 && (sizeof(size_t) > 4 || sizeof(SharedBuffer) == 16),
110 "SharedBuffer has unexpected size");
111
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800112// ---------------------------------------------------------------------------
113
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800114const void* SharedBuffer::data() const {
115 return this + 1;
116}
117
118void* SharedBuffer::data() {
119 return this + 1;
120}
121
122size_t SharedBuffer::size() const {
123 return mSize;
124}
125
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700126SharedBuffer* SharedBuffer::bufferFromData(void* data) {
127 return data ? static_cast<SharedBuffer *>(data)-1 : 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800128}
129
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700130const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
131 return data ? static_cast<const SharedBuffer *>(data)-1 : 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800132}
133
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700134size_t SharedBuffer::sizeFromData(const void* data) {
135 return data ? bufferFromData(data)->mSize : 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800136}
137
138bool SharedBuffer::onlyOwner() const {
Hans Boehm3e4c0762016-05-18 10:09:24 -0700139 return (mRefs.load(std::memory_order_acquire) == 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800140}
141
142}; // namespace android
143
144// ---------------------------------------------------------------------------
145
146#endif // ANDROID_VECTOR_H