blob: 882a8b2499a1c9e29228003ecf34760984867218 [file] [log] [blame]
Mathias Agopiana580e682010-02-11 17:30:52 -08001/*
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 Agopian6d611a82013-07-29 21:24:40 -070024#include <utils/Debug.h>
Mathias Agopiana580e682010-02-11 17:30:52 -080025
26namespace android {
27
Mathias Agopian6d611a82013-07-29 21:24:40 -070028
29class FlattenableUtils {
30public:
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 Agopianddff6232013-08-01 12:47:58 -070038 static size_t align(void const*& buffer) {
Mathias Agopian6d611a82013-07-29 21:24:40 -070039 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 Agopianddff6232013-08-01 12:47:58 -070045 template<int N>
46 static size_t align(void*& buffer) {
47 return align<N>( const_cast<void const*&>(buffer) );
48 }
49
Mathias Agopian6d611a82013-07-29 21:24:40 -070050 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 Agopian2497a152012-08-12 19:37:16 -070076/*
Mathias Agopian6d611a82013-07-29 21:24:40 -070077 * The Flattenable protocol allows an object to serialize itself out
Mathias Agopian2497a152012-08-12 19:37:16 -070078 * to a byte-buffer and an array of file descriptors.
Mathias Agopian6d611a82013-07-29 21:24:40 -070079 * Flattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -070080 */
81
Mathias Agopian6d611a82013-07-29 21:24:40 -070082template <typename T>
83class Flattenable {
Mathias Agopiana580e682010-02-11 17:30:52 -080084public:
85 // size in bytes of the flattened object
Mathias Agopian6d611a82013-07-29 21:24:40 -070086 inline size_t getFlattenedSize() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080087
88 // number of file descriptors to flatten
Mathias Agopian6d611a82013-07-29 21:24:40 -070089 inline size_t getFdCount() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080090
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 Agopianddff6232013-08-01 12:47:58 -070096 inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
Mathias Agopiana580e682010-02-11 17:30:52 -080097
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 Agopianddff6232013-08-01 12:47:58 -0700105 inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Mathias Agopiana580e682010-02-11 17:30:52 -0800106};
107
Mathias Agopian6d611a82013-07-29 21:24:40 -0700108template<typename T>
109inline size_t Flattenable<T>::getFlattenedSize() const {
110 return static_cast<T const*>(this)->T::getFlattenedSize();
111}
112template<typename T>
113inline size_t Flattenable<T>::getFdCount() const {
114 return static_cast<T const*>(this)->T::getFdCount();
115}
116template<typename T>
117inline 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}
121template<typename T>
122inline 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 Agopian2497a152012-08-12 19:37:16 -0700127/*
128 * LightFlattenable is a protocol allowing object to serialize themselves out
Mathias Agopian6d611a82013-07-29 21:24:40 -0700129 * to a byte-buffer. Because it doesn't handle file-descriptors,
130 * LightFlattenable is usually more size efficient than Flattenable.
Mathias Agopian2497a152012-08-12 19:37:16 -0700131 * LightFlattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -0700132 */
133template <typename T>
134class LightFlattenable {
135public:
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 Agopian6d611a82013-07-29 21:24:40 -0700141 inline size_t getFlattenedSize() const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700142
143 // flattens the object into buffer.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700144 inline status_t flatten(void* buffer, size_t size) const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700145
146 // unflattens the object from buffer of given size.
147 inline status_t unflatten(void const* buffer, size_t size);
148};
149
150template <typename T>
151inline bool LightFlattenable<T>::isFixedSize() const {
152 return static_cast<T const*>(this)->T::isFixedSize();
153}
154template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700155inline size_t LightFlattenable<T>::getFlattenedSize() const {
156 return static_cast<T const*>(this)->T::getFlattenedSize();
Mathias Agopian2497a152012-08-12 19:37:16 -0700157}
158template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700159inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
160 return static_cast<T const*>(this)->T::flatten(buffer, size);
Mathias Agopian2497a152012-08-12 19:37:16 -0700161}
162template <typename T>
163inline 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 Agopian6d611a82013-07-29 21:24:40 -0700170 * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
171 * need to implement any methods; obviously Foo must be a POD structure.
Mathias Agopian2497a152012-08-12 19:37:16 -0700172 */
173template <typename T>
174class LightFlattenablePod : public LightFlattenable<T> {
175public:
176 inline bool isFixedSize() const {
177 return true;
178 }
179
Mathias Agopian6d611a82013-07-29 21:24:40 -0700180 inline size_t getFlattenedSize() const {
Mathias Agopian2497a152012-08-12 19:37:16 -0700181 return sizeof(T);
182 }
Mathias Agopian6d611a82013-07-29 21:24:40 -0700183 inline status_t flatten(void* buffer, size_t size) const {
184 if (size < sizeof(T)) return NO_MEMORY;
Mathias Agopian2497a152012-08-12 19:37:16 -0700185 *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 Agopiana580e682010-02-11 17:30:52 -0800195}; // namespace android
196
197
198#endif /* ANDROID_UTILS_FLATTENABLE_H */