Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2005 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #ifndef ANDROID_SHARED_BUFFER_H |
michael@0 | 18 | #define ANDROID_SHARED_BUFFER_H |
michael@0 | 19 | |
michael@0 | 20 | #include <stdint.h> |
michael@0 | 21 | #include <sys/types.h> |
michael@0 | 22 | |
michael@0 | 23 | // --------------------------------------------------------------------------- |
michael@0 | 24 | |
michael@0 | 25 | namespace android { |
michael@0 | 26 | |
michael@0 | 27 | class SharedBuffer |
michael@0 | 28 | { |
michael@0 | 29 | public: |
michael@0 | 30 | |
michael@0 | 31 | /* flags to use with release() */ |
michael@0 | 32 | enum { |
michael@0 | 33 | eKeepStorage = 0x00000001 |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | /*! allocate a buffer of size 'size' and acquire() it. |
michael@0 | 37 | * call release() to free it. |
michael@0 | 38 | */ |
michael@0 | 39 | static SharedBuffer* alloc(size_t size); |
michael@0 | 40 | |
michael@0 | 41 | /*! free the memory associated with the SharedBuffer. |
michael@0 | 42 | * Fails if there are any users associated with this SharedBuffer. |
michael@0 | 43 | * In other words, the buffer must have been release by all its |
michael@0 | 44 | * users. |
michael@0 | 45 | */ |
michael@0 | 46 | static ssize_t dealloc(const SharedBuffer* released); |
michael@0 | 47 | |
michael@0 | 48 | //! get the SharedBuffer from the data pointer |
michael@0 | 49 | static inline const SharedBuffer* sharedBuffer(const void* data); |
michael@0 | 50 | |
michael@0 | 51 | //! access the data for read |
michael@0 | 52 | inline const void* data() const; |
michael@0 | 53 | |
michael@0 | 54 | //! access the data for read/write |
michael@0 | 55 | inline void* data(); |
michael@0 | 56 | |
michael@0 | 57 | //! get size of the buffer |
michael@0 | 58 | inline size_t size() const; |
michael@0 | 59 | |
michael@0 | 60 | //! get back a SharedBuffer object from its data |
michael@0 | 61 | static inline SharedBuffer* bufferFromData(void* data); |
michael@0 | 62 | |
michael@0 | 63 | //! get back a SharedBuffer object from its data |
michael@0 | 64 | static inline const SharedBuffer* bufferFromData(const void* data); |
michael@0 | 65 | |
michael@0 | 66 | //! get the size of a SharedBuffer object from its data |
michael@0 | 67 | static inline size_t sizeFromData(const void* data); |
michael@0 | 68 | |
michael@0 | 69 | //! edit the buffer (get a writtable, or non-const, version of it) |
michael@0 | 70 | SharedBuffer* edit() const; |
michael@0 | 71 | |
michael@0 | 72 | //! edit the buffer, resizing if needed |
michael@0 | 73 | SharedBuffer* editResize(size_t size) const; |
michael@0 | 74 | |
michael@0 | 75 | //! like edit() but fails if a copy is required |
michael@0 | 76 | SharedBuffer* attemptEdit() const; |
michael@0 | 77 | |
michael@0 | 78 | //! resize and edit the buffer, loose it's content. |
michael@0 | 79 | SharedBuffer* reset(size_t size) const; |
michael@0 | 80 | |
michael@0 | 81 | //! acquire/release a reference on this buffer |
michael@0 | 82 | void acquire() const; |
michael@0 | 83 | |
michael@0 | 84 | /*! release a reference on this buffer, with the option of not |
michael@0 | 85 | * freeing the memory associated with it if it was the last reference |
michael@0 | 86 | * returns the previous reference count |
michael@0 | 87 | */ |
michael@0 | 88 | int32_t release(uint32_t flags = 0) const; |
michael@0 | 89 | |
michael@0 | 90 | //! returns wether or not we're the only owner |
michael@0 | 91 | inline bool onlyOwner() const; |
michael@0 | 92 | |
michael@0 | 93 | |
michael@0 | 94 | private: |
michael@0 | 95 | inline SharedBuffer() { } |
michael@0 | 96 | inline ~SharedBuffer() { } |
michael@0 | 97 | inline SharedBuffer(const SharedBuffer&); |
michael@0 | 98 | |
michael@0 | 99 | // 16 bytes. must be sized to preserve correct alingment. |
michael@0 | 100 | mutable int32_t mRefs; |
michael@0 | 101 | size_t mSize; |
michael@0 | 102 | uint32_t mReserved[2]; |
michael@0 | 103 | }; |
michael@0 | 104 | |
michael@0 | 105 | // --------------------------------------------------------------------------- |
michael@0 | 106 | |
michael@0 | 107 | const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) { |
michael@0 | 108 | return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | const void* SharedBuffer::data() const { |
michael@0 | 112 | return this + 1; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | void* SharedBuffer::data() { |
michael@0 | 116 | return this + 1; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | size_t SharedBuffer::size() const { |
michael@0 | 120 | return mSize; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | SharedBuffer* SharedBuffer::bufferFromData(void* data) |
michael@0 | 124 | { |
michael@0 | 125 | return ((SharedBuffer*)data)-1; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | const SharedBuffer* SharedBuffer::bufferFromData(const void* data) |
michael@0 | 129 | { |
michael@0 | 130 | return ((const SharedBuffer*)data)-1; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | size_t SharedBuffer::sizeFromData(const void* data) |
michael@0 | 134 | { |
michael@0 | 135 | return (((const SharedBuffer*)data)-1)->mSize; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | bool SharedBuffer::onlyOwner() const { |
michael@0 | 139 | return (mRefs == 1); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | }; // namespace android |
michael@0 | 143 | |
michael@0 | 144 | // --------------------------------------------------------------------------- |
michael@0 | 145 | |
michael@0 | 146 | #endif // ANDROID_VECTOR_H |