Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | /* |
| 3 | * Copyright (C) 2017 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | // Base macros for all layout structures. |
| 19 | |
| 20 | #include <type_traits> |
| 21 | #include "common/vsoc/shm/version.h" |
| 22 | |
Greg Hartman | aca3bc7 | 2017-07-17 16:03:45 -0700 | [diff] [blame] | 23 | #define ASSERT_SHM_COMPATIBLE(T, R) \ |
| 24 | static_assert(sizeof(T) == vsoc::layout::version_info::R::T##_size, \ |
| 25 | "size changed, update the version"); \ |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 26 | static_assert(std::is_trivial<T>(), "Class uses features that are unsafe") |
| 27 | |
| 28 | #define ASSERT_SHM_CONSTANT_VALUE(T, R) \ |
| 29 | static_assert(T == vsoc::layout::version_info::R::constant_values::T, \ |
| 30 | "Constant value changed") |
| 31 | |
| 32 | namespace vsoc { |
| 33 | namespace layout { |
Jorge E. Moreira | c615b45 | 2017-08-07 17:22:44 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Base class that should be used for all shared memory objects |
| 37 | */ |
| 38 | class Base { |
| 39 | }; |
| 40 | ASSERT_SHM_COMPATIBLE(Base, multi_region); |
| 41 | |
Greg Hartman | ffe99fa | 2017-07-14 14:46:19 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Memory is shared between Guest and Host kernels. In some cases we need |
| 44 | * flag to indicate which side we're on. In those cases we'll use this |
| 45 | * simple structure. |
| 46 | * |
| 47 | * These are carefully formatted to make Guest and Host a bitfield. |
| 48 | */ |
Jorge E. Moreira | c615b45 | 2017-08-07 17:22:44 -0700 | [diff] [blame] | 49 | struct Sides : public Base { |
Greg Hartman | ffe99fa | 2017-07-14 14:46:19 -0700 | [diff] [blame] | 50 | static const uint32_t NoSides = 0; |
| 51 | static const uint32_t Guest = 1; |
| 52 | static const uint32_t Host = 2; |
| 53 | static const uint32_t Both = 3; |
Greg Hartman | a4ff248 | 2017-10-03 16:35:00 -0700 | [diff] [blame^] | 54 | #ifdef CUTTLEFISH_HOST |
Greg Hartman | ffe99fa | 2017-07-14 14:46:19 -0700 | [diff] [blame] | 55 | static const uint32_t OurSide = Host; |
| 56 | static const uint32_t Peer = Guest; |
Greg Hartman | a4ff248 | 2017-10-03 16:35:00 -0700 | [diff] [blame^] | 57 | #else |
| 58 | static const uint32_t OurSide = Guest; |
| 59 | static const uint32_t Peer = Host; |
Greg Hartman | ffe99fa | 2017-07-14 14:46:19 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | uint32_t value_; |
| 63 | }; |
| 64 | ASSERT_SHM_COMPATIBLE(Sides, multi_region); |
| 65 | |
Jorge E. Moreira | c615b45 | 2017-08-07 17:22:44 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Base class for all region layout structures. |
| 68 | */ |
| 69 | class RegionLayout : public Base { |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 70 | }; |
Jorge E. Moreira | c615b45 | 2017-08-07 17:22:44 -0700 | [diff] [blame] | 71 | ASSERT_SHM_COMPATIBLE(RegionLayout, multi_region); |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 72 | |
Greg Hartman | aca3bc7 | 2017-07-17 16:03:45 -0700 | [diff] [blame] | 73 | } // namespace layout |
| 74 | } // namespace vsoc |