• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 UTIL_CHRE_NESTED_DATA_PTR_H_
18 #define UTIL_CHRE_NESTED_DATA_PTR_H_
19 
20 #include <string.h>
21 #include <type_traits>
22 
23 namespace chre {
24 
25 /**
26  * Template which provides type punning capability between the template type and
27  * void*. Note that this void* representation must not be dereferenced - it is
28  * only safe as a temporary representation of the underlying type, for example
29  * passed through to a callback which accepts an opaque void* parameter.
30  */
31 template <typename DataType>
32 union NestedDataPtr {
33   static_assert(sizeof(DataType) <= sizeof(void *),
34                 "Requested data type must fit in a void* to use NestedDataPtr");
35   // If the sizeof() check passes, then this is unlikely to be an issue, and in
36   // many usage scenarios this wouldn't be an issue (e.g. reinterpreting a value
37   // stored in a register), but it's included here just to be safe.
38   static_assert(alignof(DataType) <= alignof(void *),
39                 "Additional alignment in NestedDataPtr can't be guaranteed");
40   static_assert(std::is_trivially_copyable<DataType>::value,
41                 "Only trivially copyable types may be used in NestedDataPtr");
42 
43   NestedDataPtr() = default;
44 
NestedDataPtr(DataType nestedData)45   explicit NestedDataPtr(DataType nestedData) : data(nestedData) {}
NestedDataPtr(void * ptr)46   explicit NestedDataPtr(void *ptr) {
47     // We use memcpy here and in the void* conversion operator, as the C++11
48     // language standard defines that accessing any field of a union other than
49     // most recently set value is undefined behavior, unless it's a structure
50     // with a common prefix to the active field. Most compilers (e.g. GCC,
51     // clang) allow for this anyways as it's permitted in C, but to avoid the UB
52     // we do the conversion via memcpy. Note that compilers will recognize this
53     // as a simple store operation and produce equivalent assembly as if we were
54     // assigning to mUnusedPtr.
55     memcpy(&data, &ptr, sizeof(ptr));
56   }
57 
58   // Implicit conversions
DataType()59   operator DataType() const {
60     return data;
61   }
62 
63   operator void *() const {
64     void *result;
65     static_assert(sizeof(data) <= sizeof(result), "Broken assumption");
66     memcpy(&result, &data, sizeof(data));
67     return result;
68   }
69 
70   DataType data;
71 
72  private:
73   // Here to force that this union has at least the alignment + size of a
74   // pointer, but we don't access it directly
75   void *mUnusedPtr;
76 };
77 
78 }  // namespace chre
79 
80 #endif  // UTIL_CHRE_NESTED_DATA_PTR_H_