• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "osal_mem.h"
10 #include <stdlib.h>
11 #include "securec.h"
12 #include "hdf_log.h"
13 
14 #define HDF_LOG_TAG osal_mem
15 
OsalMemAlloc(size_t size)16 void *OsalMemAlloc(size_t size)
17 {
18     void *buf = NULL;
19 
20     if (size == 0) {
21         HDF_LOGE("%s invalid param", __func__);
22         return NULL;
23     }
24 
25     buf = malloc(size);
26 
27     return buf;
28 }
29 
OsalMemCalloc(size_t size)30 void *OsalMemCalloc(size_t size)
31 {
32     void *buf = NULL;
33 
34     if (size == 0) {
35         HDF_LOGE("%s invalid param", __func__);
36         return NULL;
37     }
38 
39     buf = OsalMemAlloc(size);
40     if (buf != NULL) {
41         (void)memset_s(buf, size, 0, size);
42     }
43 
44     return buf;
45 }
46 
OsalMemAllocAlign(size_t alignment,size_t size)47 void *OsalMemAllocAlign(size_t alignment, size_t size)
48 {
49     void *buf = NULL;
50     int ret;
51 
52     if (size == 0) {
53         HDF_LOGE("%s invalid param", __func__);
54         return NULL;
55     }
56 
57     ret = posix_memalign(&buf, alignment, size);
58     if (ret != 0) {
59         HDF_LOGE("%s memory alloc fail %d", __func__, ret);
60         buf = NULL;
61     }
62 
63     return buf;
64 }
65 
OsalMemFree(void * mem)66 void OsalMemFree(void *mem)
67 {
68     if (mem != NULL) {
69         free(mem);
70     }
71 }
72 
73