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 #define LOG_TAG "sharedbuffer"
17 #include "SharedBuffer.h"
18 #include <stdlib.h>
19 #include <string.h>
20 #include <log/log.h>
21 // ---------------------------------------------------------------------------
22 namespace android {
alloc(size_t size)23 SharedBuffer* SharedBuffer::alloc(size_t size)
24 {
25 // Don't overflow if the combined size of the buffer / header is larger than
26 // size_max.
27 LOG_ALWAYS_FATAL_IF((size >= (SIZE_MAX - sizeof(SharedBuffer))),
28 "Invalid buffer size %zu", size);
29 SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size));
30 if (sb) {
31 // Should be std::atomic_init(&sb->mRefs, 1);
32 // But that generates a warning with some compilers.
33 // The following is OK on Android-supported platforms.
34 sb->mRefs.store(1, std::memory_order_relaxed);
35 sb->mSize = size;
36 }
37 return sb;
38 }
dealloc(const SharedBuffer * released)39 void SharedBuffer::dealloc(const SharedBuffer* released)
40 {
41 free(const_cast<SharedBuffer*>(released));
42 }
edit() const43 SharedBuffer* SharedBuffer::edit() const
44 {
45 if (onlyOwner()) {
46 return const_cast<SharedBuffer*>(this);
47 }
48 SharedBuffer* sb = alloc(mSize);
49 if (sb) {
50 memcpy(sb->data(), data(), size());
51 release();
52 }
53 return sb;
54 }
editResize(size_t newSize) const55 SharedBuffer* SharedBuffer::editResize(size_t newSize) const
56 {
57 if (onlyOwner()) {
58 SharedBuffer* buf = const_cast<SharedBuffer*>(this);
59 if (buf->mSize == newSize) return buf;
60 // Don't overflow if the combined size of the new buffer / header is larger than
61 // size_max.
62 LOG_ALWAYS_FATAL_IF((newSize >= (SIZE_MAX - sizeof(SharedBuffer))),
63 "Invalid buffer size %zu", newSize);
64 buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize);
65 if (buf != nullptr) {
66 buf->mSize = newSize;
67 return buf;
68 }
69 }
70 SharedBuffer* sb = alloc(newSize);
71 if (sb) {
72 const size_t mySize = mSize;
73 memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize);
74 release();
75 }
76 return sb;
77 }
attemptEdit() const78 SharedBuffer* SharedBuffer::attemptEdit() const
79 {
80 if (onlyOwner()) {
81 return const_cast<SharedBuffer*>(this);
82 }
83 return nullptr;
84 }
reset(size_t new_size) const85 SharedBuffer* SharedBuffer::reset(size_t new_size) const
86 {
87 // cheap-o-reset.
88 SharedBuffer* sb = alloc(new_size);
89 if (sb) {
90 release();
91 }
92 return sb;
93 }
acquire() const94 void SharedBuffer::acquire() const {
95 mRefs.fetch_add(1, std::memory_order_relaxed);
96 }
release(uint32_t flags) const97 int32_t SharedBuffer::release(uint32_t flags) const
98 {
99 const bool useDealloc = ((flags & eKeepStorage) == 0);
100 if (onlyOwner()) {
101 // Since we're the only owner, our reference count goes to zero.
102 mRefs.store(0, std::memory_order_relaxed);
103 if (useDealloc) {
104 dealloc(this);
105 }
106 // As the only owner, our previous reference count was 1.
107 return 1;
108 }
109 // There's multiple owners, we need to use an atomic decrement.
110 int32_t prevRefCount = mRefs.fetch_sub(1, std::memory_order_release);
111 if (prevRefCount == 1) {
112 // We're the last reference, we need the acquire fence.
113 atomic_thread_fence(std::memory_order_acquire);
114 if (useDealloc) {
115 dealloc(this);
116 }
117 }
118 return prevRefCount;
119 }
120 }; // namespace android
121
122