1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#include "softbus_adapter_mem.h"
17060ff233Sopenharmony_ci
18060ff233Sopenharmony_ci#include <securec.h>
19060ff233Sopenharmony_ci
20060ff233Sopenharmony_civoid *SoftBusMalloc(unsigned int size)
21060ff233Sopenharmony_ci{
22060ff233Sopenharmony_ci    if (size > MAX_MALLOC_SIZE) {
23060ff233Sopenharmony_ci        return NULL;
24060ff233Sopenharmony_ci    }
25060ff233Sopenharmony_ci    return malloc(size);
26060ff233Sopenharmony_ci}
27060ff233Sopenharmony_ci
28060ff233Sopenharmony_civoid *SoftBusCalloc(unsigned int size)
29060ff233Sopenharmony_ci{
30060ff233Sopenharmony_ci    void *tmp = SoftBusMalloc(size);
31060ff233Sopenharmony_ci    if (tmp == NULL) {
32060ff233Sopenharmony_ci        return NULL;
33060ff233Sopenharmony_ci    }
34060ff233Sopenharmony_ci
35060ff233Sopenharmony_ci    errno_t err = memset_s(tmp, size, 0, size);
36060ff233Sopenharmony_ci    if (err != EOK) {
37060ff233Sopenharmony_ci        SoftBusFree(tmp);
38060ff233Sopenharmony_ci        return NULL;
39060ff233Sopenharmony_ci    }
40060ff233Sopenharmony_ci    return tmp;
41060ff233Sopenharmony_ci}
42060ff233Sopenharmony_ci
43060ff233Sopenharmony_civoid SoftBusFree(void *pt)
44060ff233Sopenharmony_ci{
45060ff233Sopenharmony_ci    if (pt == NULL) {
46060ff233Sopenharmony_ci        return;
47060ff233Sopenharmony_ci    }
48060ff233Sopenharmony_ci    free(pt);
49060ff233Sopenharmony_ci}
50060ff233Sopenharmony_ci
51060ff233Sopenharmony_civoid SoftBusClearFree(void *pt, unsigned int size)
52060ff233Sopenharmony_ci{
53060ff233Sopenharmony_ci    if (pt == NULL) {
54060ff233Sopenharmony_ci        return;
55060ff233Sopenharmony_ci    }
56060ff233Sopenharmony_ci    (void)memset_s(pt, size, 0, size);
57060ff233Sopenharmony_ci    SoftBusFree(pt);
58060ff233Sopenharmony_ci}