Flattenable.h revision 6d611a891d0c818bf3a34a7cad036f3f0064bc4a
1a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian/* 2a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Copyright (C) 2010 The Android Open Source Project 3a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * 4a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * you may not use this file except in compliance with the License. 6a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * You may obtain a copy of the License at 7a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * 8a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * 10a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Unless required by applicable law or agreed to in writing, software 11a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * See the License for the specific language governing permissions and 14a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * limitations under the License. 15a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian */ 16a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 17a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#ifndef ANDROID_UTILS_FLATTENABLE_H 18a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#define ANDROID_UTILS_FLATTENABLE_H 19a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 20a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 21a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <stdint.h> 22a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <sys/types.h> 23a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <utils/Errors.h> 246d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian#include <utils/Debug.h> 25a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 26a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopiannamespace android { 27a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 286d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 296d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianclass FlattenableUtils { 306d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianpublic: 316d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian template<int N> 326d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static size_t align(size_t size) { 336d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) ); 346d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return (size + (N-1)) & ~(N-1); 356d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 366d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 376d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian template<int N> 386d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static size_t align(void*& buffer) { 396d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) ); 406d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian intptr_t b = intptr_t(buffer); 416d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian buffer = (void*)((intptr_t(buffer) + (N-1)) & ~(N-1)); 426d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return size_t(intptr_t(buffer) - b); 436d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 446d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 456d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void advance(void*& buffer, size_t& size, size_t offset) { 466d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian buffer = reinterpret_cast<void*>( intptr_t(buffer) + offset ); 476d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian size -= offset; 486d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 496d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 506d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void advance(void const*& buffer, size_t& size, size_t offset) { 516d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian buffer = reinterpret_cast<void const*>( intptr_t(buffer) + offset ); 526d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian size -= offset; 536d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 546d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 556d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian // write a POD structure 566d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian template<typename T> 576d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void write(void*& buffer, size_t& size, const T& value) { 586d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian *static_cast<T*>(buffer) = value; 596d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian advance(buffer, size, sizeof(T)); 606d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 616d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 626d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian // read a POD structure 636d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian template<typename T> 646d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void read(void const*& buffer, size_t& size, T& value) { 656d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian value = *static_cast<T const*>(buffer); 666d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian advance(buffer, size, sizeof(T)); 676d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 686d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian}; 696d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 706d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 712497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 726d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * The Flattenable protocol allows an object to serialize itself out 732497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * to a byte-buffer and an array of file descriptors. 746d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * Flattenable objects must implement this protocol. 752497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 762497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 776d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate <typename T> 786d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianclass Flattenable { 79a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopianpublic: 80a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size in bytes of the flattened object 816d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFlattenedSize() const; 82a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 83a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // number of file descriptors to flatten 846d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFdCount() const; 85a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 86a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // flattens the object into buffer. 87a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size should be at least of getFlattenedSize() 88a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // file descriptors are written in the fds[] array but ownership is 89a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // not transfered (ie: they must be dupped by the caller of 90a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // flatten() if needed). 916d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline status_t flatten(void*& buffer, size_t& size, 926d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian int*& fds, size_t& count) const; 93a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 94a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // unflattens the object from buffer. 95a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size should be equal to the value of getFlattenedSize() when the 96a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // object was flattened. 97a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // unflattened file descriptors are found in the fds[] array and 98a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // don't need to be dupped(). ie: the caller of unflatten doesn't 99a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // keep ownership. If a fd is not retained by unflatten() it must be 100a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // explicitly closed. 1016d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline status_t unflatten(void const*& buffer, size_t& size, 1026d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian int const*& fds, size_t& count); 103a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; 104a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 1056d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1066d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t Flattenable<T>::getFlattenedSize() const { 1076d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::getFlattenedSize(); 1086d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1096d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1106d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t Flattenable<T>::getFdCount() const { 1116d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::getFdCount(); 1126d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1136d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1146d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t Flattenable<T>::flatten( 1156d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian void*& buffer, size_t& size, int*& fds, size_t& count) const { 1166d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count); 1176d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1186d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1196d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t Flattenable<T>::unflatten( 1206d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian void const*& buffer, size_t& size, int const*& fds, size_t& count) { 1216d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count); 1226d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1236d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 1242497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 1252497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable is a protocol allowing object to serialize themselves out 1266d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * to a byte-buffer. Because it doesn't handle file-descriptors, 1276d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * LightFlattenable is usually more size efficient than Flattenable. 1282497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable objects must implement this protocol. 1292497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 1302497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1312497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenable { 1322497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic: 1332497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // returns whether this object always flatten into the same size. 1342497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // for efficiency, this should always be inline. 1352497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline bool isFixedSize() const; 1362497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1372497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // returns size in bytes of the flattened object. must be a constant. 1386d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFlattenedSize() const; 1392497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1402497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // flattens the object into buffer. 1416d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline status_t flatten(void* buffer, size_t size) const; 1422497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1432497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // unflattens the object from buffer of given size. 1442497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t unflatten(void const* buffer, size_t size); 1452497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}; 1462497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1472497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1482497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline bool LightFlattenable<T>::isFixedSize() const { 1492497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T const*>(this)->T::isFixedSize(); 1502497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1512497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1526d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t LightFlattenable<T>::getFlattenedSize() const { 1536d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::getFlattenedSize(); 1542497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1552497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1566d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const { 1576d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::flatten(buffer, size); 1582497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1592497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1602497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) { 1612497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T*>(this)->T::unflatten(buffer, size); 1622497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1632497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1642497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 1652497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenablePod is an implementation of the LightFlattenable protocol 1662497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * for POD (plain-old-data) objects. 1676d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no 1686d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * need to implement any methods; obviously Foo must be a POD structure. 1692497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 1702497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1712497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenablePod : public LightFlattenable<T> { 1722497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic: 1732497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline bool isFixedSize() const { 1742497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return true; 1752497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1762497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1776d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFlattenedSize() const { 1782497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return sizeof(T); 1792497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1806d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline status_t flatten(void* buffer, size_t size) const { 1816d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian if (size < sizeof(T)) return NO_MEMORY; 1822497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this); 1832497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return NO_ERROR; 1842497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1852497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t unflatten(void const* buffer, size_t) { 1862497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer); 1872497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return NO_ERROR; 1882497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1892497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}; 1902497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1912497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 192a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; // namespace android 193a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 194a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 195a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#endif /* ANDROID_UTILS_FLATTENABLE_H */ 196