| 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 |  | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 17 | #define LOG_TAG "sharedbuffer" | 
 | 18 |  | 
| Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 19 | #include "SharedBuffer.h" | 
 | 20 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <stdlib.h> | 
 | 22 | #include <string.h> | 
 | 23 |  | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 24 | #include <log/log.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 |  | 
 | 26 | // --------------------------------------------------------------------------- | 
 | 27 |  | 
 | 28 | namespace android { | 
 | 29 |  | 
 | 30 | SharedBuffer* SharedBuffer::alloc(size_t size) | 
 | 31 | { | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 32 |     // Don't overflow if the combined size of the buffer / header is larger than | 
 | 33 |     // size_max. | 
 | 34 |     LOG_ALWAYS_FATAL_IF((size >= (SIZE_MAX - sizeof(SharedBuffer))), | 
 | 35 |                         "Invalid buffer size %zu", size); | 
 | 36 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 |     SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size)); | 
 | 38 |     if (sb) { | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 39 |         // Should be std::atomic_init(&sb->mRefs, 1); | 
 | 40 |         // But that generates a warning with some compilers. | 
 | 41 |         // The following is OK on Android-supported platforms. | 
 | 42 |         sb->mRefs.store(1, std::memory_order_relaxed); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 |         sb->mSize = size; | 
| Vic Yang | 9fb93ed | 2019-09-05 13:18:27 -0700 | [diff] [blame] | 44 |         sb->mClientMetadata = 0; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 |     } | 
 | 46 |     return sb; | 
 | 47 | } | 
 | 48 |  | 
 | 49 |  | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 50 | void SharedBuffer::dealloc(const SharedBuffer* released) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 |     free(const_cast<SharedBuffer*>(released)); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } | 
 | 54 |  | 
 | 55 | SharedBuffer* SharedBuffer::edit() const | 
 | 56 | { | 
 | 57 |     if (onlyOwner()) { | 
 | 58 |         return const_cast<SharedBuffer*>(this); | 
 | 59 |     } | 
 | 60 |     SharedBuffer* sb = alloc(mSize); | 
 | 61 |     if (sb) { | 
 | 62 |         memcpy(sb->data(), data(), size()); | 
 | 63 |         release(); | 
 | 64 |     } | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 65 |     return sb; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } | 
 | 67 |  | 
 | 68 | SharedBuffer* SharedBuffer::editResize(size_t newSize) const | 
 | 69 | { | 
 | 70 |     if (onlyOwner()) { | 
 | 71 |         SharedBuffer* buf = const_cast<SharedBuffer*>(this); | 
 | 72 |         if (buf->mSize == newSize) return buf; | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 73 |         // Don't overflow if the combined size of the new buffer / header is larger than | 
 | 74 |         // size_max. | 
 | 75 |         LOG_ALWAYS_FATAL_IF((newSize >= (SIZE_MAX - sizeof(SharedBuffer))), | 
 | 76 |                             "Invalid buffer size %zu", newSize); | 
 | 77 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 |         buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 79 |         if (buf != nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 |             buf->mSize = newSize; | 
 | 81 |             return buf; | 
 | 82 |         } | 
 | 83 |     } | 
 | 84 |     SharedBuffer* sb = alloc(newSize); | 
 | 85 |     if (sb) { | 
 | 86 |         const size_t mySize = mSize; | 
 | 87 |         memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize); | 
 | 88 |         release(); | 
 | 89 |     } | 
 | 90 |     return sb;     | 
 | 91 | } | 
 | 92 |  | 
 | 93 | SharedBuffer* SharedBuffer::attemptEdit() const | 
 | 94 | { | 
 | 95 |     if (onlyOwner()) { | 
 | 96 |         return const_cast<SharedBuffer*>(this); | 
 | 97 |     } | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 98 |     return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | } | 
 | 100 |  | 
 | 101 | SharedBuffer* SharedBuffer::reset(size_t new_size) const | 
 | 102 | { | 
 | 103 |     // cheap-o-reset. | 
 | 104 |     SharedBuffer* sb = alloc(new_size); | 
 | 105 |     if (sb) { | 
 | 106 |         release(); | 
 | 107 |     } | 
 | 108 |     return sb; | 
 | 109 | } | 
 | 110 |  | 
 | 111 | void SharedBuffer::acquire() const { | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 112 |     mRefs.fetch_add(1, std::memory_order_relaxed); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } | 
 | 114 |  | 
 | 115 | int32_t SharedBuffer::release(uint32_t flags) const | 
 | 116 | { | 
| Greg Kaiser | 476dbc4 | 2016-08-01 14:40:25 -0700 | [diff] [blame] | 117 |     const bool useDealloc = ((flags & eKeepStorage) == 0); | 
 | 118 |     if (onlyOwner()) { | 
 | 119 |         // Since we're the only owner, our reference count goes to zero. | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 120 |         mRefs.store(0, std::memory_order_relaxed); | 
| Greg Kaiser | 476dbc4 | 2016-08-01 14:40:25 -0700 | [diff] [blame] | 121 |         if (useDealloc) { | 
 | 122 |             dealloc(this); | 
 | 123 |         } | 
 | 124 |         // As the only owner, our previous reference count was 1. | 
 | 125 |         return 1; | 
 | 126 |     } | 
 | 127 |     // There's multiple owners, we need to use an atomic decrement. | 
 | 128 |     int32_t prevRefCount = mRefs.fetch_sub(1, std::memory_order_release); | 
 | 129 |     if (prevRefCount == 1) { | 
 | 130 |         // We're the last reference, we need the acquire fence. | 
 | 131 |         atomic_thread_fence(std::memory_order_acquire); | 
 | 132 |         if (useDealloc) { | 
 | 133 |             dealloc(this); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 |         } | 
 | 135 |     } | 
| Greg Kaiser | 476dbc4 | 2016-08-01 14:40:25 -0700 | [diff] [blame] | 136 |     return prevRefCount; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } | 
 | 138 |  | 
 | 139 |  | 
 | 140 | }; // namespace android |