1 /* 2 * Copyright (C) 2017 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_UTIL_CONTAINER_SUPPORT_H_ 18 #define CHRE_UTIL_CONTAINER_SUPPORT_H_ 19 20 /** 21 * @file Provides replacements for macros and functions that are normally 22 * provided by the CHRE framework implementation. These portable implementations 23 * are implemented using the CHRE API rather than private system APIs. 24 */ 25 26 #if defined CHRE_IS_NANOAPP_BUILD 27 28 #include "chre/util/nanoapp/assert.h" 29 #include "chre_api/chre.h" 30 31 #ifdef CHRE_STANDALONE_POSIX_ALIGNED_ALLOC 32 #include <stdlib.h> 33 #else 34 #include "chre/util/always_false.h" 35 #endif // CHRE_STANDALONE_POSIX_ALIGNED_ALLOC 36 37 namespace chre { 38 39 /** 40 * Provides the memoryAlloc function that is normally provided by the CHRE 41 * runtime. It maps into chreHeapAlloc. 42 * 43 * @param size the size of the allocation to make. 44 * @return a pointer to allocated memory or nullptr if allocation failed. 45 */ memoryAlloc(size_t size)46inline void *memoryAlloc(size_t size) { 47 return chreHeapAlloc(static_cast<uint32_t>(size)); 48 } 49 50 template <typename T> memoryAlignedAlloc()51inline T *memoryAlignedAlloc() { 52 #ifdef CHRE_STANDALONE_POSIX_ALIGNED_ALLOC 53 void *ptr; 54 int result = posix_memalign(&ptr, alignof(T), sizeof(T)); 55 if (result != 0) { 56 ptr = nullptr; 57 } 58 return static_cast<T *>(ptr); 59 #else 60 static_assert(AlwaysFalse<T>::value, 61 "memoryAlignedAlloc is unsupported on this platform"); 62 return nullptr; 63 #endif // CHRE_STANDALONE_POSIX_ALIGNED_ALLOC 64 } 65 66 /** 67 * Provides the memoryFree function that is normally provided by the CHRE 68 * runtime. It maps into chreHeapFree. 69 * 70 * @param pointer the allocation to release. 71 */ memoryFree(void * pointer)72inline void memoryFree(void *pointer) { 73 chreHeapFree(pointer); 74 } 75 76 } // namespace chre 77 78 #elif defined CHRE_IS_HOST_BUILD 79 80 #include "chre/util/host/assert.h" 81 82 namespace chre { 83 memoryAlloc(size_t size)84inline void *memoryAlloc(size_t size) { 85 return malloc(size); 86 } 87 88 template <typename T> memoryAlignedAlloc()89inline T *memoryAlignedAlloc() { 90 void *ptr; 91 int result = posix_memalign(&ptr, alignof(T), sizeof(T)); 92 if (result != 0) { 93 ptr = nullptr; 94 } 95 return static_cast<T *>(ptr); 96 } 97 memoryFree(void * pointer)98inline void memoryFree(void *pointer) { 99 free(pointer); 100 } 101 102 } // namespace chre 103 104 #else 105 #include "chre/platform/assert.h" 106 #include "chre/platform/memory.h" 107 #endif // CHRE_IS_NANOAPP_BUILD 108 109 #endif // CHRE_UTIL_CONTAINER_SUPPORT_H_ 110