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