summaryrefslogtreecommitdiffstats
path: root/src/util-mem.c
blob: babdfa3f5671616e5819c8efc0818a3bcb574713 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright (C) 2007-2020 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "suricata-common.h"
#include "suricata.h"
#include "util-atomic.h"
#include "util-debug.h"

#if defined(_WIN32) || defined(__WIN32)
#include <mm_malloc.h>
#endif

SC_ATOMIC_EXTERN(unsigned int, engine_stage);

void *SCMallocFunc(const size_t sz)
{
    void *ptrmem = malloc(sz);
    if (unlikely(ptrmem == NULL)) {
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            uintmax_t scmalloc_size_ = (uintmax_t)sz;
            SCLogError("SCMalloc failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes",
                    strerror(errno), scmalloc_size_);
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
    return ptrmem;
}

void *SCReallocFunc(void *ptr, const size_t size)
{
    void *ptrmem = realloc(ptr, size);
    if (unlikely(ptrmem == NULL)) {
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            SCLogError("SCRealloc failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes",
                    strerror(errno), (uintmax_t)size);
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
    return ptrmem;
}

void *SCCallocFunc(const size_t nm, const size_t sz)
{
    void *ptrmem = calloc(nm, sz);
    if (unlikely(ptrmem == NULL)) {
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            SCLogError("SCCalloc failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes",
                    strerror(errno), (uintmax_t)nm * sz);
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
    return ptrmem;
}

char *SCStrdupFunc(const char *s)
{
    char *ptrmem = strdup(s);
    if (unlikely(ptrmem == NULL)) {
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            size_t _scstrdup_len = strlen(s);
            SCLogError("SCStrdup failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes",
                    strerror(errno), (uintmax_t)_scstrdup_len);
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
    return ptrmem;
}

char *SCStrndupFunc(const char *s, size_t n)
{
#ifdef HAVE_STRNDUP
    char *ptrmem = strndup(s, n);
#else
    const size_t sz = n + 1;
    char *ptrmem = (char *)malloc(sz);
    if (likely(ptrmem != NULL)) {
        strlcpy(ptrmem, s, sz);
    }
#endif
    if (unlikely(ptrmem == NULL)) {
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            SCLogError("SCStrndup failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes",
                    strerror(errno), (uintmax_t)(n + 1));
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
    return ptrmem;
}

void *SCMallocAlignedFunc(const size_t size, const size_t align)
{
#if defined(__WIN32) || defined(_WIN32)
    void *ptrmem = _mm_malloc(size, align);
    if (unlikely(ptrmem == NULL)) {
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            SCLogError("SCMallocAligned(posix_memalign) failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes, alignment %" PRIuMAX,
                    strerror(errno), (uintmax_t)size, (uintmax_t)align);
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
#else
    void *ptrmem = NULL;
    int r = posix_memalign(&ptrmem, align, size);
    if (unlikely(r != 0 || ptrmem == NULL)) {
        if (ptrmem != NULL) {
            free(ptrmem);
            ptrmem = NULL;
        }
        if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
            SCLogError("SCMallocAligned(posix_memalign) failed: %s, while trying "
                       "to allocate %" PRIuMAX " bytes, alignment %" PRIuMAX,
                    strerror(errno), (uintmax_t)size, (uintmax_t)align);
            FatalError("Out of memory. The engine cannot be initialized. Exiting...");
        }
    }
#endif
    return ptrmem;
}

void SCFreeAlignedFunc(void *ptr)
{
#if defined(__WIN32) || defined(_WIN32)
    _mm_free(ptr);
#else
    free(ptr);
#endif
}