1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ACELITE_ACE_MEM_BASE_H 17 #define OHOS_ACELITE_ACE_MEM_BASE_H 18 19 #include <cstddef> 20 #include <cstdint> 21 22 namespace OHOS { 23 namespace ACELite { 24 /** 25 * @brief The ACEMemHooks struct saving the memory allocating hooks used for ACE 26 */ 27 struct ACEMemHooks { 28 void *(*malloc_func)(size_t sz); 29 void (*free_func)(void *ptr); 30 void *(*calloc_func)(size_t num, size_t size); 31 }; 32 33 /** 34 * @brief InitMemHooks Set the memory hooks used in whole JS framework, malloc and free 35 * must be given and must not be null. the initialization should be called at the very begin 36 * of system start-up, and only can be called once. 37 * 38 * @param [in] hooks: malloc, free and calloc function pointer 39 */ 40 void InitMemHooks(const ACEMemHooks &hooks); 41 42 /** 43 * @brief InitCacheBuf config the cache buffer used for ACE, for example: PSRAM buffer 44 * @param bufAddress the start address of the buffer 45 * @param bufSize the whole length of the buffer, count in bytes 46 */ 47 void InitCacheBuf(uintptr_t bufAddress, size_t bufSize); 48 49 /** 50 * @brief Allocate the required memory space and return a pointer to it 51 * 52 * @param [in] size: size of memory block,in bytes 53 * 54 * @return a pointer to the allocated memory block.if the request fails,return NULL 55 */ 56 void *ace_malloc(size_t size); 57 58 /** 59 * @brief Allocate the required memory space and return a pointer to it 60 * 61 * @param [in] num: number of elements to be allocated 62 * @param [in] size: size of memory block,in bytes 63 * 64 * @return a pointer to the allocated memory block.if the request fails,return NULL 65 */ 66 void *ace_calloc(size_t num, size_t size); 67 68 /** 69 * @brief Free the memory space allocated by malloc(),calloc() or realloc() 70 * 71 * @param [in] ptr: pointer points to a memory block to be freed 72 */ 73 void ace_free(void *ptr); 74 } // namespace ACELite 75 } // namespace OHOS 76 #endif // OHOS_ACELITE_ACE_MEM_BASE_H 77