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 
17 #define LOG_TAG "sharedbuffer"
18 
19 #include "SharedBuffer.h"
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <log/log.h>
25 
26 // ---------------------------------------------------------------------------
27 
28 namespace android {
29 
alloc(size_t size)30 SharedBuffer* SharedBuffer::alloc(size_t size)
31 {
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 
37     SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size));
38     if (sb) {
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);
43         sb->mSize = size;
44         sb->mClientMetadata = 0;
45     }
46     return sb;
47 }
48 
49 
dealloc(const SharedBuffer * released)50 void SharedBuffer::dealloc(const SharedBuffer* released)
51 {
52     free(const_cast<SharedBuffer*>(released));
53 }
54 
edit() const55 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     }
65     return sb;
66 }
67 
editResize(size_t newSize) const68 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;
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 
78         buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize);
79         if (buf != nullptr) {
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 
attemptEdit() const93 SharedBuffer* SharedBuffer::attemptEdit() const
94 {
95     if (onlyOwner()) {
96         return const_cast<SharedBuffer*>(this);
97     }
98     return nullptr;
99 }
100 
reset(size_t new_size) const101 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 
acquire() const111 void SharedBuffer::acquire() const {
112     mRefs.fetch_add(1, std::memory_order_relaxed);
113 }
114 
release(uint32_t flags) const115 int32_t SharedBuffer::release(uint32_t flags) const
116 {
117     const bool useDealloc = ((flags & eKeepStorage) == 0);
118     if (onlyOwner()) {
119         // Since we're the only owner, our reference count goes to zero.
120         mRefs.store(0, std::memory_order_relaxed);
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);
134         }
135     }
136     return prevRefCount;
137 }
138 
139 
140 }; // namespace android
141