• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 __STDC_LIMIT_MACROS
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <log/log.h>
23 #include <utils/SharedBuffer.h>
24 #include <utils/Atomic.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         sb->mRefs = 1;
40         sb->mSize = size;
41     }
42     return sb;
43 }
44 
45 
dealloc(const SharedBuffer * released)46 ssize_t SharedBuffer::dealloc(const SharedBuffer* released)
47 {
48     if (released->mRefs != 0) return -1; // XXX: invalid operation
49     free(const_cast<SharedBuffer*>(released));
50     return 0;
51 }
52 
edit() const53 SharedBuffer* SharedBuffer::edit() const
54 {
55     if (onlyOwner()) {
56         return const_cast<SharedBuffer*>(this);
57     }
58     SharedBuffer* sb = alloc(mSize);
59     if (sb) {
60         memcpy(sb->data(), data(), size());
61         release();
62     }
63     return sb;
64 }
65 
editResize(size_t newSize) const66 SharedBuffer* SharedBuffer::editResize(size_t newSize) const
67 {
68     if (onlyOwner()) {
69         SharedBuffer* buf = const_cast<SharedBuffer*>(this);
70         if (buf->mSize == newSize) return buf;
71         // Don't overflow if the combined size of the new buffer / header is larger than
72         // size_max.
73         LOG_ALWAYS_FATAL_IF((newSize >= (SIZE_MAX - sizeof(SharedBuffer))),
74                             "Invalid buffer size %zu", newSize);
75 
76         buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize);
77         if (buf != NULL) {
78             buf->mSize = newSize;
79             return buf;
80         }
81     }
82     SharedBuffer* sb = alloc(newSize);
83     if (sb) {
84         const size_t mySize = mSize;
85         memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize);
86         release();
87     }
88     return sb;
89 }
90 
attemptEdit() const91 SharedBuffer* SharedBuffer::attemptEdit() const
92 {
93     if (onlyOwner()) {
94         return const_cast<SharedBuffer*>(this);
95     }
96     return 0;
97 }
98 
reset(size_t new_size) const99 SharedBuffer* SharedBuffer::reset(size_t new_size) const
100 {
101     // cheap-o-reset.
102     SharedBuffer* sb = alloc(new_size);
103     if (sb) {
104         release();
105     }
106     return sb;
107 }
108 
acquire() const109 void SharedBuffer::acquire() const {
110     android_atomic_inc(&mRefs);
111 }
112 
release(uint32_t flags) const113 int32_t SharedBuffer::release(uint32_t flags) const
114 {
115     int32_t prev = 1;
116     if (onlyOwner() || ((prev = android_atomic_dec(&mRefs)) == 1)) {
117         mRefs = 0;
118         if ((flags & eKeepStorage) == 0) {
119             free(const_cast<SharedBuffer*>(this));
120         }
121     }
122     return prev;
123 }
124 
125 
126 }; // namespace android
127