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> 38ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian static size_t align(void const*& 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 45ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian template<int N> 46ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian static size_t align(void*& buffer) { 47ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian return align<N>( const_cast<void const*&>(buffer) ); 48ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian } 49ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian 506d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void advance(void*& buffer, size_t& size, size_t offset) { 516d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian buffer = reinterpret_cast<void*>( intptr_t(buffer) + offset ); 526d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian size -= offset; 536d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 546d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 556d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void advance(void const*& buffer, size_t& size, size_t offset) { 566d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian buffer = reinterpret_cast<void const*>( intptr_t(buffer) + offset ); 576d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian size -= offset; 586d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 596d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 606d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian // write a POD structure 616d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian template<typename T> 626d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void write(void*& buffer, size_t& size, const T& value) { 636d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian *static_cast<T*>(buffer) = value; 646d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian advance(buffer, size, sizeof(T)); 656d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 666d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 676d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian // read a POD structure 686d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian template<typename T> 696d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian static void read(void const*& buffer, size_t& size, T& value) { 706d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian value = *static_cast<T const*>(buffer); 716d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian advance(buffer, size, sizeof(T)); 726d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian } 736d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian}; 746d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 756d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 762497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 776d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * The Flattenable protocol allows an object to serialize itself out 782497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * to a byte-buffer and an array of file descriptors. 796d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * Flattenable objects must implement this protocol. 802497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 812497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 826d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate <typename T> 836d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianclass Flattenable { 84a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopianpublic: 85a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size in bytes of the flattened object 866d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFlattenedSize() const; 87a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 88a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // number of file descriptors to flatten 896d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFdCount() const; 90a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 91a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // flattens the object into buffer. 92a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size should be at least of getFlattenedSize() 93a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // file descriptors are written in the fds[] array but ownership is 94a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // not transfered (ie: they must be dupped by the caller of 95a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // flatten() if needed). 96ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 97a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 98a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // unflattens the object from buffer. 99a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // size should be equal to the value of getFlattenedSize() when the 100a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // object was flattened. 101a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // unflattened file descriptors are found in the fds[] array and 102a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // don't need to be dupped(). ie: the caller of unflatten doesn't 103a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // keep ownership. If a fd is not retained by unflatten() it must be 104a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian // explicitly closed. 105ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 106a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; 107a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 1086d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1096d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t Flattenable<T>::getFlattenedSize() const { 1106d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::getFlattenedSize(); 1116d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1126d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1136d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t Flattenable<T>::getFdCount() const { 1146d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::getFdCount(); 1156d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1166d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1176d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t Flattenable<T>::flatten( 1186d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian void*& buffer, size_t& size, int*& fds, size_t& count) const { 1196d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count); 1206d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1216d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T> 1226d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t Flattenable<T>::unflatten( 1236d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian void const*& buffer, size_t& size, int const*& fds, size_t& count) { 1246d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count); 1256d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian} 1266d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian 1272497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 1282497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable is a protocol allowing object to serialize themselves out 1296d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * to a byte-buffer. Because it doesn't handle file-descriptors, 1306d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * LightFlattenable is usually more size efficient than Flattenable. 1312497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable objects must implement this protocol. 1322497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 1332497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1342497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenable { 1352497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic: 1362497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // returns whether this object always flatten into the same size. 1372497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // for efficiency, this should always be inline. 1382497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline bool isFixedSize() const; 1392497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1402497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // returns size in bytes of the flattened object. must be a constant. 1416d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFlattenedSize() const; 1422497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1432497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // flattens the object into buffer. 1446d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline status_t flatten(void* buffer, size_t size) const; 1452497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1462497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian // unflattens the object from buffer of given size. 1472497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t unflatten(void const* buffer, size_t size); 1482497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}; 1492497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1502497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1512497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline bool LightFlattenable<T>::isFixedSize() const { 1522497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T const*>(this)->T::isFixedSize(); 1532497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1542497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1556d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t LightFlattenable<T>::getFlattenedSize() const { 1566d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::getFlattenedSize(); 1572497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1582497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1596d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const { 1606d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian return static_cast<T const*>(this)->T::flatten(buffer, size); 1612497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1622497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1632497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) { 1642497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return static_cast<T*>(this)->T::unflatten(buffer, size); 1652497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian} 1662497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1672497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/* 1682497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenablePod is an implementation of the LightFlattenable protocol 1692497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * for POD (plain-old-data) objects. 1706d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no 1716d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * need to implement any methods; obviously Foo must be a POD structure. 1722497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */ 1732497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T> 1742497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenablePod : public LightFlattenable<T> { 1752497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic: 1762497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline bool isFixedSize() const { 1772497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return true; 1782497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1792497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1806d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline size_t getFlattenedSize() const { 1812497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return sizeof(T); 1822497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1836d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian inline status_t flatten(void* buffer, size_t size) const { 1846d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian if (size < sizeof(T)) return NO_MEMORY; 1852497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this); 1862497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return NO_ERROR; 1872497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1882497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian inline status_t unflatten(void const* buffer, size_t) { 1892497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer); 1902497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian return NO_ERROR; 1912497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian } 1922497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}; 1932497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 1942497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian 195a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; // namespace android 196a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 197a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian 198a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#endif /* ANDROID_UTILS_FLATTENABLE_H */ 199