• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
23 #include <sys/types.h>
24 #include <utils/Errors.h>
25 #include <utils/Debug.h>
26 
27 #include <type_traits>
28 
29 namespace android {
30 
31 
32 class FlattenableUtils {
33 public:
34     template<size_t N>
align(size_t size)35     static size_t align(size_t size) {
36         static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
37         return (size + (N-1)) & ~(N-1);
38     }
39 
40     template<size_t N>
align(void const * & buffer)41     static size_t align(void const*& buffer) {
42         static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
43         uintptr_t b = uintptr_t(buffer);
44         buffer = reinterpret_cast<void*>((uintptr_t(buffer) + (N-1)) & ~(N-1));
45         return size_t(uintptr_t(buffer) - b);
46     }
47 
48     template<size_t N>
align(void * & buffer)49     static size_t align(void*& buffer) {
50         static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
51         void* b = buffer;
52         buffer = reinterpret_cast<void*>((uintptr_t(buffer) + (N-1)) & ~(N-1));
53         size_t delta = size_t(uintptr_t(buffer) - uintptr_t(b));
54         memset(b, 0, delta);
55         return delta;
56     }
57 
advance(void * & buffer,size_t & size,size_t offset)58     static void advance(void*& buffer, size_t& size, size_t offset) {
59         buffer = reinterpret_cast<void*>( uintptr_t(buffer) + offset );
60         size -= offset;
61     }
62 
advance(void const * & buffer,size_t & size,size_t offset)63     static void advance(void const*& buffer, size_t& size, size_t offset) {
64         buffer = reinterpret_cast<void const*>( uintptr_t(buffer) + offset );
65         size -= offset;
66     }
67 
68     // write a POD structure
69     template<typename T>
write(void * & buffer,size_t & size,const T & value)70     static void write(void*& buffer, size_t& size, const T& value) {
71         static_assert(std::is_trivially_copyable<T>::value,
72                       "Cannot flatten a non-trivially-copyable type");
73         memcpy(buffer, &value, sizeof(T));
74         advance(buffer, size, sizeof(T));
75     }
76 
77     // read a POD structure
78     template<typename T>
read(void const * & buffer,size_t & size,T & value)79     static void read(void const*& buffer, size_t& size, T& value) {
80         static_assert(std::is_trivially_copyable<T>::value,
81                       "Cannot unflatten a non-trivially-copyable type");
82         memcpy(&value, buffer, sizeof(T));
83         advance(buffer, size, sizeof(T));
84     }
85 };
86 
87 
88 /*
89  * The Flattenable protocol allows an object to serialize itself out
90  * to a byte-buffer and an array of file descriptors.
91  * Flattenable objects must implement this protocol.
92  */
93 
94 template <typename T>
95 class Flattenable {
96 public:
97     // size in bytes of the flattened object
98     inline size_t getFlattenedSize() const;
99 
100     // number of file descriptors to flatten
101     inline size_t getFdCount() const;
102 
103     // flattens the object into buffer.
104     // size should be at least of getFlattenedSize()
105     // file descriptors are written in the fds[] array but ownership is
106     // not transfered (ie: they must be dupped by the caller of
107     // flatten() if needed).
108     inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
109 
110     // unflattens the object from buffer.
111     // size should be equal to the value of getFlattenedSize() when the
112     // object was flattened.
113     // unflattened file descriptors are found in the fds[] array and
114     // don't need to be dupped(). ie: the caller of unflatten doesn't
115     // keep ownership. If a fd is not retained by unflatten() it must be
116     // explicitly closed.
117     inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
118 };
119 
120 template<typename T>
getFlattenedSize()121 inline size_t Flattenable<T>::getFlattenedSize() const {
122     return static_cast<T const*>(this)->T::getFlattenedSize();
123 }
124 template<typename T>
getFdCount()125 inline size_t Flattenable<T>::getFdCount() const {
126     return static_cast<T const*>(this)->T::getFdCount();
127 }
128 template<typename T>
flatten(void * & buffer,size_t & size,int * & fds,size_t & count)129 inline status_t Flattenable<T>::flatten(
130         void*& buffer, size_t& size, int*& fds, size_t& count) const {
131     return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count);
132 }
133 template<typename T>
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)134 inline status_t Flattenable<T>::unflatten(
135         void const*& buffer, size_t& size, int const*& fds, size_t& count) {
136     return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count);
137 }
138 
139 /*
140  * LightFlattenable is a protocol allowing object to serialize themselves out
141  * to a byte-buffer. Because it doesn't handle file-descriptors,
142  * LightFlattenable is usually more size efficient than Flattenable.
143  * LightFlattenable objects must implement this protocol.
144  */
145 template <typename T>
146 class LightFlattenable {
147 public:
148     // returns whether this object always flatten into the same size.
149     // for efficiency, this should always be inline.
150     inline bool isFixedSize() const;
151 
152     // returns size in bytes of the flattened object. must be a constant.
153     inline size_t getFlattenedSize() const;
154 
155     // flattens the object into buffer.
156     inline status_t flatten(void* buffer, size_t size) const;
157 
158     // unflattens the object from buffer of given size.
159     inline status_t unflatten(void const* buffer, size_t size);
160 };
161 
162 template <typename T>
isFixedSize()163 inline bool LightFlattenable<T>::isFixedSize() const {
164     return static_cast<T const*>(this)->T::isFixedSize();
165 }
166 template <typename T>
getFlattenedSize()167 inline size_t LightFlattenable<T>::getFlattenedSize() const {
168     return static_cast<T const*>(this)->T::getFlattenedSize();
169 }
170 template <typename T>
flatten(void * buffer,size_t size)171 inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
172     return static_cast<T const*>(this)->T::flatten(buffer, size);
173 }
174 template <typename T>
unflatten(void const * buffer,size_t size)175 inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) {
176     return static_cast<T*>(this)->T::unflatten(buffer, size);
177 }
178 
179 /*
180  * LightFlattenablePod is an implementation of the LightFlattenable protocol
181  * for POD (plain-old-data) objects.
182  * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
183  * need to implement any methods; obviously Foo must be a POD structure.
184  */
185 template <typename T>
186 class LightFlattenablePod : public LightFlattenable<T> {
187 public:
isFixedSize()188     inline bool isFixedSize() const {
189         return true;
190     }
191 
getFlattenedSize()192     inline size_t getFlattenedSize() const {
193         return sizeof(T);
194     }
flatten(void * buffer,size_t size)195     inline status_t flatten(void* buffer, size_t size) const {
196         if (size < sizeof(T)) return NO_MEMORY;
197         memcpy(buffer, static_cast<T const*>(this), sizeof(T));
198         return OK;
199     }
unflatten(void const * buffer,size_t)200     inline status_t unflatten(void const* buffer, size_t) {
201         memcpy(static_cast<T*>(this), buffer, sizeof(T));
202         return OK;
203     }
204 };
205 
206 }  // namespace android
207 
208 #endif /* ANDROID_UTILS_FLATTENABLE_H */
209