| The Android Open Source Project | cbb1011 | 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 |  | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 17 | /* | 
 | 18 |  * DEPRECATED.  DO NOT USE FOR NEW CODE. | 
 | 19 |  */ | 
 | 20 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #ifndef ANDROID_SHARED_BUFFER_H | 
 | 22 | #define ANDROID_SHARED_BUFFER_H | 
 | 23 |  | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 24 | #include <atomic> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <stdint.h> | 
 | 26 | #include <sys/types.h> | 
 | 27 |  | 
 | 28 | // --------------------------------------------------------------------------- | 
 | 29 |  | 
 | 30 | namespace android { | 
 | 31 |  | 
 | 32 | class SharedBuffer | 
 | 33 | { | 
 | 34 | public: | 
 | 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 Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 51 |     static          void                    dealloc(const SharedBuffer* released); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 |  | 
 | 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 |  | 
 | 96 | private: | 
 | 97 |         inline SharedBuffer() { } | 
 | 98 |         inline ~SharedBuffer() { } | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 99 |         SharedBuffer(const SharedBuffer&); | 
 | 100 |         SharedBuffer& operator = (const SharedBuffer&); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 |   | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 102 |         // Must be sized to preserve correct alignment. | 
 | 103 |         mutable std::atomic<int32_t>        mRefs; | 
 | 104 |                 size_t                      mSize; | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 105 |                 uint32_t                    mReserved; | 
 | 106 | public: | 
 | 107 |         // mClientMetadata is reserved for client use.  It is initialized to 0 | 
 | 108 |         // and the clients can do whatever they want with it.  Note that this is | 
 | 109 |         // placed last so that it is adjcent to the buffer allocated. | 
 | 110 |                 uint32_t                    mClientMetadata; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | }; | 
 | 112 |  | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 113 | static_assert(sizeof(SharedBuffer) % 8 == 0 | 
 | 114 |         && (sizeof(size_t) > 4 || sizeof(SharedBuffer) == 16), | 
 | 115 |         "SharedBuffer has unexpected size"); | 
 | 116 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | // --------------------------------------------------------------------------- | 
 | 118 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | const void* SharedBuffer::data() const { | 
 | 120 |     return this + 1; | 
 | 121 | } | 
 | 122 |  | 
 | 123 | void* SharedBuffer::data() { | 
 | 124 |     return this + 1; | 
 | 125 | } | 
 | 126 |  | 
 | 127 | size_t SharedBuffer::size() const { | 
 | 128 |     return mSize; | 
 | 129 | } | 
 | 130 |  | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 131 | SharedBuffer* SharedBuffer::bufferFromData(void* data) { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 132 |     return data ? static_cast<SharedBuffer *>(data)-1 : nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } | 
 | 134 |      | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 135 | const SharedBuffer* SharedBuffer::bufferFromData(const void* data) { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 136 |     return data ? static_cast<const SharedBuffer *>(data)-1 : nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } | 
 | 138 |  | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 139 | size_t SharedBuffer::sizeFromData(const void* data) { | 
 | 140 |     return data ? bufferFromData(data)->mSize : 0; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } | 
 | 142 |  | 
 | 143 | bool SharedBuffer::onlyOwner() const { | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 144 |     return (mRefs.load(std::memory_order_acquire) == 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } | 
 | 146 |  | 
| Pirama Arumuga Nainar | 90affce | 2018-04-11 23:10:28 -0700 | [diff] [blame] | 147 | }  // namespace android | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 |  | 
 | 149 | // --------------------------------------------------------------------------- | 
 | 150 |  | 
 | 151 | #endif // ANDROID_VECTOR_H |