Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 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 | |
| 21 | #include <stdint.h> |
David Pursell | 2ebce73 | 2016-09-23 09:19:12 -0700 | [diff] [blame] | 22 | #include <string.h> |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <utils/Errors.h> |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 25 | #include <utils/Debug.h> |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 26 | |
David Pursell | 2ebce73 | 2016-09-23 09:19:12 -0700 | [diff] [blame] | 27 | #include <type_traits> |
| 28 | |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 31 | |
| 32 | class FlattenableUtils { |
| 33 | public: |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 34 | template<size_t N> |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 35 | static size_t align(size_t size) { |
Steven Moreland | b8f152d | 2017-12-18 16:14:13 -0800 | [diff] [blame] | 36 | static_assert(!(N & (N - 1)), "Can only align to a power of 2."); |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 37 | return (size + (N-1)) & ~(N-1); |
| 38 | } |
| 39 | |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 40 | template<size_t N> |
Mathias Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 41 | static size_t align(void const*& buffer) { |
Steven Moreland | b8f152d | 2017-12-18 16:14:13 -0800 | [diff] [blame] | 42 | static_assert(!(N & (N - 1)), "Can only align to a power of 2."); |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 43 | 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 Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 48 | template<size_t N> |
Mathias Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 49 | static size_t align(void*& buffer) { |
Steven Moreland | cb0dfbf | 2019-10-07 17:31:17 -0700 | [diff] [blame^] | 50 | 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 Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 58 | static void advance(void*& buffer, size_t& size, size_t offset) { |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 59 | buffer = reinterpret_cast<void*>( uintptr_t(buffer) + offset ); |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 60 | size -= offset; |
| 61 | } |
| 62 | |
| 63 | static void advance(void const*& buffer, size_t& size, size_t offset) { |
Colin Cross | 1811d15 | 2016-09-26 14:24:48 -0700 | [diff] [blame] | 64 | buffer = reinterpret_cast<void const*>( uintptr_t(buffer) + offset ); |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 65 | 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 Pursell | 2ebce73 | 2016-09-23 09:19:12 -0700 | [diff] [blame] | 71 | static_assert(std::is_trivially_copyable<T>::value, |
| 72 | "Cannot flatten a non-trivially-copyable type"); |
| 73 | memcpy(buffer, &value, sizeof(T)); |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 74 | 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 Pursell | 2ebce73 | 2016-09-23 09:19:12 -0700 | [diff] [blame] | 80 | static_assert(std::is_trivially_copyable<T>::value, |
| 81 | "Cannot unflatten a non-trivially-copyable type"); |
| 82 | memcpy(&value, buffer, sizeof(T)); |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 83 | advance(buffer, size, sizeof(T)); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 88 | /* |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 89 | * The Flattenable protocol allows an object to serialize itself out |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 90 | * to a byte-buffer and an array of file descriptors. |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 91 | * Flattenable objects must implement this protocol. |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 94 | template <typename T> |
| 95 | class Flattenable { |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 96 | public: |
| 97 | // size in bytes of the flattened object |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 98 | inline size_t getFlattenedSize() const; |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 99 | |
| 100 | // number of file descriptors to flatten |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 101 | inline size_t getFdCount() const; |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 102 | |
| 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 Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 108 | inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 109 | |
| 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 Agopian | ddff623 | 2013-08-01 12:47:58 -0700 | [diff] [blame] | 117 | inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 120 | template<typename T> |
| 121 | inline size_t Flattenable<T>::getFlattenedSize() const { |
| 122 | return static_cast<T const*>(this)->T::getFlattenedSize(); |
| 123 | } |
| 124 | template<typename T> |
| 125 | inline size_t Flattenable<T>::getFdCount() const { |
| 126 | return static_cast<T const*>(this)->T::getFdCount(); |
| 127 | } |
| 128 | template<typename T> |
| 129 | inline 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 | } |
| 133 | template<typename T> |
| 134 | inline 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 Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 139 | /* |
| 140 | * LightFlattenable is a protocol allowing object to serialize themselves out |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 141 | * to a byte-buffer. Because it doesn't handle file-descriptors, |
| 142 | * LightFlattenable is usually more size efficient than Flattenable. |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 143 | * LightFlattenable objects must implement this protocol. |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 144 | */ |
| 145 | template <typename T> |
| 146 | class LightFlattenable { |
| 147 | public: |
| 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 Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 153 | inline size_t getFlattenedSize() const; |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 154 | |
| 155 | // flattens the object into buffer. |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 156 | inline status_t flatten(void* buffer, size_t size) const; |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 157 | |
| 158 | // unflattens the object from buffer of given size. |
| 159 | inline status_t unflatten(void const* buffer, size_t size); |
| 160 | }; |
| 161 | |
| 162 | template <typename T> |
| 163 | inline bool LightFlattenable<T>::isFixedSize() const { |
| 164 | return static_cast<T const*>(this)->T::isFixedSize(); |
| 165 | } |
| 166 | template <typename T> |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 167 | inline size_t LightFlattenable<T>::getFlattenedSize() const { |
| 168 | return static_cast<T const*>(this)->T::getFlattenedSize(); |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 169 | } |
| 170 | template <typename T> |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 171 | inline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const { |
| 172 | return static_cast<T const*>(this)->T::flatten(buffer, size); |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 173 | } |
| 174 | template <typename T> |
| 175 | inline 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 Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 182 | * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no |
| 183 | * need to implement any methods; obviously Foo must be a POD structure. |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 184 | */ |
| 185 | template <typename T> |
| 186 | class LightFlattenablePod : public LightFlattenable<T> { |
| 187 | public: |
| 188 | inline bool isFixedSize() const { |
| 189 | return true; |
| 190 | } |
| 191 | |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 192 | inline size_t getFlattenedSize() const { |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 193 | return sizeof(T); |
| 194 | } |
Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 195 | inline status_t flatten(void* buffer, size_t size) const { |
| 196 | if (size < sizeof(T)) return NO_MEMORY; |
Chris Forbes | c46cbcb | 2017-05-04 09:45:23 -0700 | [diff] [blame] | 197 | memcpy(buffer, static_cast<T const*>(this), sizeof(T)); |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 198 | return NO_ERROR; |
| 199 | } |
| 200 | inline status_t unflatten(void const* buffer, size_t) { |
Chris Forbes | c46cbcb | 2017-05-04 09:45:23 -0700 | [diff] [blame] | 201 | memcpy(static_cast<T*>(this), buffer, sizeof(T)); |
Mathias Agopian | 2497a15 | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 202 | return NO_ERROR; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | |
Mathias Agopian | a580e68 | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 207 | }; // namespace android |
| 208 | |
| 209 | |
| 210 | #endif /* ANDROID_UTILS_FLATTENABLE_H */ |