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 | #include <utils/Atomic.h> |
| 24 | |
Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 25 | #include "SharedBuffer.h" |
| 26 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | // --------------------------------------------------------------------------- |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | SharedBuffer* SharedBuffer::alloc(size_t size) |
| 32 | { |
Sergio Giro | d95e47f | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 33 | // Don't overflow if the combined size of the buffer / header is larger than |
| 34 | // size_max. |
| 35 | LOG_ALWAYS_FATAL_IF((size >= (SIZE_MAX - sizeof(SharedBuffer))), |
| 36 | "Invalid buffer size %zu", size); |
| 37 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size)); |
| 39 | if (sb) { |
| 40 | sb->mRefs = 1; |
| 41 | sb->mSize = size; |
| 42 | } |
| 43 | return sb; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | ssize_t SharedBuffer::dealloc(const SharedBuffer* released) |
| 48 | { |
| 49 | if (released->mRefs != 0) return -1; // XXX: invalid operation |
| 50 | free(const_cast<SharedBuffer*>(released)); |
| 51 | return 0; |
| 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 { |
| 111 | android_atomic_inc(&mRefs); |
| 112 | } |
| 113 | |
| 114 | int32_t SharedBuffer::release(uint32_t flags) const |
| 115 | { |
| 116 | int32_t prev = 1; |
| 117 | if (onlyOwner() || ((prev = android_atomic_dec(&mRefs)) == 1)) { |
| 118 | mRefs = 0; |
| 119 | if ((flags & eKeepStorage) == 0) { |
| 120 | free(const_cast<SharedBuffer*>(this)); |
| 121 | } |
| 122 | } |
| 123 | return prev; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | }; // namespace android |