blob: d80af8f9a0deb6ca5823e63bb6b57d6bbb890589 [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) {
Steven Morelandb8f152d2017-12-18 16:14:13 -080036 static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
Mathias Agopian6d611a82013-07-29 21:24:40 -070037 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) {
Steven Morelandb8f152d2017-12-18 16:14:13 -080042 static_assert(!(N & (N - 1)), "Can only align to a power of 2.");
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) {
Steven Morelandcb0dfbf2019-10-07 17:31:17 -070050 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;
Mathias Agopianddff6232013-08-01 12:47:58 -070056 }
57
Mathias Agopian6d611a82013-07-29 21:24:40 -070058 static void advance(void*& buffer, size_t& size, size_t offset) {
Colin Cross1811d152016-09-26 14:24:48 -070059 buffer = reinterpret_cast<void*>( uintptr_t(buffer) + offset );
Mathias Agopian6d611a82013-07-29 21:24:40 -070060 size -= offset;
61 }
62
63 static void advance(void const*& buffer, size_t& size, size_t offset) {
Colin Cross1811d152016-09-26 14:24:48 -070064 buffer = reinterpret_cast<void const*>( uintptr_t(buffer) + offset );
Mathias Agopian6d611a82013-07-29 21:24:40 -070065 size -= offset;
66 }
67
68 // write a POD structure
69 template<typename T>
70 static void write(void*& buffer, size_t& size, const T& value) {
David Pursell2ebce732016-09-23 09:19:12 -070071 static_assert(std::is_trivially_copyable<T>::value,
72 "Cannot flatten a non-trivially-copyable type");
73 memcpy(buffer, &value, sizeof(T));
Mathias Agopian6d611a82013-07-29 21:24:40 -070074 advance(buffer, size, sizeof(T));
75 }
76
77 // read a POD structure
78 template<typename T>
79 static void read(void const*& buffer, size_t& size, T& value) {
David Pursell2ebce732016-09-23 09:19:12 -070080 static_assert(std::is_trivially_copyable<T>::value,
81 "Cannot unflatten a non-trivially-copyable type");
82 memcpy(&value, buffer, sizeof(T));
Mathias Agopian6d611a82013-07-29 21:24:40 -070083 advance(buffer, size, sizeof(T));
84 }
85};
86
87
Mathias Agopian2497a152012-08-12 19:37:16 -070088/*
Mathias Agopian6d611a82013-07-29 21:24:40 -070089 * The Flattenable protocol allows an object to serialize itself out
Mathias Agopian2497a152012-08-12 19:37:16 -070090 * to a byte-buffer and an array of file descriptors.
Mathias Agopian6d611a82013-07-29 21:24:40 -070091 * Flattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -070092 */
93
Mathias Agopian6d611a82013-07-29 21:24:40 -070094template <typename T>
95class Flattenable {
Mathias Agopiana580e682010-02-11 17:30:52 -080096public:
97 // size in bytes of the flattened object
Mathias Agopian6d611a82013-07-29 21:24:40 -070098 inline size_t getFlattenedSize() const;
Mathias Agopiana580e682010-02-11 17:30:52 -080099
100 // number of file descriptors to flatten
Mathias Agopian6d611a82013-07-29 21:24:40 -0700101 inline size_t getFdCount() const;
Mathias Agopiana580e682010-02-11 17:30:52 -0800102
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).
Mathias Agopianddff6232013-08-01 12:47:58 -0700108 inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
Mathias Agopiana580e682010-02-11 17:30:52 -0800109
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.
Mathias Agopianddff6232013-08-01 12:47:58 -0700117 inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Mathias Agopiana580e682010-02-11 17:30:52 -0800118};
119
Mathias Agopian6d611a82013-07-29 21:24:40 -0700120template<typename T>
121inline size_t Flattenable<T>::getFlattenedSize() const {
122 return static_cast<T const*>(this)->T::getFlattenedSize();
123}
124template<typename T>
125inline size_t Flattenable<T>::getFdCount() const {
126 return static_cast<T const*>(this)->T::getFdCount();
127}
128template<typename T>
129inline 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}
133template<typename T>
134inline 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
Mathias Agopian2497a152012-08-12 19:37:16 -0700139/*
140 * LightFlattenable is a protocol allowing object to serialize themselves out
Mathias Agopian6d611a82013-07-29 21:24:40 -0700141 * to a byte-buffer. Because it doesn't handle file-descriptors,
142 * LightFlattenable is usually more size efficient than Flattenable.
Mathias Agopian2497a152012-08-12 19:37:16 -0700143 * LightFlattenable objects must implement this protocol.
Mathias Agopian2497a152012-08-12 19:37:16 -0700144 */
145template <typename T>
146class LightFlattenable {
147public:
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.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700153 inline size_t getFlattenedSize() const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700154
155 // flattens the object into buffer.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700156 inline status_t flatten(void* buffer, size_t size) const;
Mathias Agopian2497a152012-08-12 19:37:16 -0700157
158 // unflattens the object from buffer of given size.
159 inline status_t unflatten(void const* buffer, size_t size);
160};
161
162template <typename T>
163inline bool LightFlattenable<T>::isFixedSize() const {
164 return static_cast<T const*>(this)->T::isFixedSize();
165}
166template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700167inline size_t LightFlattenable<T>::getFlattenedSize() const {
168 return static_cast<T const*>(this)->T::getFlattenedSize();
Mathias Agopian2497a152012-08-12 19:37:16 -0700169}
170template <typename T>
Mathias Agopian6d611a82013-07-29 21:24:40 -0700171inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
172 return static_cast<T const*>(this)->T::flatten(buffer, size);
Mathias Agopian2497a152012-08-12 19:37:16 -0700173}
174template <typename T>
175inline 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.
Mathias Agopian6d611a82013-07-29 21:24:40 -0700182 * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
183 * need to implement any methods; obviously Foo must be a POD structure.
Mathias Agopian2497a152012-08-12 19:37:16 -0700184 */
185template <typename T>
186class LightFlattenablePod : public LightFlattenable<T> {
187public:
188 inline bool isFixedSize() const {
189 return true;
190 }
191
Mathias Agopian6d611a82013-07-29 21:24:40 -0700192 inline size_t getFlattenedSize() const {
Mathias Agopian2497a152012-08-12 19:37:16 -0700193 return sizeof(T);
194 }
Mathias Agopian6d611a82013-07-29 21:24:40 -0700195 inline status_t flatten(void* buffer, size_t size) const {
196 if (size < sizeof(T)) return NO_MEMORY;
Chris Forbesc46cbcb2017-05-04 09:45:23 -0700197 memcpy(buffer, static_cast<T const*>(this), sizeof(T));
Mathias Agopian2497a152012-08-12 19:37:16 -0700198 return NO_ERROR;
199 }
200 inline status_t unflatten(void const* buffer, size_t) {
Chris Forbesc46cbcb2017-05-04 09:45:23 -0700201 memcpy(static_cast<T*>(this), buffer, sizeof(T));
Mathias Agopian2497a152012-08-12 19:37:16 -0700202 return NO_ERROR;
203 }
204};
205
206
Mathias Agopiana580e682010-02-11 17:30:52 -0800207}; // namespace android
208
209
210#endif /* ANDROID_UTILS_FLATTENABLE_H */