Home | History | Annotate | Download | only in shm
      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 #include <stdint.h>
     19 #include <type_traits>
     20 
     21 // Base macros for all layout structures.
     22 // ShmTypeValidator provides meaningful information about the type size
     23 // mismatch in compilation error messages, eg.
     24 //
     25 // error:
     26 //    static_assert failed "Class size changed, update the layout_size field"
     27 //    static_assert(Current == Expected,
     28 // note: in instantiation of template class
     29 //    'ShmTypeValidator<vsoc::layout::myclass::ClassName>'
     30 //    requested here ASSERT_SHM_COMPATIBLE(ClassName);
     31 //
     32 template <typename Type, size_t expected_size = Type::layout_size>
     33 struct ShmTypeValidator {
     34   static_assert(sizeof(Type) == expected_size,
     35                 "Class size changed, update the layout_size field");
     36   static_assert(std::is_trivial<Type>(), "Class uses features that are unsafe");
     37   static constexpr bool valid =
     38       sizeof(Type) == expected_size && std::is_trivial<Type>();
     39 };
     40 
     41 #define ASSERT_SHM_COMPATIBLE(T)            \
     42   static_assert(ShmTypeValidator<T>::valid, \
     43                 "Compilation error. Please fix above errors and retry.")
     44 
     45 namespace vsoc {
     46 namespace layout {
     47 
     48 /**
     49  * Memory is shared between Guest and Host kernels. In some cases we need
     50  * flag to indicate which side we're on. In those cases we'll use this
     51  * simple structure.
     52  *
     53  * These are carefully formatted to make Guest and Host a bitfield.
     54  */
     55 enum Sides : uint32_t {
     56   NoSides = 0,
     57   Guest = 1,
     58   Host = 2,
     59   Both = 3,
     60 #ifdef CUTTLEFISH_HOST
     61   OurSide = Host,
     62   Peer = Guest
     63 #else
     64   OurSide = Guest,
     65   Peer = Host
     66 #endif
     67 };
     68 // Enums can't have static members, so can't use the macro here.
     69   static_assert(ShmTypeValidator<Sides, 4>::valid,
     70               "Compilation error. Please fix above errors and retry.");
     71 
     72 /**
     73  * Base class for all region layout structures.
     74  */
     75 class RegionLayout {
     76 public:
     77   static constexpr size_t layout_size = 1;
     78 };
     79 ASSERT_SHM_COMPATIBLE(RegionLayout);
     80 
     81 }  // namespace layout
     82 }  // namespace vsoc
     83