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 #include "ace_mem_base.h"
16
17 #include <cstdlib>
18 #include "cache_manager.h"
19
20 namespace OHOS {
21 namespace ACELite {
22 /**
23 * @brief g_customHookSet
24 * Record the memory hooks used for ACE, only can be setup once when system start-up.
25 * The standard memory allocating methods are used as default.
26 */
27 static ACEMemHooks g_memoryHooks = {malloc, free, calloc};
28 /**
29 * @brief g_customHookSet flag for representing if the customer hooks are set
30 */
31 static bool g_customHookSet = false;
32
InitMemHooks(const ACEMemHooks & hooks)33 void InitMemHooks(const ACEMemHooks &hooks)
34 {
35 if (g_customHookSet) {
36 // only can be configured once
37 return;
38 }
39 if ((hooks.malloc_func == nullptr) || (hooks.free_func == nullptr)) {
40 // the malloc and free must be given
41 return;
42 }
43 g_memoryHooks.malloc_func = hooks.malloc_func;
44 g_memoryHooks.free_func = hooks.free_func;
45 g_memoryHooks.calloc_func = hooks.calloc_func;
46 // set the flag
47 g_customHookSet = true;
48 }
49
InitCacheBuf(uintptr_t bufAddress,size_t bufSize)50 void InitCacheBuf(uintptr_t bufAddress, size_t bufSize)
51 {
52 CacheManager::GetInstance().SetupCacheMemInfo(bufAddress, bufSize);
53 }
54
ace_malloc(size_t size)55 void *ace_malloc(size_t size)
56 {
57 return g_memoryHooks.malloc_func(size);
58 }
59
ace_calloc(size_t num,size_t size)60 void *ace_calloc(size_t num, size_t size)
61 {
62 return (g_memoryHooks.calloc_func != nullptr) ? (g_memoryHooks.calloc_func(num, size)) : nullptr;
63 }
64
ace_free(void * ptr)65 void ace_free(void *ptr)
66 {
67 g_memoryHooks.free_func(ptr);
68 }
69 } // namespace ACELite
70 } // namespace OHOS
71