blob: d16bdb08670c5916ba628af99851869e4d2a162e [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Salyzyncfd5b082016-10-17 14:28:00 -070017#define LOG_TAG "sharedbuffer"
18
Mathias Agopian22dbf392017-02-28 15:06:51 -080019#include "SharedBuffer.h"
20
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021#include <stdlib.h>
22#include <string.h>
23
Mark Salyzyn30f991f2017-01-10 13:19:54 -080024#include <log/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025
26// ---------------------------------------------------------------------------
27
28namespace android {
29
30SharedBuffer* SharedBuffer::alloc(size_t size)
31{
Sergio Giro7987b832015-08-18 17:36:50 +010032 // 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 Projectcbb10112009-03-03 19:31:44 -080037 SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size));
38 if (sb) {
Hans Boehm3e4c0762016-05-18 10:09:24 -070039 // 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 Projectcbb10112009-03-03 19:31:44 -080043 sb->mSize = size;
Vic Yang9fb93ed2019-09-05 13:18:27 -070044 sb->mClientMetadata = 0;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080045 }
46 return sb;
47}
48
49
Hans Boehm3e4c0762016-05-18 10:09:24 -070050void SharedBuffer::dealloc(const SharedBuffer* released)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052 free(const_cast<SharedBuffer*>(released));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053}
54
55SharedBuffer* 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 Giro7987b832015-08-18 17:36:50 +010065 return sb;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080066}
67
68SharedBuffer* 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 Giro7987b832015-08-18 17:36:50 +010073 // 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
Tomasz Wasilczyk0a2a0152024-04-30 10:47:35 -070078 buf = (SharedBuffer*)realloc(reinterpret_cast<void*>(buf), sizeof(SharedBuffer) + newSize);
Yi Konge1731a42018-07-16 18:11:34 -070079 if (buf != nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080080 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
93SharedBuffer* SharedBuffer::attemptEdit() const
94{
95 if (onlyOwner()) {
96 return const_cast<SharedBuffer*>(this);
97 }
Yi Konge1731a42018-07-16 18:11:34 -070098 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080099}
100
101SharedBuffer* 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
111void SharedBuffer::acquire() const {
Hans Boehm3e4c0762016-05-18 10:09:24 -0700112 mRefs.fetch_add(1, std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800113}
114
115int32_t SharedBuffer::release(uint32_t flags) const
116{
Greg Kaiser476dbc42016-08-01 14:40:25 -0700117 const bool useDealloc = ((flags & eKeepStorage) == 0);
118 if (onlyOwner()) {
119 // Since we're the only owner, our reference count goes to zero.
Hans Boehm3e4c0762016-05-18 10:09:24 -0700120 mRefs.store(0, std::memory_order_relaxed);
Greg Kaiser476dbc42016-08-01 14:40:25 -0700121 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 Projectcbb10112009-03-03 19:31:44 -0800134 }
135 }
Greg Kaiser476dbc42016-08-01 14:40:25 -0700136 return prevRefCount;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800137}
138
139
140}; // namespace android