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
SoftBusMalloc(unsigned int size)20 void *SoftBusMalloc(unsigned int size)
21 {
22 if (size > MAX_MALLOC_SIZE) {
23 return NULL;
24 }
25 return malloc(size);
26 }
27
SoftBusCalloc(unsigned int size)28 void *SoftBusCalloc(unsigned int size)
29 {
30 void *tmp = SoftBusMalloc(size);
31 if (tmp == NULL) {
32 return NULL;
33 }
34
35 errno_t err = memset_s(tmp, size, 0, size);
36 if (err != EOK) {
37 SoftBusFree(tmp);
38 return NULL;
39 }
40 return tmp;
41 }
42
SoftBusFree(void * pt)43 void SoftBusFree(void *pt)
44 {
45 if (pt == NULL) {
46 return;
47 }
48 free(pt);
49 }
50
SoftBusClearFree(void * pt,unsigned int size)51 void SoftBusClearFree(void *pt, unsigned int size)
52 {
53 if (pt == NULL) {
54 return;
55 }
56 (void)memset_s(pt, size, 0, size);
57 SoftBusFree(pt);
58 }