blob: 22b811a14bc489a24d0b834117f254f0faead067 [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>
David Pursell2ebce732016-09-23 09:19:12 -070022#include <string.h>
Mathias Agopiana580e682010-02-11 17:30:52 -080023#include <sys/types.h>
24#include <utils/Errors.h>
Mathias Agopian6d611a82013-07-29 21:24:40 -070025#include <utils/Debug.h>
Mathias Agopiana580e682010-02-11 17:30:52 -080026
David Pursell2ebce732016-09-23 09:19:12 -070027#include <type_traits>
28
Mathias Agopiana580e682010-02-11 17:30:52 -080029namespace android {
30
Mathias Agopian6d611a82013-07-29 21:24:40 -070031
32class FlattenableUtils {
33public:
Colin Cross1811d152016-09-26 14:24:48 -070034 template<size_t N>
Mathias Agopian6d611a82013-07-29 21:24:40 -070035 static size_t align(size_t size) {
36 COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) );
37 return (size + (N-1)) & ~(N-1);
38 }
39
Colin Cross1811d152016-09-26 14:24:48 -070040 template<size_t N>
Mathias Agopianddff6232013-08-01 12:47:58 -070041 static size_t align(void const*& buffer) {
Mathias Agopian6d611a82013-07-29 21:24:40 -070042 COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) );
Colin Cross1811d152016-09-26 14:24:48 -070043 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);
Mathias Agopian6d611a82013-07-29 21:24:40 -070046 }
47
Colin Cross1811d152016-09-26 14:24:48 -070048 template<size_t N>
Mathias Agopianddff6232013-08-01 12:47:58 -070049 static size_t align(void*& buffer) {
50 return align<N>( const_cast<void const*&>(buffer) );
51 }
52
Mathias Agopian6d611a82013-07-29 21:24:40 -070053 static void advance(void*& buffer, size_t& size, size_t offset) {
Colin Cross1811d152016-09-26 14:24:48 -070054 buffer = reinterpret_cast<void*>( uintptr_t(buffer) + offset );
Mathias Agopian6d611a82013-07-29 21:24:40 -070055 size -= offset;
56 }
57
58 static void advance(void const*& buffer, size_t& size, size_t offset) {
Colin Cross1811d152016-09-26 14:24:48 -070059 buffer = reinterpret_cast<void const*>( uintptr_t(buffer) + offset );
Mathias Agopian6d611a82013-07-29 21:24:40 -070060 size -= offset;
61 }
62
63 // write a POD structure
64 template<typename T>
65 static void write(void*& buffer, size_t& size, const T& value) {
David Pursell2ebce732016-09-23 09:19:12 -070066 static_assert(std::is_trivially_copyable<T>::value,
67 "Cannot flatten a non-trivially-copyable type");
68 memcpy(buffer, &value, sizeof(T));
Mathias Agopian6d611a82013-07-29 21:24:40 -070069 advance(buffer, size, sizeof(T));
70 }
71
72 // read a POD structure
73 template<typename T>
74 static void read(void const*& buffer, size_t& size, T& value) {
David Pursell2ebce732016-09-23 09:19:12 -070075 static_assert(std::is_trivially_copyable<T>::value,
76 "Cannot unflatten a non-trivially-copyable type");
77 memcpy(&value, buffer, sizeof(T));
Mathias Agopian6d611a82013-07-29 21:24:40 -070078 advance(buffer, size, sizeof(T));
79 }
80};
81
82
Mathias Agopian2497a152012-08-12 19:37:16 -070083/*
Mathias Agopian6d611a82013-07-29 21:24:40 -070084 * The Flattenable protocol allows an object to serialize itself out
Mathias Agopian2497a152012-08-12 19:37:16 -070085 * to a byte-buffer and an array of file descriptors.
Mathias Agopian6d611a82013-07-29 21:24:40 -070086 * Flattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -070087 */
88
Mathias Agopian6d611a82013-07-29 21:24:40 -070089template <typename T>
90class Flattenable {
Mathias Agopiana580e682010-02-11 17:30:52 -080091public:
92 // size in bytes of the flattened object
Mathias Agopian6d611a82013-07-29 21:24:40 -070093 inline size_t getFlattenedSize() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080094
95 // number of file descriptors to flatten
Mathias Agopian6d611a82013-07-29 21:24:40 -070096 inline size_t getFdCount() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080097
98 // flattens the object into buffer.
99 // size should be at least of getFlattenedSize()
100 // file descriptors are written in the fds[] array but ownership is
101 // not transfered (ie: they must be dupped by the caller of
102 // flatten() if needed).
Mathias Agopianddff6232013-08-01 12:47:58 -0700103 inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
Mathias Agopiana580e682010-02-11 17:30:52 -0800104
105 // unflattens the object from buffer.
106 // size should be equal to the value of getFlattenedSize() when the
107 // object was flattened.
108 // unflattened file descriptors are found in the fds[] array and
109 // don't need to be dupped(). ie: the caller of unflatten doesn't
110 // keep ownership. If a fd is not retained by unflatten() it must be
111 // explicitly closed.
Mathias Agopianddff6232013-08-01 12:47:58 -0700112 inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Mathias Agopiana580e682010-02-11 17:30:52 -0800113};
114
Mathias Agopian6d611a82013-07-29 21:24:40 -0700115template<typename T>
116inline size_t Flattenable<T>::getFlattenedSize() const {
117 return static_cast<T const*>(this)->T::getFlattenedSize();
118}
119template<typename T>
120inline size_t Flattenable<T>::getFdCount() const {
121 return static_cast<T const*>(this)->T::getFdCount();
122}
123template<typename T>
124inline status_t Flattenable<T>::flatten(
125 void*& buffer, size_t& size, int*& fds, size_t& count) const {
126 return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count);
127}
128template<typename T>
129inline status_t Flattenable<T>::unflatten(
130 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
131 return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count);
132}
133
Mathias Agopian2497a152012-08-12 19:37:16 -0700134/*
135 * LightFlattenable is a protocol allowing object to serialize themselves out
Mathias Agopian6d611a82013-07-29 21:24:40 -0700136 * to a byte-buffer. Because it doesn't handle file-descriptors,
137 * LightFlattenable is usually more size efficient than Flattenable.
Mathias Agopian2497a152012-08-12 19:37:16 -0700138 * LightFlattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -0700139 */
140template <typename T>
141class LightFlattenable {
142public:
143 // returns whether this object always flatten into the same size.
144 // for efficiency, this should always be inline.
145 inline bool isFixedSize() const;
146
147 // returns size in bytes of the flattened object. must be a constant.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700148 inline size_t getFlattenedSize() const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700149
150 // flattens the object into buffer.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700151 inline status_t flatten(void* buffer, size_t size) const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700152
153 // unflattens the object from buffer of given size.
154 inline status_t unflatten(void const* buffer, size_t size);
155};
156
157template <typename T>
158inline bool LightFlattenable<T>::isFixedSize() const {
159 return static_cast<T const*>(this)->T::isFixedSize();
160}
161template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700162inline size_t LightFlattenable<T>::getFlattenedSize() const {
163 return static_cast<T const*>(this)->T::getFlattenedSize();
Mathias Agopian2497a152012-08-12 19:37:16 -0700164}
165template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700166inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
167 return static_cast<T const*>(this)->T::flatten(buffer, size);
Mathias Agopian2497a152012-08-12 19:37:16 -0700168}
169template <typename T>
170inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) {
171 return static_cast<T*>(this)->T::unflatten(buffer, size);
172}
173
174/*
175 * LightFlattenablePod is an implementation of the LightFlattenable protocol
176 * for POD (plain-old-data) objects.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700177 * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
178 * need to implement any methods; obviously Foo must be a POD structure.
Mathias Agopian2497a152012-08-12 19:37:16 -0700179 */
180template <typename T>
181class LightFlattenablePod : public LightFlattenable<T> {
182public:
183 inline bool isFixedSize() const {
184 return true;
185 }
186
Mathias Agopian6d611a82013-07-29 21:24:40 -0700187 inline size_t getFlattenedSize() const {
Mathias Agopian2497a152012-08-12 19:37:16 -0700188 return sizeof(T);
189 }
Mathias Agopian6d611a82013-07-29 21:24:40 -0700190 inline status_t flatten(void* buffer, size_t size) const {
191 if (size < sizeof(T)) return NO_MEMORY;
Mathias Agopian2497a152012-08-12 19:37:16 -0700192 *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this);
193 return NO_ERROR;
194 }
195 inline status_t unflatten(void const* buffer, size_t) {
196 *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer);
197 return NO_ERROR;
198 }
199};
200
201
Mathias Agopiana580e682010-02-11 17:30:52 -0800202}; // namespace android
203
204
205#endif /* ANDROID_UTILS_FLATTENABLE_H */