summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Giro2015-09-23 10:22:59 -0500
committerSergio Giro2015-09-23 10:22:59 -0500
commitd2529f28308ba7583f4d8baaa937218d81cbe8bd (patch)
tree08e7ec119fa22c98d69f649e7ffa05f544a7d9f0 /libutils
parent1d758c90959225c76ed4d318290ad1bf11532409 (diff)
downloadplatform-system-core-d2529f28308ba7583f4d8baaa937218d81cbe8bd.tar.gz
platform-system-core-d2529f28308ba7583f4d8baaa937218d81cbe8bd.tar.xz
platform-system-core-d2529f28308ba7583f4d8baaa937218d81cbe8bd.zip
libutils: hide SharedBuffer by moving SharedBuffer.h to the implementation directory
Some methods in header files of classes using SharedBuffer need to be moved to the implementation files accordingly Change-Id: I891f3ace2b940ab219e4e449040bfed71c0547db
Diffstat (limited to 'libutils')
-rw-r--r--libutils/BasicHashtable.cpp8
-rw-r--r--libutils/SharedBuffer.cpp3
-rw-r--r--libutils/SharedBuffer.h137
-rw-r--r--libutils/String16.cpp11
-rw-r--r--libutils/String8.cpp13
-rw-r--r--libutils/VectorImpl.cpp3
6 files changed, 172 insertions, 3 deletions
diff --git a/libutils/BasicHashtable.cpp b/libutils/BasicHashtable.cpp
index 491d9e98b..1e9f05333 100644
--- a/libutils/BasicHashtable.cpp
+++ b/libutils/BasicHashtable.cpp
@@ -22,6 +22,8 @@
22#include <utils/BasicHashtable.h> 22#include <utils/BasicHashtable.h>
23#include <utils/misc.h> 23#include <utils/misc.h>
24 24
25#include "SharedBuffer.h"
26
25namespace android { 27namespace android {
26 28
27BasicHashtableImpl::BasicHashtableImpl(size_t entrySize, bool hasTrivialDestructor, 29BasicHashtableImpl::BasicHashtableImpl(size_t entrySize, bool hasTrivialDestructor,
@@ -46,6 +48,12 @@ BasicHashtableImpl::~BasicHashtableImpl()
46{ 48{
47} 49}
48 50
51void BasicHashtableImpl::edit() {
52 if (mBuckets && !SharedBuffer::bufferFromData(mBuckets)->onlyOwner()) {
53 clone();
54 }
55}
56
49void BasicHashtableImpl::dispose() { 57void BasicHashtableImpl::dispose() {
50 if (mBuckets) { 58 if (mBuckets) {
51 releaseBuckets(mBuckets, mBucketCount); 59 releaseBuckets(mBuckets, mBucketCount);
diff --git a/libutils/SharedBuffer.cpp b/libutils/SharedBuffer.cpp
index 3555fb712..003e38697 100644
--- a/libutils/SharedBuffer.cpp
+++ b/libutils/SharedBuffer.cpp
@@ -17,9 +17,10 @@
17#include <stdlib.h> 17#include <stdlib.h>
18#include <string.h> 18#include <string.h>
19 19
20#include <utils/SharedBuffer.h>
21#include <utils/Atomic.h> 20#include <utils/Atomic.h>
22 21
22#include "SharedBuffer.h"
23
23// --------------------------------------------------------------------------- 24// ---------------------------------------------------------------------------
24 25
25namespace android { 26namespace android {
diff --git a/libutils/SharedBuffer.h b/libutils/SharedBuffer.h
new file mode 100644
index 000000000..b6709537e
--- /dev/null
+++ b/libutils/SharedBuffer.h
@@ -0,0 +1,137 @@
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#ifndef ANDROID_SHARED_BUFFER_H
18#define ANDROID_SHARED_BUFFER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23// ---------------------------------------------------------------------------
24
25namespace android {
26
27class SharedBuffer
28{
29public:
30
31 /* flags to use with release() */
32 enum {
33 eKeepStorage = 0x00000001
34 };
35
36 /*! allocate a buffer of size 'size' and acquire() it.
37 * call release() to free it.
38 */
39 static SharedBuffer* alloc(size_t size);
40
41 /*! free the memory associated with the SharedBuffer.
42 * Fails if there are any users associated with this SharedBuffer.
43 * In other words, the buffer must have been release by all its
44 * users.
45 */
46 static ssize_t dealloc(const SharedBuffer* released);
47
48 //! access the data for read
49 inline const void* data() const;
50
51 //! access the data for read/write
52 inline void* data();
53
54 //! get size of the buffer
55 inline size_t size() const;
56
57 //! get back a SharedBuffer object from its data
58 static inline SharedBuffer* bufferFromData(void* data);
59
60 //! get back a SharedBuffer object from its data
61 static inline const SharedBuffer* bufferFromData(const void* data);
62
63 //! get the size of a SharedBuffer object from its data
64 static inline size_t sizeFromData(const void* data);
65
66 //! edit the buffer (get a writtable, or non-const, version of it)
67 SharedBuffer* edit() const;
68
69 //! edit the buffer, resizing if needed
70 SharedBuffer* editResize(size_t size) const;
71
72 //! like edit() but fails if a copy is required
73 SharedBuffer* attemptEdit() const;
74
75 //! resize and edit the buffer, loose it's content.
76 SharedBuffer* reset(size_t size) const;
77
78 //! acquire/release a reference on this buffer
79 void acquire() const;
80
81 /*! release a reference on this buffer, with the option of not
82 * freeing the memory associated with it if it was the last reference
83 * returns the previous reference count
84 */
85 int32_t release(uint32_t flags = 0) const;
86
87 //! returns wether or not we're the only owner
88 inline bool onlyOwner() const;
89
90
91private:
92 inline SharedBuffer() { }
93 inline ~SharedBuffer() { }
94 SharedBuffer(const SharedBuffer&);
95 SharedBuffer& operator = (const SharedBuffer&);
96
97 // 16 bytes. must be sized to preserve correct alignment.
98 mutable int32_t mRefs;
99 size_t mSize;
100 uint32_t mReserved[2];
101};
102
103// ---------------------------------------------------------------------------
104
105const void* SharedBuffer::data() const {
106 return this + 1;
107}
108
109void* SharedBuffer::data() {
110 return this + 1;
111}
112
113size_t SharedBuffer::size() const {
114 return mSize;
115}
116
117SharedBuffer* SharedBuffer::bufferFromData(void* data) {
118 return data ? static_cast<SharedBuffer *>(data)-1 : 0;
119}
120
121const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
122 return data ? static_cast<const SharedBuffer *>(data)-1 : 0;
123}
124
125size_t SharedBuffer::sizeFromData(const void* data) {
126 return data ? bufferFromData(data)->mSize : 0;
127}
128
129bool SharedBuffer::onlyOwner() const {
130 return (mRefs == 1);
131}
132
133}; // namespace android
134
135// ---------------------------------------------------------------------------
136
137#endif // ANDROID_VECTOR_H
diff --git a/libutils/String16.cpp b/libutils/String16.cpp
index 91efdaa39..67be9d8fa 100644
--- a/libutils/String16.cpp
+++ b/libutils/String16.cpp
@@ -25,6 +25,7 @@
25#include <stdio.h> 25#include <stdio.h>
26#include <ctype.h> 26#include <ctype.h>
27 27
28#include "SharedBuffer.h"
28 29
29namespace android { 30namespace android {
30 31
@@ -165,6 +166,16 @@ String16::~String16()
165 SharedBuffer::bufferFromData(mString)->release(); 166 SharedBuffer::bufferFromData(mString)->release();
166} 167}
167 168
169size_t String16::size() const
170{
171 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
172}
173
174const SharedBuffer* String16::sharedBuffer() const
175{
176 return SharedBuffer::bufferFromData(mString);
177}
178
168void String16::setTo(const String16& other) 179void String16::setTo(const String16& other)
169{ 180{
170 SharedBuffer::bufferFromData(other.mString)->acquire(); 181 SharedBuffer::bufferFromData(other.mString)->acquire();
diff --git a/libutils/String8.cpp b/libutils/String8.cpp
index 69313ead7..5e8552049 100644
--- a/libutils/String8.cpp
+++ b/libutils/String8.cpp
@@ -19,12 +19,13 @@
19#include <utils/Compat.h> 19#include <utils/Compat.h>
20#include <utils/Log.h> 20#include <utils/Log.h>
21#include <utils/Unicode.h> 21#include <utils/Unicode.h>
22#include <utils/SharedBuffer.h>
23#include <utils/String16.h> 22#include <utils/String16.h>
24#include <utils/threads.h> 23#include <utils/threads.h>
25 24
26#include <ctype.h> 25#include <ctype.h>
27 26
27#include "SharedBuffer.h"
28
28/* 29/*
29 * Functions outside android is below the namespace android, since they use 30 * Functions outside android is below the namespace android, since they use
30 * functions and constants in android namespace. 31 * functions and constants in android namespace.
@@ -211,6 +212,16 @@ String8::~String8()
211 SharedBuffer::bufferFromData(mString)->release(); 212 SharedBuffer::bufferFromData(mString)->release();
212} 213}
213 214
215size_t String8::length() const
216{
217 return SharedBuffer::sizeFromData(mString)-1;
218}
219
220const SharedBuffer* String8::sharedBuffer() const
221{
222 return SharedBuffer::bufferFromData(mString);
223}
224
214String8 String8::format(const char* fmt, ...) 225String8 String8::format(const char* fmt, ...)
215{ 226{
216 va_list args; 227 va_list args;
diff --git a/libutils/VectorImpl.cpp b/libutils/VectorImpl.cpp
index 2f770f590..2ac158b9f 100644
--- a/libutils/VectorImpl.cpp
+++ b/libutils/VectorImpl.cpp
@@ -23,9 +23,10 @@
23#include <cutils/log.h> 23#include <cutils/log.h>
24 24
25#include <utils/Errors.h> 25#include <utils/Errors.h>
26#include <utils/SharedBuffer.h>
27#include <utils/VectorImpl.h> 26#include <utils/VectorImpl.h>
28 27
28#include "SharedBuffer.h"
29
29/*****************************************************************************/ 30/*****************************************************************************/
30 31
31 32