summaryrefslogtreecommitdiffstats
path: root/include/share/alloc.h
blob: 2e92dd893ae684cc0996284c5a6ddc280da4a229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __SHARE_ALLOC_H_
#define __SHARE_ALLOC_H_

#include <limits.h>

// malloc(n floor 1)
static inline void *safe_malloc_(size_t n)
{
    // dlmalloc is already safe
    return malloc(n);
}

// malloc(n1 * n2) then memset to zero
static inline void *safe_calloc_(size_t n1, size_t n2)
{
    // dlcalloc is already safe
    return calloc(n1, n2);
}

// malloc(n1 + n2)
static inline void *safe_malloc_add_2op_(size_t n1, size_t n2)
{
    unsigned long long n = n1 + n2;
    size_t ns = n;
    // check for overflow
    return n == ns ? malloc(ns) : NULL;
}

// malloc(n1 * n2)
static inline void *safe_malloc_mul_2op_(size_t n1, size_t n2)
{
    unsigned long long n = n1 * n2;
    size_t ns = n;
    // check for overflow
    return n == ns ? malloc(ns) : NULL;
}

// malloc(n1 * (n2 + n3))
static inline void *safe_malloc_muladd2_(size_t n1, size_t n2, size_t n3)
{
    unsigned long long n = n1 * (n2 + n3);
    size_t ns = n;
    // check for overflow
    return n == ns ? malloc(ns) : NULL;
}

// realloc(ptr, n1 * n2)
static inline void *safe_realloc_mul_2op_(void *ptr, size_t n1, size_t n2)
{
    unsigned long long n = n1 * n2;
    size_t ns = n;
    // check for overflow
    return n == ns ? realloc(ptr, ns) : NULL;
}

#endif // __SHARE_ALLOC_H_