The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | //! get the SharedBuffer from the data pointer |
| 49 | static inline const SharedBuffer* sharedBuffer(const void* data); |
| 50 | |
| 51 | //! access the data for read |
| 52 | inline const void* data() const; |
| 53 | |
| 54 | //! access the data for read/write |
| 55 | inline void* data(); |
| 56 | |
| 57 | //! get size of the buffer |
| 58 | inline size_t size() const; |
| 59 | |
| 60 | //! get back a SharedBuffer object from its data |
| 61 | static inline SharedBuffer* bufferFromData(void* data); |
| 62 | |
| 63 | //! get back a SharedBuffer object from its data |
| 64 | static inline const SharedBuffer* bufferFromData(const void* data); |
| 65 | |
| 66 | //! get the size of a SharedBuffer object from its data |
| 67 | static inline size_t sizeFromData(const void* data); |
| 68 | |
| 69 | //! edit the buffer (get a writtable, or non-const, version of it) |
| 70 | SharedBuffer* edit() const; |
| 71 | |
| 72 | //! edit the buffer, resizing if needed |
| 73 | SharedBuffer* editResize(size_t size) const; |
| 74 | |
| 75 | //! like edit() but fails if a copy is required |
| 76 | SharedBuffer* attemptEdit() const; |
| 77 | |
| 78 | //! resize and edit the buffer, loose it's content. |
| 79 | SharedBuffer* reset(size_t size) const; |
| 80 | |
| 81 | //! acquire/release a reference on this buffer |
| 82 | void acquire() const; |
| 83 | |
| 84 | /*! release a reference on this buffer, with the option of not |
| 85 | * freeing the memory associated with it if it was the last reference |
| 86 | * returns the previous reference count |
| 87 | */ |
| 88 | int32_t release(uint32_t flags = 0) const; |
| 89 | |
| 90 | //! returns wether or not we're the only owner |
| 91 | inline bool onlyOwner() const; |
| 92 | |
| 93 | |
| 94 | private: |
| 95 | inline SharedBuffer() { } |
| 96 | inline ~SharedBuffer() { } |
| 97 | inline SharedBuffer(const SharedBuffer&); |
| 98 | |
| 99 | // 16 bytes. must be sized to preserve correct alingment. |
| 100 | mutable int32_t mRefs; |
| 101 | size_t mSize; |
| 102 | uint32_t mReserved[2]; |
| 103 | }; |
| 104 | |
| 105 | // --------------------------------------------------------------------------- |
| 106 | |
| 107 | const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) { |
| 108 | return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0; |
| 109 | } |
| 110 | |
| 111 | const void* SharedBuffer::data() const { |
| 112 | return this + 1; |
| 113 | } |
| 114 | |
| 115 | void* SharedBuffer::data() { |
| 116 | return this + 1; |
| 117 | } |
| 118 | |
| 119 | size_t SharedBuffer::size() const { |
| 120 | return mSize; |
| 121 | } |
| 122 | |
| 123 | SharedBuffer* SharedBuffer::bufferFromData(void* data) |
| 124 | { |
| 125 | return ((SharedBuffer*)data)-1; |
| 126 | } |
| 127 | |
| 128 | const SharedBuffer* SharedBuffer::bufferFromData(const void* data) |
| 129 | { |
| 130 | return ((const SharedBuffer*)data)-1; |
| 131 | } |
| 132 | |
| 133 | size_t SharedBuffer::sizeFromData(const void* data) |
| 134 | { |
| 135 | return (((const SharedBuffer*)data)-1)->mSize; |
| 136 | } |
| 137 | |
| 138 | bool SharedBuffer::onlyOwner() const { |
| 139 | return (mRefs == 1); |
| 140 | } |
| 141 | |
| 142 | }; // namespace android |
| 143 | |
| 144 | // --------------------------------------------------------------------------- |
| 145 | |
| 146 | #endif // ANDROID_VECTOR_H |