1 /*
2  * Copyright (c) 2021-2023 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 #include "hks_mem.h"
17 
18 #if defined(HKS_USE_OHOS_MEM)
19 #include "ohos_mem_pool.h"
20 #endif
21 
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "securec.h"
26 
HksMalloc(size_t size)27 void *HksMalloc(size_t size)
28 {
29     if (size == 0 || size > MAX_MALLOC_SIZE) {
30         return NULL;
31     }
32     void *ret = NULL;
33 #if defined(HKS_USE_OHOS_MEM)
34     ret = OhosMalloc(MEM_TYPE_HICHAIN, size);
35 #else
36     ret = malloc(size);
37 #endif
38     if (ret == NULL) {
39         return NULL;
40     }
41     (void)memset_s(ret, size, 0, size);
42     return ret;
43 }
44 
HksMemCmp(const void * ptr1,const void * ptr2,uint32_t size)45 int32_t HksMemCmp(const void *ptr1, const void *ptr2, uint32_t size)
46 {
47     return memcmp(ptr1, ptr2, size);
48 }
49 
HksFreeImpl(void * addr)50 void HksFreeImpl(void *addr)
51 {
52     if (addr == NULL) {
53         return;
54     }
55 #if defined(HKS_USE_OHOS_MEM)
56     OhosFree(addr);
57 #else
58     free(addr);
59 #endif
60 }
61