minor SharedBuffer clean-up

Change-Id: If38b7ce85806ae628c00f2c938de4e3f75142543
This commit is contained in:
Mathias Agopian 2012-08-31 16:20:23 -07:00
parent 2401ead700
commit 50f262fe56
2 changed files with 15 additions and 24 deletions

View File

@ -44,9 +44,6 @@ public:
* users. * users.
*/ */
static ssize_t dealloc(const SharedBuffer* released); static ssize_t dealloc(const SharedBuffer* released);
//! get the SharedBuffer from the data pointer
static inline const SharedBuffer* sharedBuffer(const void* data);
//! access the data for read //! access the data for read
inline const void* data() const; inline const void* data() const;
@ -94,7 +91,8 @@ public:
private: private:
inline SharedBuffer() { } inline SharedBuffer() { }
inline ~SharedBuffer() { } inline ~SharedBuffer() { }
inline SharedBuffer(const SharedBuffer&); SharedBuffer(const SharedBuffer&);
SharedBuffer& operator = (const SharedBuffer&);
// 16 bytes. must be sized to preserve correct alignment. // 16 bytes. must be sized to preserve correct alignment.
mutable int32_t mRefs; mutable int32_t mRefs;
@ -104,10 +102,6 @@ private:
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) {
return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0;
}
const void* SharedBuffer::data() const { const void* SharedBuffer::data() const {
return this + 1; return this + 1;
} }
@ -120,19 +114,16 @@ size_t SharedBuffer::size() const {
return mSize; return mSize;
} }
SharedBuffer* SharedBuffer::bufferFromData(void* data) SharedBuffer* SharedBuffer::bufferFromData(void* data) {
{ return data ? static_cast<SharedBuffer *>(data)-1 : 0;
return ((SharedBuffer*)data)-1;
} }
const SharedBuffer* SharedBuffer::bufferFromData(const void* data) const SharedBuffer* SharedBuffer::bufferFromData(const void* data) {
{ return data ? static_cast<const SharedBuffer *>(data)-1 : 0;
return ((const SharedBuffer*)data)-1;
} }
size_t SharedBuffer::sizeFromData(const void* data) size_t SharedBuffer::sizeFromData(const void* data) {
{ return data ? bufferFromData(data)->mSize : 0;
return (((const SharedBuffer*)data)-1)->mSize;
} }
bool SharedBuffer::onlyOwner() const { bool SharedBuffer::onlyOwner() const {

View File

@ -51,7 +51,7 @@ VectorImpl::VectorImpl(const VectorImpl& rhs)
mFlags(rhs.mFlags), mItemSize(rhs.mItemSize) mFlags(rhs.mFlags), mItemSize(rhs.mItemSize)
{ {
if (mStorage) { if (mStorage) {
SharedBuffer::sharedBuffer(mStorage)->acquire(); SharedBuffer::bufferFromData(mStorage)->acquire();
} }
} }
@ -73,7 +73,7 @@ VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
if (rhs.mCount) { if (rhs.mCount) {
mStorage = rhs.mStorage; mStorage = rhs.mStorage;
mCount = rhs.mCount; mCount = rhs.mCount;
SharedBuffer::sharedBuffer(mStorage)->acquire(); SharedBuffer::bufferFromData(mStorage)->acquire();
} else { } else {
mStorage = 0; mStorage = 0;
mCount = 0; mCount = 0;
@ -85,7 +85,7 @@ VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
void* VectorImpl::editArrayImpl() void* VectorImpl::editArrayImpl()
{ {
if (mStorage) { if (mStorage) {
SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage)->attemptEdit(); SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage)->attemptEdit();
if (sb == 0) { if (sb == 0) {
sb = SharedBuffer::alloc(capacity() * mItemSize); sb = SharedBuffer::alloc(capacity() * mItemSize);
if (sb) { if (sb) {
@ -101,7 +101,7 @@ void* VectorImpl::editArrayImpl()
size_t VectorImpl::capacity() const size_t VectorImpl::capacity() const
{ {
if (mStorage) { if (mStorage) {
return SharedBuffer::sharedBuffer(mStorage)->size() / mItemSize; return SharedBuffer::bufferFromData(mStorage)->size() / mItemSize;
} }
return 0; return 0;
} }
@ -346,7 +346,7 @@ ssize_t VectorImpl::setCapacity(size_t new_capacity)
void VectorImpl::release_storage() void VectorImpl::release_storage()
{ {
if (mStorage) { if (mStorage) {
const SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage); const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
if (sb->release(SharedBuffer::eKeepStorage) == 1) { if (sb->release(SharedBuffer::eKeepStorage) == 1) {
_do_destroy(mStorage, mCount); _do_destroy(mStorage, mCount);
SharedBuffer::dealloc(sb); SharedBuffer::dealloc(sb);
@ -372,7 +372,7 @@ void* VectorImpl::_grow(size_t where, size_t amount)
(mFlags & HAS_TRIVIAL_COPY) && (mFlags & HAS_TRIVIAL_COPY) &&
(mFlags & HAS_TRIVIAL_DTOR)) (mFlags & HAS_TRIVIAL_DTOR))
{ {
const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage); const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
mStorage = sb->data(); mStorage = sb->data();
} else { } else {
@ -424,7 +424,7 @@ void VectorImpl::_shrink(size_t where, size_t amount)
(mFlags & HAS_TRIVIAL_COPY) && (mFlags & HAS_TRIVIAL_COPY) &&
(mFlags & HAS_TRIVIAL_DTOR)) (mFlags & HAS_TRIVIAL_DTOR))
{ {
const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage); const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
mStorage = sb->data(); mStorage = sb->data();
} else { } else {