blob: c283ad766c111da1590bc0d97f3a0c50f788b8a0 [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>
38 static size_t align(void*& 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 static void advance(void*& buffer, size_t& size, size_t offset) {
46 buffer = reinterpret_cast<void*>( intptr_t(buffer) + offset );
47 size -= offset;
48 }
49
50 static void advance(void const*& buffer, size_t& size, size_t offset) {
51 buffer = reinterpret_cast<void const*>( intptr_t(buffer) + offset );
52 size -= offset;
53 }
54
55 // write a POD structure
56 template<typename T>
57 static void write(void*& buffer, size_t& size, const T& value) {
58 *static_cast<T*>(buffer) = value;
59 advance(buffer, size, sizeof(T));
60 }
61
62 // read a POD structure
63 template<typename T>
64 static void read(void const*& buffer, size_t& size, T& value) {
65 value = *static_cast<T const*>(buffer);
66 advance(buffer, size, sizeof(T));
67 }
68};
69
70
Mathias Agopian2497a152012-08-12 19:37:16 -070071/*
Mathias Agopian6d611a82013-07-29 21:24:40 -070072 * The Flattenable protocol allows an object to serialize itself out
Mathias Agopian2497a152012-08-12 19:37:16 -070073 * to a byte-buffer and an array of file descriptors.
Mathias Agopian6d611a82013-07-29 21:24:40 -070074 * Flattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -070075 */
76
Mathias Agopian6d611a82013-07-29 21:24:40 -070077template <typename T>
78class Flattenable {
Mathias Agopiana580e682010-02-11 17:30:52 -080079public:
80 // size in bytes of the flattened object
Mathias Agopian6d611a82013-07-29 21:24:40 -070081 inline size_t getFlattenedSize() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080082
83 // number of file descriptors to flatten
Mathias Agopian6d611a82013-07-29 21:24:40 -070084 inline size_t getFdCount() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080085
86 // flattens the object into buffer.
87 // size should be at least of getFlattenedSize()
88 // file descriptors are written in the fds[] array but ownership is
89 // not transfered (ie: they must be dupped by the caller of
90 // flatten() if needed).
Mathias Agopian6d611a82013-07-29 21:24:40 -070091 inline status_t flatten(void*& buffer, size_t& size,
92 int*& fds, size_t& count) const;
Mathias Agopiana580e682010-02-11 17:30:52 -080093
94 // unflattens the object from buffer.
95 // size should be equal to the value of getFlattenedSize() when the
96 // object was flattened.
97 // unflattened file descriptors are found in the fds[] array and
98 // don't need to be dupped(). ie: the caller of unflatten doesn't
99 // keep ownership. If a fd is not retained by unflatten() it must be
100 // explicitly closed.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700101 inline status_t unflatten(void const*& buffer, size_t& size,
102 int const*& fds, size_t& count);
Mathias Agopiana580e682010-02-11 17:30:52 -0800103};
104
Mathias Agopian6d611a82013-07-29 21:24:40 -0700105template<typename T>
106inline size_t Flattenable<T>::getFlattenedSize() const {
107 return static_cast<T const*>(this)->T::getFlattenedSize();
108}
109template<typename T>
110inline size_t Flattenable<T>::getFdCount() const {
111 return static_cast<T const*>(this)->T::getFdCount();
112}
113template<typename T>
114inline status_t Flattenable<T>::flatten(
115 void*& buffer, size_t& size, int*& fds, size_t& count) const {
116 return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count);
117}
118template<typename T>
119inline status_t Flattenable<T>::unflatten(
120 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
121 return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count);
122}
123
Mathias Agopian2497a152012-08-12 19:37:16 -0700124/*
125 * LightFlattenable is a protocol allowing object to serialize themselves out
Mathias Agopian6d611a82013-07-29 21:24:40 -0700126 * to a byte-buffer. Because it doesn't handle file-descriptors,
127 * LightFlattenable is usually more size efficient than Flattenable.
Mathias Agopian2497a152012-08-12 19:37:16 -0700128 * LightFlattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -0700129 */
130template <typename T>
131class LightFlattenable {
132public:
133 // returns whether this object always flatten into the same size.
134 // for efficiency, this should always be inline.
135 inline bool isFixedSize() const;
136
137 // returns size in bytes of the flattened object. must be a constant.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700138 inline size_t getFlattenedSize() const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700139
140 // flattens the object into buffer.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700141 inline status_t flatten(void* buffer, size_t size) const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700142
143 // unflattens the object from buffer of given size.
144 inline status_t unflatten(void const* buffer, size_t size);
145};
146
147template <typename T>
148inline bool LightFlattenable<T>::isFixedSize() const {
149 return static_cast<T const*>(this)->T::isFixedSize();
150}
151template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700152inline size_t LightFlattenable<T>::getFlattenedSize() const {
153 return static_cast<T const*>(this)->T::getFlattenedSize();
Mathias Agopian2497a152012-08-12 19:37:16 -0700154}
155template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700156inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
157 return static_cast<T const*>(this)->T::flatten(buffer, size);
Mathias Agopian2497a152012-08-12 19:37:16 -0700158}
159template <typename T>
160inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) {
161 return static_cast<T*>(this)->T::unflatten(buffer, size);
162}
163
164/*
165 * LightFlattenablePod is an implementation of the LightFlattenable protocol
166 * for POD (plain-old-data) objects.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700167 * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
168 * need to implement any methods; obviously Foo must be a POD structure.
Mathias Agopian2497a152012-08-12 19:37:16 -0700169 */
170template <typename T>
171class LightFlattenablePod : public LightFlattenable<T> {
172public:
173 inline bool isFixedSize() const {
174 return true;
175 }
176
Mathias Agopian6d611a82013-07-29 21:24:40 -0700177 inline size_t getFlattenedSize() const {
Mathias Agopian2497a152012-08-12 19:37:16 -0700178 return sizeof(T);
179 }
Mathias Agopian6d611a82013-07-29 21:24:40 -0700180 inline status_t flatten(void* buffer, size_t size) const {
181 if (size < sizeof(T)) return NO_MEMORY;
Mathias Agopian2497a152012-08-12 19:37:16 -0700182 *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this);
183 return NO_ERROR;
184 }
185 inline status_t unflatten(void const* buffer, size_t) {
186 *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer);
187 return NO_ERROR;
188 }
189};
190
191
Mathias Agopiana580e682010-02-11 17:30:52 -0800192}; // namespace android
193
194
195#endif /* ANDROID_UTILS_FLATTENABLE_H */