1 /*
2  * Copyright (C) 2020 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 CHRE_PLATFORM_SHARED_MEMORY_H_
18 #define CHRE_PLATFORM_SHARED_MEMORY_H_
19 
20 #include <cstddef>
21 #include <type_traits>
22 #include <utility>
23 
24 namespace chre {
25 
26 /**
27  * Allocates space for a nanoapp binary to be mapped into. Memory allocation
28  * should come from the lowest power memory region available.  If alignment is
29  * left as 0, the default alignment for the platform should be used.
30  *
31  * @param size The size of the binary that will be placed in the memory region
32  * @param alignment The alignment boundary the nanoapp binary requires
33  */
34 void *nanoappBinaryAlloc(size_t size, size_t alignment = 0);
35 
36 /**
37  * Allocates space for a nanoapp binary to be mapped into. Memory allocation
38  * should come from a high capacity memory region. If DRAM or another large
39  * capacity region is not available, this will allocate from the same memory
40  * region as nanoappBinaryAlloc. If alignment is left as 0, the default
41  * alignment for the platform should be used.
42  *
43  * @param size The size of the binary that will be placed in the memory region
44  * @param alignment The alignment boundary the nanoapp binary requires
45  */
46 void *nanoappBinaryDramAlloc(size_t size, size_t alignment = 0);
47 
48 /**
49  * Memory free from memory allocated using nanoappBinaryAlloc. The semantics
50  * are the same as free.
51  *
52  * @param pointer Pointer to data returned from nanoappBinaryAlloc
53  */
54 void nanoappBinaryFree(void *pointer);
55 
56 /**
57  * Memory free from memory allocated using nanoappBinaryDramAlloc. The semantics
58  * are the same as free.
59  *
60  * @param pointer Pointer to data returned from nanoappBinaryDramAlloc
61  */
62 void nanoappBinaryDramFree(void *pointer);
63 
64 /**
65  * Memory allocation specifically using the DRAM heap. The semantics are the
66  * same as malloc.
67  *
68  * If DRAM or another large memory capacity region is not available, this will
69  * allocate from the same memory region as memoryAlloc.
70  */
71 void *memoryAllocDram(size_t size);
72 
73 /**
74  * Memory free from memory allocated using the DRAM heap. The semantics are the
75  * same as free.
76  *
77  * If DRAM or another large memory capacity region is not available, this will
78  * free from the same memory region as memoryFree.
79  */
80 void memoryFreeDram(void *pointer);
81 
82 /**
83  * Ensures memory allocated through memoryAllocDram can be utilized. If memory
84  * allocated through memoryAllocDram is always available, this method can be a
85  * no-op.
86  *
87  * This must be support being invoked from multiple threads.
88  */
89 void forceDramAccess();
90 
91 /**
92  * Removes CHRE's vote to keep DRAM accessible. This must only be called when
93  * CHRE is idle.
94  */
95 void removeDramAccessVote();
96 
97 /**
98  * Allocates memory in DRAM for an object of size T and constructs the object in
99  * the newly allocated object by forwarding the provided parameters.
100  */
101 template <typename T, typename... Args>
memoryAllocDram(Args &&...args)102 inline T *memoryAllocDram(Args &&... args) {
103   auto *storage = static_cast<T *>(memoryAllocDram(sizeof(T)));
104   if (storage != nullptr) {
105     new (storage) T(std::forward<Args>(args)...);
106   }
107 
108   return storage;
109 }
110 
111 }  // namespace chre
112 
113 #endif  // CHRE_PLATFORM_SHARED_MEMORY_H_
114