From 1d76781b7aa19611c4045fdf6b848af6c6094e0b Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Mon, 29 Jul 2013 21:24:40 -0700 Subject: [PATCH] Make Flattenable not virtual (libutils) Making an object Flattenable doesn't force it to become virtual anymore. For instance, Fence and GraphicBuffer are now non-virtual classes. Also change Flatennable protocol a bit so that it updates its parameters (pointers, sizes) to make it easier to implement a flattenable in terms of other flattenables. Change-Id: Ie81dc7637180b3c2cfcbaf644f8987ca804eb891 --- include/utils/BlobCache.h | 20 ++----- include/utils/Flattenable.h | 111 ++++++++++++++++++++++++++++-------- libs/utils/Android.mk | 1 - libs/utils/BlobCache.cpp | 20 +------ libs/utils/Flattenable.cpp | 24 -------- 5 files changed, 94 insertions(+), 82 deletions(-) delete mode 100644 libs/utils/Flattenable.cpp diff --git a/include/utils/BlobCache.h b/include/utils/BlobCache.h index 4f342a2ad..7d621e438 100644 --- a/include/utils/BlobCache.h +++ b/include/utils/BlobCache.h @@ -33,7 +33,8 @@ namespace android { // and then reloaded in a subsequent execution of the program. This // serialization is non-portable and the data should only be used by the device // that generated it. -class BlobCache : public RefBase, public Flattenable { +class BlobCache : public RefBase { + public: // Create an empty blob cache. The blob cache will cache key/value pairs @@ -78,14 +79,10 @@ public: // 0 <= valueSize size_t get(const void* key, size_t keySize, void* value, size_t valueSize); + // getFlattenedSize returns the number of bytes needed to store the entire // serialized cache. - virtual size_t getFlattenedSize() const; - - // getFdCount returns the number of file descriptors that will result from - // flattening the cache. This will always return 0 so as to allow the - // flattened cache to be saved to disk and then later restored. - virtual size_t getFdCount() const; + size_t getFlattenedSize() const; // flatten serializes the current contents of the cache into the memory // pointed to by 'buffer'. The serialized cache contents can later be @@ -94,9 +91,7 @@ public: // // Preconditions: // size >= this.getFlattenedSize() - // count == 0 - virtual status_t flatten(void* buffer, size_t size, int fds[], - size_t count) const; + status_t flatten(void* buffer, size_t size) const; // unflatten replaces the contents of the cache with the serialized cache // contents in the memory pointed to by 'buffer'. The previous contents of @@ -104,10 +99,7 @@ public: // unflattening the serialized cache contents then the BlobCache will be // left in an empty state. // - // Preconditions: - // count == 0 - virtual status_t unflatten(void const* buffer, size_t size, int fds[], - size_t count); + status_t unflatten(void const* buffer, size_t size); private: // Copying is disallowed. diff --git a/include/utils/Flattenable.h b/include/utils/Flattenable.h index e40d289de..c283ad766 100644 --- a/include/utils/Flattenable.h +++ b/include/utils/Flattenable.h @@ -21,30 +21,75 @@ #include #include #include +#include namespace android { + +class FlattenableUtils { +public: + template + static size_t align(size_t size) { + COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) ); + return (size + (N-1)) & ~(N-1); + } + + template + static size_t align(void*& buffer) { + COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) ); + intptr_t b = intptr_t(buffer); + buffer = (void*)((intptr_t(buffer) + (N-1)) & ~(N-1)); + return size_t(intptr_t(buffer) - b); + } + + static void advance(void*& buffer, size_t& size, size_t offset) { + buffer = reinterpret_cast( intptr_t(buffer) + offset ); + size -= offset; + } + + static void advance(void const*& buffer, size_t& size, size_t offset) { + buffer = reinterpret_cast( intptr_t(buffer) + offset ); + size -= offset; + } + + // write a POD structure + template + static void write(void*& buffer, size_t& size, const T& value) { + *static_cast(buffer) = value; + advance(buffer, size, sizeof(T)); + } + + // read a POD structure + template + static void read(void const*& buffer, size_t& size, T& value) { + value = *static_cast(buffer); + advance(buffer, size, sizeof(T)); + } +}; + + /* - * The Flattenable interface allows an object to serialize itself out + * The Flattenable protocol allows an object to serialize itself out * to a byte-buffer and an array of file descriptors. + * Flattenable objects must implement this protocol. */ -class Flattenable -{ +template +class Flattenable { public: // size in bytes of the flattened object - virtual size_t getFlattenedSize() const = 0; + inline size_t getFlattenedSize() const; // number of file descriptors to flatten - virtual size_t getFdCount() const = 0; + inline size_t getFdCount() const; // flattens the object into buffer. // size should be at least of getFlattenedSize() // file descriptors are written in the fds[] array but ownership is // not transfered (ie: they must be dupped by the caller of // flatten() if needed). - virtual status_t flatten(void* buffer, size_t size, - int fds[], size_t count) const = 0; + inline status_t flatten(void*& buffer, size_t& size, + int*& fds, size_t& count) const; // unflattens the object from buffer. // size should be equal to the value of getFlattenedSize() when the @@ -53,21 +98,34 @@ public: // don't need to be dupped(). ie: the caller of unflatten doesn't // keep ownership. If a fd is not retained by unflatten() it must be // explicitly closed. - virtual status_t unflatten(void const* buffer, size_t size, - int fds[], size_t count) = 0; - -protected: - virtual ~Flattenable() = 0; - + inline status_t unflatten(void const*& buffer, size_t& size, + int const*& fds, size_t& count); }; +template +inline size_t Flattenable::getFlattenedSize() const { + return static_cast(this)->T::getFlattenedSize(); +} +template +inline size_t Flattenable::getFdCount() const { + return static_cast(this)->T::getFdCount(); +} +template +inline status_t Flattenable::flatten( + void*& buffer, size_t& size, int*& fds, size_t& count) const { + return static_cast(this)->T::flatten(buffer, size, fds, count); +} +template +inline status_t Flattenable::unflatten( + void const*& buffer, size_t& size, int const*& fds, size_t& count) { + return static_cast(this)->T::unflatten(buffer, size, fds, count); +} + /* * LightFlattenable is a protocol allowing object to serialize themselves out - * to a byte-buffer. - * + * to a byte-buffer. Because it doesn't handle file-descriptors, + * LightFlattenable is usually more size efficient than Flattenable. * LightFlattenable objects must implement this protocol. - * - * LightFlattenable doesn't require the object to be virtual. */ template class LightFlattenable { @@ -77,10 +135,10 @@ public: inline bool isFixedSize() const; // returns size in bytes of the flattened object. must be a constant. - inline size_t getSize() const; + inline size_t getFlattenedSize() const; // flattens the object into buffer. - inline status_t flatten(void* buffer) const; + inline status_t flatten(void* buffer, size_t size) const; // unflattens the object from buffer of given size. inline status_t unflatten(void const* buffer, size_t size); @@ -91,12 +149,12 @@ inline bool LightFlattenable::isFixedSize() const { return static_cast(this)->T::isFixedSize(); } template -inline size_t LightFlattenable::getSize() const { - return static_cast(this)->T::getSize(); +inline size_t LightFlattenable::getFlattenedSize() const { + return static_cast(this)->T::getFlattenedSize(); } template -inline status_t LightFlattenable::flatten(void* buffer) const { - return static_cast(this)->T::flatten(buffer); +inline status_t LightFlattenable::flatten(void* buffer, size_t size) const { + return static_cast(this)->T::flatten(buffer, size); } template inline status_t LightFlattenable::unflatten(void const* buffer, size_t size) { @@ -106,6 +164,8 @@ inline status_t LightFlattenable::unflatten(void const* buffer, size_t size) /* * LightFlattenablePod is an implementation of the LightFlattenable protocol * for POD (plain-old-data) objects. + * Simply derive from LightFlattenablePod to make Foo flattenable; no + * need to implement any methods; obviously Foo must be a POD structure. */ template class LightFlattenablePod : public LightFlattenable { @@ -114,10 +174,11 @@ public: return true; } - inline size_t getSize() const { + inline size_t getFlattenedSize() const { return sizeof(T); } - inline status_t flatten(void* buffer) const { + inline status_t flatten(void* buffer, size_t size) const { + if (size < sizeof(T)) return NO_MEMORY; *reinterpret_cast(buffer) = *static_cast(this); return NO_ERROR; } diff --git a/libs/utils/Android.mk b/libs/utils/Android.mk index abf4b2e33..7e6b1be42 100644 --- a/libs/utils/Android.mk +++ b/libs/utils/Android.mk @@ -22,7 +22,6 @@ commonSources:= \ BlobCache.cpp \ CallStack.cpp \ FileMap.cpp \ - Flattenable.cpp \ JenkinsHash.cpp \ LinearAllocator.cpp \ LinearTransform.cpp \ diff --git a/libs/utils/BlobCache.cpp b/libs/utils/BlobCache.cpp index be398ee4e..0fb1d8e99 100644 --- a/libs/utils/BlobCache.cpp +++ b/libs/utils/BlobCache.cpp @@ -176,17 +176,7 @@ size_t BlobCache::getFlattenedSize() const { return size; } -size_t BlobCache::getFdCount() const { - return 0; -} - -status_t BlobCache::flatten(void* buffer, size_t size, int fds[], size_t count) - const { - if (count != 0) { - ALOGE("flatten: nonzero fd count: %zu", count); - return BAD_VALUE; - } - +status_t BlobCache::flatten(void* buffer, size_t size) const { // Write the cache header if (size < sizeof(Header)) { ALOGE("flatten: not enough room for cache header"); @@ -228,16 +218,10 @@ status_t BlobCache::flatten(void* buffer, size_t size, int fds[], size_t count) return OK; } -status_t BlobCache::unflatten(void const* buffer, size_t size, int fds[], - size_t count) { +status_t BlobCache::unflatten(void const* buffer, size_t size) { // All errors should result in the BlobCache being in an empty state. mCacheEntries.clear(); - if (count != 0) { - ALOGE("unflatten: nonzero fd count: %zu", count); - return BAD_VALUE; - } - // Read the cache header if (size < sizeof(Header)) { ALOGE("unflatten: not enough room for cache header"); diff --git a/libs/utils/Flattenable.cpp b/libs/utils/Flattenable.cpp deleted file mode 100644 index 1f2ffaa28..000000000 --- a/libs/utils/Flattenable.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -namespace android { - -Flattenable::~Flattenable() { -} - -}; // namespace android