diff options
Diffstat (limited to 'include/utils/Flattenable.h')
-rw-r--r-- | include/utils/Flattenable.h | 111 |
1 files changed, 86 insertions, 25 deletions
diff --git a/include/utils/Flattenable.h b/include/utils/Flattenable.h index e40d289de5..c283ad766c 100644 --- a/include/utils/Flattenable.h +++ b/include/utils/Flattenable.h @@ -21,30 +21,75 @@ #include <stdint.h> #include <sys/types.h> #include <utils/Errors.h> +#include <utils/Debug.h> namespace android { + +class FlattenableUtils { +public: + template<int N> + static size_t align(size_t size) { + COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) ); + return (size + (N-1)) & ~(N-1); + } + + template<int N> + 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<void*>( intptr_t(buffer) + offset ); + size -= offset; + } + + static void advance(void const*& buffer, size_t& size, size_t offset) { + buffer = reinterpret_cast<void const*>( intptr_t(buffer) + offset ); + size -= offset; + } + + // write a POD structure + template<typename T> + static void write(void*& buffer, size_t& size, const T& value) { + *static_cast<T*>(buffer) = value; + advance(buffer, size, sizeof(T)); + } + + // read a POD structure + template<typename T> + static void read(void const*& buffer, size_t& size, T& value) { + value = *static_cast<T const*>(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 <typename T> +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<typename T> +inline size_t Flattenable<T>::getFlattenedSize() const { + return static_cast<T const*>(this)->T::getFlattenedSize(); +} +template<typename T> +inline size_t Flattenable<T>::getFdCount() const { + return static_cast<T const*>(this)->T::getFdCount(); +} +template<typename T> +inline status_t Flattenable<T>::flatten( + void*& buffer, size_t& size, int*& fds, size_t& count) const { + return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count); +} +template<typename T> +inline status_t Flattenable<T>::unflatten( + void const*& buffer, size_t& size, int const*& fds, size_t& count) { + return static_cast<T*>(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 <typename T> 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<T>::isFixedSize() const { return static_cast<T const*>(this)->T::isFixedSize(); } template <typename T> -inline size_t LightFlattenable<T>::getSize() const { - return static_cast<T const*>(this)->T::getSize(); +inline size_t LightFlattenable<T>::getFlattenedSize() const { + return static_cast<T const*>(this)->T::getFlattenedSize(); } template <typename T> -inline status_t LightFlattenable<T>::flatten(void* buffer) const { - return static_cast<T const*>(this)->T::flatten(buffer); +inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const { + return static_cast<T const*>(this)->T::flatten(buffer, size); } template <typename T> inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) { @@ -106,6 +164,8 @@ inline status_t LightFlattenable<T>::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<Foo> to make Foo flattenable; no + * need to implement any methods; obviously Foo must be a POD structure. */ template <typename T> class LightFlattenablePod : public LightFlattenable<T> { @@ -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<T*>(buffer) = *static_cast<T const*>(this); return NO_ERROR; } |