1 /* 2 * Copyright 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_PIXELFLINGER_SHARED_BUFFER_H 18 #define ANDROID_PIXELFLINGER_SHARED_BUFFER_H 19 20 #include <stdint.h> 21 #include <sys/types.h> 22 23 // --------------------------------------------------------------------------- 24 25 namespace android { 26 namespace tinyutils { 27 28 class SharedBuffer 29 { 30 public: 31 32 /* flags to use with release() */ 33 enum { 34 eKeepStorage = 0x00000001 35 }; 36 37 /*! allocate a buffer of size 'size' and acquire() it. 38 * call release() to free it. 39 */ 40 static SharedBuffer* alloc(size_t size); 41 42 /*! free the memory associated with the SharedBuffer. 43 * Fails if there are any users associated with this SharedBuffer. 44 * In other words, the buffer must have been release by all its 45 * users. 46 */ 47 static ssize_t dealloc(const SharedBuffer* released); 48 49 //! get the SharedBuffer from the data pointer 50 static inline const SharedBuffer* sharedBuffer(const void* data); 51 52 //! access the data for read 53 inline const void* data() const; 54 55 //! access the data for read/write 56 inline void* data(); 57 58 //! get size of the buffer 59 inline size_t size() const; 60 61 //! get back a SharedBuffer object from its data 62 static inline SharedBuffer* bufferFromData(void* data); 63 64 //! get back a SharedBuffer object from its data 65 static inline const SharedBuffer* bufferFromData(const void* data); 66 67 //! get the size of a SharedBuffer object from its data 68 static inline size_t sizeFromData(const void* data); 69 70 //! edit the buffer (get a writtable, or non-const, version of it) 71 SharedBuffer* edit() const; 72 73 //! edit the buffer, resizing if needed 74 SharedBuffer* editResize(size_t size) const; 75 76 //! like edit() but fails if a copy is required 77 SharedBuffer* attemptEdit() const; 78 79 //! resize and edit the buffer, loose it's content. 80 SharedBuffer* reset(size_t size) const; 81 82 //! acquire/release a reference on this buffer 83 void acquire() const; 84 85 /*! release a reference on this buffer, with the option of not 86 * freeing the memory associated with it if it was the last reference 87 * returns the previous reference count 88 */ 89 int32_t release(uint32_t flags = 0) const; 90 91 //! returns wether or not we're the only owner 92 inline bool onlyOwner() const; 93 94 95 private: 96 inline SharedBuffer() { } 97 inline ~SharedBuffer() { } 98 inline SharedBuffer(const SharedBuffer&); 99 100 // 16 bytes. must be sized to preserve correct alingment. 101 mutable int32_t mRefs; 102 size_t mSize; 103 uint32_t mReserved[2]; 104 }; 105 106 // --------------------------------------------------------------------------- 107 108 const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) { 109 return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0; 110 } 111 112 const void* SharedBuffer::data() const { 113 return this + 1; 114 } 115 116 void* SharedBuffer::data() { 117 return this + 1; 118 } 119 120 size_t SharedBuffer::size() const { 121 return mSize; 122 } 123 124 SharedBuffer* SharedBuffer::bufferFromData(void* data) 125 { 126 return ((SharedBuffer*)data)-1; 127 } 128 129 const SharedBuffer* SharedBuffer::bufferFromData(const void* data) 130 { 131 return ((const SharedBuffer*)data)-1; 132 } 133 134 size_t SharedBuffer::sizeFromData(const void* data) 135 { 136 return (((const SharedBuffer*)data)-1)->mSize; 137 } 138 139 bool SharedBuffer::onlyOwner() const { 140 return (mRefs == 1); 141 } 142 143 } // namespace tinyutils 144 } // namespace android 145 146 // --------------------------------------------------------------------------- 147 148 #endif // ANDROID_PIXELFLINGER_SHARED_BUFFER_H 149