1 /*
2 * Copyright (c) 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 #include "softbus_adapter_mem.h"
17
18 #include <securec.h>
19
20 #if defined(OHOS_MEM)
21 #include "ohos_mem_pool.h"
22 #endif
23
SoftBusMalloc(unsigned int size)24 void *SoftBusMalloc(unsigned int size)
25 {
26 if (size > MAX_MALLOC_SIZE) {
27 return NULL;
28 }
29
30 #if defined(OHOS_MEM)
31 void *tmp = OhosMalloc(MEM_TYPE_SOFTBUS_LSRAM, size);
32 #else
33 void *tmp = malloc(size);
34 #endif
35 return tmp;
36 }
37
SoftBusCalloc(unsigned int size)38 void *SoftBusCalloc(unsigned int size)
39 {
40 void *tmp = SoftBusMalloc(size);
41 if (tmp == NULL) {
42 return NULL;
43 }
44
45 errno_t err = memset_s(tmp, size, 0, size);
46 if (err != EOK) {
47 SoftBusFree(tmp);
48 return NULL;
49 }
50 return tmp;
51 }
52
SoftBusFree(void *pt)53 void SoftBusFree(void *pt)
54 {
55 if (pt == NULL) {
56 return;
57 }
58 #if defined(OHOS_MEM)
59 OhosFree(pt);
60 #else
61 free(pt);
62 #endif
63 }
64
SoftBusClearFree(void *pt, unsigned int size)65 void SoftBusClearFree(void *pt, unsigned int size)
66 {
67 if (pt == NULL) {
68 return;
69 }
70 (void)memset_s(pt, size, 0, size);
71 SoftBusFree(pt);
72 }