blob: c4ca0e0e579895900559facb626640d0d518d1be [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
17#include <stdlib.h>
18#include <string.h>
19
Sergio Giro7987b832015-08-18 17:36:50 +010020#include <log/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021#include <utils/SharedBuffer.h>
22#include <utils/Atomic.h>
23
24// ---------------------------------------------------------------------------
25
26namespace android {
27
28SharedBuffer* SharedBuffer::alloc(size_t size)
29{
Sergio Giro7987b832015-08-18 17:36:50 +010030 // Don't overflow if the combined size of the buffer / header is larger than
31 // size_max.
32 LOG_ALWAYS_FATAL_IF((size >= (SIZE_MAX - sizeof(SharedBuffer))),
33 "Invalid buffer size %zu", size);
34
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035 SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size));
36 if (sb) {
37 sb->mRefs = 1;
38 sb->mSize = size;
39 }
40 return sb;
41}
42
43
44ssize_t SharedBuffer::dealloc(const SharedBuffer* released)
45{
46 if (released->mRefs != 0) return -1; // XXX: invalid operation
47 free(const_cast<SharedBuffer*>(released));
48 return 0;
49}
50
51SharedBuffer* SharedBuffer::edit() const
52{
53 if (onlyOwner()) {
54 return const_cast<SharedBuffer*>(this);
55 }
56 SharedBuffer* sb = alloc(mSize);
57 if (sb) {
58 memcpy(sb->data(), data(), size());
59 release();
60 }
Sergio Giro7987b832015-08-18 17:36:50 +010061 return sb;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062}
63
64SharedBuffer* SharedBuffer::editResize(size_t newSize) const
65{
66 if (onlyOwner()) {
67 SharedBuffer* buf = const_cast<SharedBuffer*>(this);
68 if (buf->mSize == newSize) return buf;
Sergio Giro7987b832015-08-18 17:36:50 +010069 // Don't overflow if the combined size of the new buffer / header is larger than
70 // size_max.
71 LOG_ALWAYS_FATAL_IF((newSize >= (SIZE_MAX - sizeof(SharedBuffer))),
72 "Invalid buffer size %zu", newSize);
73
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize);
75 if (buf != NULL) {
76 buf->mSize = newSize;
77 return buf;
78 }
79 }
80 SharedBuffer* sb = alloc(newSize);
81 if (sb) {
82 const size_t mySize = mSize;
83 memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize);
84 release();
85 }
86 return sb;
87}
88
89SharedBuffer* SharedBuffer::attemptEdit() const
90{
91 if (onlyOwner()) {
92 return const_cast<SharedBuffer*>(this);
93 }
94 return 0;
95}
96
97SharedBuffer* SharedBuffer::reset(size_t new_size) const
98{
99 // cheap-o-reset.
100 SharedBuffer* sb = alloc(new_size);
101 if (sb) {
102 release();
103 }
104 return sb;
105}
106
107void SharedBuffer::acquire() const {
108 android_atomic_inc(&mRefs);
109}
110
111int32_t SharedBuffer::release(uint32_t flags) const
112{
113 int32_t prev = 1;
114 if (onlyOwner() || ((prev = android_atomic_dec(&mRefs)) == 1)) {
115 mRefs = 0;
116 if ((flags & eKeepStorage) == 0) {
117 free(const_cast<SharedBuffer*>(this));
118 }
119 }
120 return prev;
121}
122
123
124}; // namespace android