• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HKS_MEM_H
17 #define HKS_MEM_H
18 
19 #ifdef __cplusplus
20 #include <cstdint>
21 #include <cstdlib>
22 
23 #define HKS_NULL_POINTER nullptr
24 #else
25 #include <stdint.h>
26 #include <stdlib.h>
27 
28 #define HKS_NULL_POINTER NULL
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 void *HksMalloc(size_t size);
36 int32_t HksMemCmp(const void *ptr1, const void *ptr2, uint32_t size);
37 
38 #define SELF_FREE_PTR(PTR, FREE_FUNC) \
39 { \
40     if ((PTR) != HKS_NULL_POINTER) { \
41         FREE_FUNC(PTR); \
42         (PTR) = HKS_NULL_POINTER; \
43     } \
44 }
45 
46 #define HKS_FREE_PTR(p) SELF_FREE_PTR(p, free)
47 
48 #define HksFree(p) SELF_FREE_PTR(p, free)
49 
50 #define HKS_FREE_BLOB(blob) do { \
51     if ((blob).data != HKS_NULL_POINTER) { \
52         free((blob).data); \
53         (blob).data = HKS_NULL_POINTER; \
54     } \
55     (blob).size = 0; \
56 } while (0)
57 
58 #define HKS_MEMSET_FREE_PTR(ptr, size) \
59 { \
60     if ((ptr) != HKS_NULL_POINTER) { \
61         (void)memset_s((ptr), (size), 0, (size)); \
62         free(ptr); \
63         (ptr) = HKS_NULL_POINTER; \
64     } \
65 }
66 
67 #define HKS_MEMSET_FREE_BLOB(blob) do { \
68     if ((blob).data != HKS_NULL_POINTER) { \
69         (void)memset_s((blob).data, (blob).size, 0, (blob).size); \
70         free((blob).data); \
71         (blob).data = HKS_NULL_POINTER; \
72     } \
73     (blob).size = 0; \
74 } while (0)
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif /* HKS_MEM_H */
81