1a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian/*
2a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
3a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian *
4a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * you may not use this file except in compliance with the License.
6a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * You may obtain a copy of the License at
7a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian *
8a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian *
10a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * See the License for the specific language governing permissions and
14a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian * limitations under the License.
15a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian */
16a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
17a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#ifndef ANDROID_UTILS_FLATTENABLE_H
18a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#define ANDROID_UTILS_FLATTENABLE_H
19a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
20a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
21a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <stdint.h>
222ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell#include <string.h>
23a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <sys/types.h>
24a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#include <utils/Errors.h>
256d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian#include <utils/Debug.h>
26a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
272ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell#include <type_traits>
282ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell
29a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopiannamespace android {
30a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
316d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
326d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianclass FlattenableUtils {
336d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianpublic:
341811d156e9c6b5486d25090ec3e911632dc6edffColin Cross    template<size_t N>
356d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    static size_t align(size_t size) {
366d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) );
376d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        return (size + (N-1)) & ~(N-1);
386d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    }
396d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
401811d156e9c6b5486d25090ec3e911632dc6edffColin Cross    template<size_t N>
41ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian    static size_t align(void const*& buffer) {
426d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        COMPILE_TIME_ASSERT_FUNCTION_SCOPE( !(N & (N-1)) );
431811d156e9c6b5486d25090ec3e911632dc6edffColin Cross        uintptr_t b = uintptr_t(buffer);
441811d156e9c6b5486d25090ec3e911632dc6edffColin Cross        buffer = reinterpret_cast<void*>((uintptr_t(buffer) + (N-1)) & ~(N-1));
451811d156e9c6b5486d25090ec3e911632dc6edffColin Cross        return size_t(uintptr_t(buffer) - b);
466d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    }
476d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
481811d156e9c6b5486d25090ec3e911632dc6edffColin Cross    template<size_t N>
49ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian    static size_t align(void*& buffer) {
50ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian        return align<N>( const_cast<void const*&>(buffer) );
51ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian    }
52ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian
536d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    static void advance(void*& buffer, size_t& size, size_t offset) {
541811d156e9c6b5486d25090ec3e911632dc6edffColin Cross        buffer = reinterpret_cast<void*>( uintptr_t(buffer) + offset );
556d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        size -= offset;
566d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    }
576d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
586d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    static void advance(void const*& buffer, size_t& size, size_t offset) {
591811d156e9c6b5486d25090ec3e911632dc6edffColin Cross        buffer = reinterpret_cast<void const*>( uintptr_t(buffer) + offset );
606d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        size -= offset;
616d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    }
626d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
636d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    // write a POD structure
646d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    template<typename T>
656d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    static void write(void*& buffer, size_t& size, const T& value) {
662ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell        static_assert(std::is_trivially_copyable<T>::value,
672ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell                      "Cannot flatten a non-trivially-copyable type");
682ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell        memcpy(buffer, &value, sizeof(T));
696d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        advance(buffer, size, sizeof(T));
706d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    }
716d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
726d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    // read a POD structure
736d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    template<typename T>
746d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    static void read(void const*& buffer, size_t& size, T& value) {
752ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell        static_assert(std::is_trivially_copyable<T>::value,
762ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell                      "Cannot unflatten a non-trivially-copyable type");
772ebce7384bc7bc6c31122096326c9384d09bdce1David Pursell        memcpy(&value, buffer, sizeof(T));
786d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        advance(buffer, size, sizeof(T));
796d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    }
806d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian};
816d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
826d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
832497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/*
846d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * The Flattenable protocol allows an object to serialize itself out
852497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * to a byte-buffer and an array of file descriptors.
866d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * Flattenable objects must implement this protocol.
872497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */
882497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
896d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate <typename T>
906d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianclass Flattenable {
91a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopianpublic:
92a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // size in bytes of the flattened object
936d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    inline size_t getFlattenedSize() const;
94a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
95a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // number of file descriptors to flatten
966d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    inline size_t getFdCount() const;
97a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
98a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // flattens the object into buffer.
99a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // size should be at least of getFlattenedSize()
100a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // file descriptors are written in the fds[] array but ownership is
101a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // not transfered (ie: they must be dupped by the caller of
102a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // flatten() if needed).
103ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian    inline status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
104a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
105a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // unflattens the object from buffer.
106a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // size should be equal to the value of getFlattenedSize() when the
107a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // object was flattened.
108a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // unflattened file descriptors are found in the fds[] array and
109a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // don't need to be dupped(). ie: the caller of unflatten doesn't
110a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // keep ownership. If a fd is not retained by unflatten() it must be
111a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian    // explicitly closed.
112ddff6230495b66312ad93f652d0c79069a64dbbdMathias Agopian    inline status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
113a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian};
114a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
1156d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T>
1166d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t Flattenable<T>::getFlattenedSize() const {
1176d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    return static_cast<T const*>(this)->T::getFlattenedSize();
1186d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian}
1196d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T>
1206d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t Flattenable<T>::getFdCount() const {
1216d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    return static_cast<T const*>(this)->T::getFdCount();
1226d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian}
1236d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T>
1246d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t Flattenable<T>::flatten(
1256d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        void*& buffer, size_t& size, int*& fds, size_t& count) const {
1266d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    return static_cast<T const*>(this)->T::flatten(buffer, size, fds, count);
1276d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian}
1286d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopiantemplate<typename T>
1296d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t Flattenable<T>::unflatten(
1306d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        void const*& buffer, size_t& size, int const*& fds, size_t& count) {
1316d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    return static_cast<T*>(this)->T::unflatten(buffer, size, fds, count);
1326d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian}
1336d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian
1342497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/*
1352497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable is a protocol allowing object to serialize themselves out
1366d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * to a byte-buffer. Because it doesn't handle file-descriptors,
1376d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * LightFlattenable is usually more size efficient than Flattenable.
1382497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenable objects must implement this protocol.
1392497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */
1402497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T>
1412497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenable {
1422497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic:
1432497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    // returns whether this object always flatten into the same size.
1442497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    // for efficiency, this should always be inline.
1452497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    inline bool isFixedSize() const;
1462497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
1472497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    // returns size in bytes of the flattened object. must be a constant.
1486d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    inline size_t getFlattenedSize() const;
1492497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
1502497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    // flattens the object into buffer.
1516d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    inline status_t flatten(void* buffer, size_t size) const;
1522497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
1532497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    // unflattens the object from buffer of given size.
1542497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    inline status_t unflatten(void const* buffer, size_t size);
1552497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian};
1562497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
1572497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T>
1582497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline bool LightFlattenable<T>::isFixedSize() const {
1592497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    return static_cast<T const*>(this)->T::isFixedSize();
1602497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}
1612497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T>
1626d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline size_t LightFlattenable<T>::getFlattenedSize() const {
1636d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    return static_cast<T const*>(this)->T::getFlattenedSize();
1642497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}
1652497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T>
1666d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopianinline status_t LightFlattenable<T>::flatten(void* buffer, size_t size) const {
1676d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    return static_cast<T const*>(this)->T::flatten(buffer, size);
1682497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}
1692497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T>
1702497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianinline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) {
1712497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    return static_cast<T*>(this)->T::unflatten(buffer, size);
1722497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian}
1732497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
1742497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian/*
1752497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * LightFlattenablePod is an implementation of the LightFlattenable protocol
1762497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian * for POD (plain-old-data) objects.
1776d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * Simply derive from LightFlattenablePod<Foo> to make Foo flattenable; no
1786d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian * need to implement any methods; obviously Foo must be a POD structure.
1792497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian */
1802497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopiantemplate <typename T>
1812497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianclass LightFlattenablePod : public LightFlattenable<T> {
1822497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopianpublic:
1832497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    inline bool isFixedSize() const {
1842497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian        return true;
1852497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    }
1862497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
1876d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    inline size_t getFlattenedSize() const {
1882497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian        return sizeof(T);
1892497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    }
1906d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian    inline status_t flatten(void* buffer, size_t size) const {
1916d611a891d0c818bf3a34a7cad036f3f0064bc4aMathias Agopian        if (size < sizeof(T)) return NO_MEMORY;
192c46cbcbbf9a4c23ab8887d8e4a364a14e63c2b1bChris Forbes        memcpy(buffer, static_cast<T const*>(this), sizeof(T));
1932497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian        return NO_ERROR;
1942497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    }
1952497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    inline status_t unflatten(void const* buffer, size_t) {
196c46cbcbbf9a4c23ab8887d8e4a364a14e63c2b1bChris Forbes        memcpy(static_cast<T*>(this), buffer, sizeof(T));
1972497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian        return NO_ERROR;
1982497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian    }
1992497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian};
2002497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
2012497a1524dd909d0eb933544c94d2c2e9e2c3394Mathias Agopian
202a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian}; // namespace android
203a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
204a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian
205a580e68cc3bea688167eb5e55122bec8e83ab939Mathias Agopian#endif /* ANDROID_UTILS_FLATTENABLE_H */
206