blob: c7dd1ab3446f495973b45305acee55b3728a86f4 [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
Sergio Girod95e47f2015-08-18 17:36:50 +010017#define __STDC_LIMIT_MACROS
18#include <stdint.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019#include <stdlib.h>
20#include <string.h>
21
Sergio Girod95e47f2015-08-18 17:36:50 +010022#include <log/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <utils/Atomic.h>
24
Sergio Girod2529f22015-09-23 16:22:59 +010025#include "SharedBuffer.h"
26
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027// ---------------------------------------------------------------------------
28
29namespace android {
30
31SharedBuffer* SharedBuffer::alloc(size_t size)
32{
Sergio Girod95e47f2015-08-18 17:36:50 +010033 // 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 Projectcbb10112009-03-03 19:31:44 -080038 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
47ssize_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
54SharedBuffer* 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 Girod95e47f2015-08-18 17:36:50 +010064 return sb;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065}
66
67SharedBuffer* 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 Girod95e47f2015-08-18 17:36:50 +010072 // 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 Projectcbb10112009-03-03 19:31:44 -080077 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
92SharedBuffer* SharedBuffer::attemptEdit() const
93{
94 if (onlyOwner()) {
95 return const_cast<SharedBuffer*>(this);
96 }
97 return 0;
98}
99
100SharedBuffer* 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
110void SharedBuffer::acquire() const {
111 android_atomic_inc(&mRefs);
112}
113
114int32_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