Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_UTILS_FLATTENABLE_H
     18 #define ANDROID_UTILS_FLATTENABLE_H
     19 
     20 
     21 #include <stdint.h>
     22 #include <sys/types.h>
     23 #include <utils/Errors.h>
     24 #include <utils/Debug.h>
     25 
     26 namespace android {
     27 
     28 
     29 class FlattenableUtils {
     30 public:
     31     template<int N>
     32     static size_t align(size_t size) {
     33         COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) );
     34         return (size + (N-1)) & ~(N-1);
     35     }
     36 
     37     template<int N>
     38     static size_t align(void const*& buffer) {
     39         COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) );
     40         intptr_t b = intptr_t(buffer);
     41         buffer = (void*)((intptr_t(buffer) + (N-1)) & ~(N-1));
     42         return size_t(intptr_t(buffer) - b);
     43     }
     44 
     45     template<int N>
     46     static size_t align(void*& buffer) {
     47         return align<N>( const_cast<void const*&>(buffer) );
     48     }
     49 
     50     static void advance(void*& buffer, size_t& size, size_t offset) {
     51         buffer = reinterpret_cast<void*>( intptr_t(buffer) + offset );
     52         size -= offset;
     53     }
     54 
     55     static void advance(void const*& buffer, size_t& size, size_t offset) {
     56         buffer = reinterpret_cast<void const*>( intptr_t(buffer) + offset );
     57         size -= offset;
     58     }
     59 
     60     // write a POD structure
     61     template<typename T>
     62     static void write(void*& buffer, size_t& size, const T& value) {
     63         *static_cast<T*>(buffer) = value;
     64         advance(buffer, size, sizeof(T));
     65     }
     66 
     67     // read a POD structure
     68     template<typename T>
     69     static void read(void const*& buffer, size_t& size, T& value) {
     70         value = *static_cast<T const*>(buffer);
     71         advance(buffer, size, sizeof(T));
     72     }
     73 };
     74 
     75 
     76 /*
     77  * The Flattenable protocol allows an object to serialize itself out
     78  * to a byte-buffer and an array of file descriptors.
     79  * Flattenable objects must implement this protocol.
     80  */
     81 
     82 template <typename T>
     83 class Flattenable {
     84 public:
     85     // size in bytes of the flattened object
     86     inline size_t getFlattenedSize() const;
     87 
     88     // number of file descriptors to flatten
     89     inline size_t getFdCount() const;
     90 
     91     // flattens the object into buffer.
     92     // size should be at least of getFlattenedSize()
     93     // file descriptors are written in the fds[] array but ownership is
     94     // not transfered (ie: they must be dupped by the caller of
     95     // flatten() if needed).
     96     inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
     97 
     98     // unflattens the object from buffer.
     99     // size should be equal to the value of getFlattenedSize() when the
    100     // object was flattened.
    101     // unflattened file descriptors are found in the fds[] array and
    102     // don't need to be dupped(). ie: the caller of unflatten doesn't
    103     // keep ownership. If a fd is not retained by unflatten() it must be
    104     // explicitly closed.
    105     inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
    106 };
    107 
    108 template<typename T>
    109 inline size_t Flattenable<T>::getFlattenedSize() const {
    110     return static_cast<T const*>(this)->T::getFlattenedSize();
    111 }
    112 template<typename T>
    113 inline size_t Flattenable<T>::getFdCount() const {
    114     return static_cast<T const*>(this)->T::getFdCount();
    115 }
    116 template<typename T>
    117 inline status_t Flattenable<T>::flatten(
    118         void*& buffer, size_t& size, int*& fds, size_t& count) const {
    119     return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count);
    120 }
    121 template<typename T>
    122 inline status_t Flattenable<T>::unflatten(
    123         void const*& buffer, size_t& size, int const*& fds, size_t& count) {
    124     return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count);
    125 }
    126 
    127 /*
    128  * LightFlattenable is a protocol allowing object to serialize themselves out
    129  * to a byte-buffer. Because it doesn't handle file-descriptors,
    130  * LightFlattenable is usually more size efficient than Flattenable.
    131  * LightFlattenable objects must implement this protocol.
    132  */
    133 template <typename T>
    134 class LightFlattenable {
    135 public:
    136     // returns whether this object always flatten into the same size.
    137     // for efficiency, this should always be inline.
    138     inline bool isFixedSize() const;
    139 
    140     // returns size in bytes of the flattened object. must be a constant.
    141     inline size_t getFlattenedSize() const;
    142 
    143     // flattens the object into buffer.
    144     inline status_t flatten(void* buffer, size_t size) const;
    145 
    146     // unflattens the object from buffer of given size.
    147     inline status_t unflatten(void const* buffer, size_t size);
    148 };
    149 
    150 template <typename T>
    151 inline bool LightFlattenable<T>::isFixedSize() const {
    152     return static_cast<T const*>(this)->T::isFixedSize();
    153 }
    154 template <typename T>
    155 inline size_t LightFlattenable<T>::getFlattenedSize() const {
    156     return static_cast<T const*>(this)->T::getFlattenedSize();
    157 }
    158 template <typename T>
    159 inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
    160     return static_cast<T const*>(this)->T::flatten(buffer, size);
    161 }
    162 template <typename T>
    163 inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) {
    164     return static_cast<T*>(this)->T::unflatten(buffer, size);
    165 }
    166 
    167 /*
    168  * LightFlattenablePod is an implementation of the LightFlattenable protocol
    169  * for POD (plain-old-data) objects.
    170  * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
    171  * need to implement any methods; obviously Foo must be a POD structure.
    172  */
    173 template <typename T>
    174 class LightFlattenablePod : public LightFlattenable<T> {
    175 public:
    176     inline bool isFixedSize() const {
    177         return true;
    178     }
    179 
    180     inline size_t getFlattenedSize() const {
    181         return sizeof(T);
    182     }
    183     inline status_t flatten(void* buffer, size_t size) const {
    184         if (size < sizeof(T)) return NO_MEMORY;
    185         *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this);
    186         return NO_ERROR;
    187     }
    188     inline status_t unflatten(void const* buffer, size_t) {
    189         *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer);
    190         return NO_ERROR;
    191     }
    192 };
    193 
    194 
    195 }; // namespace android
    196 
    197 
    198 #endif /* ANDROID_UTILS_FLATTENABLE_H */
    199