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