| 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 |  | 
| Sergio Giro | d95e47f | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 17 | #define __STDC_LIMIT_MACROS | 
 | 18 | #include <stdint.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdlib.h> | 
 | 20 | #include <string.h> | 
 | 21 |  | 
| Sergio Giro | d95e47f | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 22 | #include <log/log.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 |  | 
| Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 24 | #include "SharedBuffer.h" | 
 | 25 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | // --------------------------------------------------------------------------- | 
 | 27 |  | 
 | 28 | namespace android { | 
 | 29 |  | 
 | 30 | SharedBuffer* SharedBuffer::alloc(size_t size) | 
 | 31 | { | 
| Sergio Giro | d95e47f | 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; | 
 | 44 |     } | 
 | 45 |     return sb; | 
 | 46 | } | 
 | 47 |  | 
 | 48 |  | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 49 | void SharedBuffer::dealloc(const SharedBuffer* released) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 |     free(const_cast<SharedBuffer*>(released)); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } | 
 | 53 |  | 
 | 54 | SharedBuffer* SharedBuffer::edit() const | 
 | 55 | { | 
 | 56 |     if (onlyOwner()) { | 
 | 57 |         return const_cast<SharedBuffer*>(this); | 
 | 58 |     } | 
 | 59 |     SharedBuffer* sb = alloc(mSize); | 
 | 60 |     if (sb) { | 
 | 61 |         memcpy(sb->data(), data(), size()); | 
 | 62 |         release(); | 
 | 63 |     } | 
| Sergio Giro | d95e47f | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 64 |     return sb; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } | 
 | 66 |  | 
 | 67 | SharedBuffer* SharedBuffer::editResize(size_t newSize) const | 
 | 68 | { | 
 | 69 |     if (onlyOwner()) { | 
 | 70 |         SharedBuffer* buf = const_cast<SharedBuffer*>(this); | 
 | 71 |         if (buf->mSize == newSize) return buf; | 
| Sergio Giro | d95e47f | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 72 |         // Don't overflow if the combined size of the new buffer / header is larger than | 
 | 73 |         // size_max. | 
 | 74 |         LOG_ALWAYS_FATAL_IF((newSize >= (SIZE_MAX - sizeof(SharedBuffer))), | 
 | 75 |                             "Invalid buffer size %zu", newSize); | 
 | 76 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 |         buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize); | 
 | 78 |         if (buf != NULL) { | 
 | 79 |             buf->mSize = newSize; | 
 | 80 |             return buf; | 
 | 81 |         } | 
 | 82 |     } | 
 | 83 |     SharedBuffer* sb = alloc(newSize); | 
 | 84 |     if (sb) { | 
 | 85 |         const size_t mySize = mSize; | 
 | 86 |         memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize); | 
 | 87 |         release(); | 
 | 88 |     } | 
 | 89 |     return sb;     | 
 | 90 | } | 
 | 91 |  | 
 | 92 | SharedBuffer* SharedBuffer::attemptEdit() const | 
 | 93 | { | 
 | 94 |     if (onlyOwner()) { | 
 | 95 |         return const_cast<SharedBuffer*>(this); | 
 | 96 |     } | 
 | 97 |     return 0; | 
 | 98 | } | 
 | 99 |  | 
 | 100 | SharedBuffer* SharedBuffer::reset(size_t new_size) const | 
 | 101 | { | 
 | 102 |     // cheap-o-reset. | 
 | 103 |     SharedBuffer* sb = alloc(new_size); | 
 | 104 |     if (sb) { | 
 | 105 |         release(); | 
 | 106 |     } | 
 | 107 |     return sb; | 
 | 108 | } | 
 | 109 |  | 
 | 110 | void SharedBuffer::acquire() const { | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 111 |     mRefs.fetch_add(1, std::memory_order_relaxed); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } | 
 | 113 |  | 
 | 114 | int32_t SharedBuffer::release(uint32_t flags) const | 
 | 115 | { | 
 | 116 |     int32_t prev = 1; | 
| Hans Boehm | 3e4c076 | 2016-05-18 10:09:24 -0700 | [diff] [blame] | 117 |     if (onlyOwner() || ((prev = mRefs.fetch_sub(1, std::memory_order_release) == 1) | 
 | 118 |             && (atomic_thread_fence(std::memory_order_acquire), true))) { | 
 | 119 |         mRefs.store(0, std::memory_order_relaxed); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 |         if ((flags & eKeepStorage) == 0) { | 
 | 121 |             free(const_cast<SharedBuffer*>(this)); | 
 | 122 |         } | 
 | 123 |     } | 
 | 124 |     return prev; | 
 | 125 | } | 
 | 126 |  | 
 | 127 |  | 
 | 128 | }; // namespace android |