• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 namespace android {
26 
27 class SharedBuffer
28 {
29 public:
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 
91 private:
SharedBuffer()92         inline SharedBuffer() { }
~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 
data()105 const void* SharedBuffer::data() const {
106     return this + 1;
107 }
108 
data()109 void* SharedBuffer::data() {
110     return this + 1;
111 }
112 
size()113 size_t SharedBuffer::size() const {
114     return mSize;
115 }
116 
bufferFromData(void * data)117 SharedBuffer* SharedBuffer::bufferFromData(void* data) {
118     return data ? static_cast<SharedBuffer *>(data)-1 : 0;
119 }
120 
bufferFromData(const void * data)121 const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
122     return data ? static_cast<const SharedBuffer *>(data)-1 : 0;
123 }
124 
sizeFromData(const void * data)125 size_t SharedBuffer::sizeFromData(const void* data) {
126     return data ? bufferFromData(data)->mSize : 0;
127 }
128 
onlyOwner()129 bool SharedBuffer::onlyOwner() const {
130     return (mRefs == 1);
131 }
132 
133 }; // namespace android
134 
135 // ---------------------------------------------------------------------------
136 
137 #endif // ANDROID_VECTOR_H
138