summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Giro2015-09-23 12:15:12 -0500
committerAndroid Git Automerger2015-09-23 12:15:12 -0500
commitc565bdb67b801fe2b4965b44e53dccfd9097993c (patch)
treefae97afa559ac30497bf7b5d1df2009236f8dde7 /libutils
parentec22417993d2a9a98ee957ffc7cd4af95debb546 (diff)
parentf777655d85af3aefa4a1683f289da1ec67988330 (diff)
downloadplatform-system-core-c565bdb67b801fe2b4965b44e53dccfd9097993c.tar.gz
platform-system-core-c565bdb67b801fe2b4965b44e53dccfd9097993c.tar.xz
platform-system-core-c565bdb67b801fe2b4965b44e53dccfd9097993c.zip
am f777655d: resolved conflicts for 6ee734cf to stage-aosp-master
* commit 'f777655d85af3aefa4a1683f289da1ec67988330': libutils: hide SharedBuffer by moving SharedBuffer.h to the implementation directory
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 947551a6a..c7dd1ab34 100644
--- a/libutils/SharedBuffer.cpp
+++ b/libutils/SharedBuffer.cpp
@@ -20,9 +20,10 @@
20#include <string.h> 20#include <string.h>
21 21
22#include <log/log.h> 22#include <log/log.h>
23#include <utils/SharedBuffer.h>
24#include <utils/Atomic.h> 23#include <utils/Atomic.h>
25 24
25#include "SharedBuffer.h"
26
26// --------------------------------------------------------------------------- 27// ---------------------------------------------------------------------------
27 28
28namespace android { 29namespace 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 2d0602357..4fca2c119 100644
--- a/libutils/String8.cpp
+++ b/libutils/String8.cpp
@@ -22,12 +22,13 @@
22#include <utils/Compat.h> 22#include <utils/Compat.h>
23#include <utils/Log.h> 23#include <utils/Log.h>
24#include <utils/Unicode.h> 24#include <utils/Unicode.h>
25#include <utils/SharedBuffer.h>
26#include <utils/String16.h> 25#include <utils/String16.h>
27#include <utils/threads.h> 26#include <utils/threads.h>
28 27
29#include <ctype.h> 28#include <ctype.h>
30 29
30#include "SharedBuffer.h"
31
31/* 32/*
32 * Functions outside android is below the namespace android, since they use 33 * Functions outside android is below the namespace android, since they use
33 * functions and constants in android namespace. 34 * functions and constants in android namespace.
@@ -214,6 +215,16 @@ String8::~String8()
214 SharedBuffer::bufferFromData(mString)->release(); 215 SharedBuffer::bufferFromData(mString)->release();
215} 216}
216 217
218size_t String8::length() const
219{
220 return SharedBuffer::sizeFromData(mString)-1;
221}
222
223const SharedBuffer* String8::sharedBuffer() const
224{
225 return SharedBuffer::bufferFromData(mString);
226}
227
217String8 String8::format(const char* fmt, ...) 228String8 String8::format(const char* fmt, ...)
218{ 229{
219 va_list args; 230 va_list args;
diff --git a/libutils/VectorImpl.cpp b/libutils/VectorImpl.cpp
index 7c5347299..e8d40ed20 100644
--- a/libutils/VectorImpl.cpp
+++ b/libutils/VectorImpl.cpp
@@ -24,9 +24,10 @@
24#include <safe_iop.h> 24#include <safe_iop.h>
25 25
26#include <utils/Errors.h> 26#include <utils/Errors.h>
27#include <utils/SharedBuffer.h>
28#include <utils/VectorImpl.h> 27#include <utils/VectorImpl.h>
29 28
29#include "SharedBuffer.h"
30
30/*****************************************************************************/ 31/*****************************************************************************/
31 32
32 33