| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 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> | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 24 | #include <utils/Debug.h> | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 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> | 
| Mathias Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 38 | static size_t align(void const*& buffer) { | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 45 | template<int N> | 
|  | 46 | static size_t align(void*& buffer) { | 
|  | 47 | return align<N>( const_cast<void const*&>(buffer) ); | 
|  | 48 | } | 
|  | 49 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 76 | /* | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 77 | * The Flattenable protocol allows an object to serialize itself out | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 78 | * to a byte-buffer and an array of file descriptors. | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 79 | * Flattenable objects must implement this protocol. | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 80 | */ | 
|  | 81 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 82 | template <typename T> | 
|  | 83 | class Flattenable { | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 84 | public: | 
|  | 85 | // size in bytes of the flattened object | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 86 | inline size_t getFlattenedSize() const; | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 87 |  | 
|  | 88 | // number of file descriptors to flatten | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 89 | inline size_t getFdCount() const; | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 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). | 
| Mathias Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 96 | inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 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. | 
| Mathias Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 105 | inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 106 | }; | 
|  | 107 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 127 | /* | 
|  | 128 | * LightFlattenable is a protocol allowing object to serialize themselves out | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 129 | * to a byte-buffer. Because it doesn't handle file-descriptors, | 
|  | 130 | * LightFlattenable is usually more size efficient than Flattenable. | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 131 | * LightFlattenable objects must implement this protocol. | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 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. | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 141 | inline size_t getFlattenedSize() const; | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | // flattens the object into buffer. | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 144 | inline status_t flatten(void* buffer, size_t size) const; | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 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> | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 155 | inline size_t LightFlattenable<T>::getFlattenedSize() const { | 
|  | 156 | return static_cast<T const*>(this)->T::getFlattenedSize(); | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 157 | } | 
|  | 158 | template <typename T> | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 159 | inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const { | 
|  | 160 | return static_cast<T const*>(this)->T::flatten(buffer, size); | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 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. | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 170 | * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no | 
|  | 171 | * need to implement any methods; obviously Foo must be a POD structure. | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 172 | */ | 
|  | 173 | template <typename T> | 
|  | 174 | class LightFlattenablePod : public LightFlattenable<T> { | 
|  | 175 | public: | 
|  | 176 | inline bool isFixedSize() const { | 
|  | 177 | return true; | 
|  | 178 | } | 
|  | 179 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 180 | inline size_t getFlattenedSize() const { | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 181 | return sizeof(T); | 
|  | 182 | } | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 183 | inline status_t flatten(void* buffer, size_t size) const { | 
|  | 184 | if (size < sizeof(T)) return NO_MEMORY; | 
| Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 195 | }; // namespace android | 
|  | 196 |  | 
|  | 197 |  | 
|  | 198 | #endif /* ANDROID_UTILS_FLATTENABLE_H */ |