]>
git.ipfire.org Git - people/ms/suricata.git/blob - src/util-mem.c
1 /* Copyright (C) 2007-2020 Open Information Security Foundation
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 #include "suricata-common.h"
20 #include "util-atomic.h"
22 #if defined(_WIN32) || defined(__WIN32)
23 #include <mm_malloc.h>
26 SC_ATOMIC_EXTERN(unsigned int, engine_stage
);
28 void *SCMallocFunc(const size_t sz
)
30 void *ptrmem
= malloc(sz
);
31 if (unlikely(ptrmem
== NULL
)) {
32 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
33 uintmax_t scmalloc_size_
= (uintmax_t)sz
;
34 SCLogError(SC_ERR_MEM_ALLOC
, "SCMalloc failed: %s, while trying "
35 "to allocate %"PRIuMAX
" bytes", strerror(errno
), scmalloc_size_
);
36 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
42 void *SCReallocFunc(void *ptr
, const size_t size
)
44 void *ptrmem
= realloc(ptr
, size
);
45 if (unlikely(ptrmem
== NULL
)) {
46 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
47 SCLogError(SC_ERR_MEM_ALLOC
, "SCRealloc failed: %s, while trying "
48 "to allocate %"PRIuMAX
" bytes", strerror(errno
), (uintmax_t)size
);
49 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
55 void *SCCallocFunc(const size_t nm
, const size_t sz
)
57 void *ptrmem
= calloc(nm
, sz
);
58 if (unlikely(ptrmem
== NULL
)) {
59 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
60 SCLogError(SC_ERR_MEM_ALLOC
, "SCCalloc failed: %s, while trying "
61 "to allocate %"PRIuMAX
" bytes", strerror(errno
), (uintmax_t)nm
*sz
);
62 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
68 char *SCStrdupFunc(const char *s
)
70 char *ptrmem
= strdup(s
);
71 if (unlikely(ptrmem
== NULL
)) {
72 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
73 size_t _scstrdup_len
= strlen(s
);
74 SCLogError(SC_ERR_MEM_ALLOC
, "SCStrdup failed: %s, while trying "
75 "to allocate %"PRIuMAX
" bytes", strerror(errno
), (uintmax_t)_scstrdup_len
);
76 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
82 char *SCStrndupFunc(const char *s
, size_t n
)
85 char *ptrmem
= strndup(s
, n
);
87 const size_t sz
= n
+ 1;
88 char *ptrmem
= (char *)malloc(sz
);
89 if (likely(ptrmem
!= NULL
)) {
90 strlcpy(ptrmem
, s
, sz
);
93 if (unlikely(ptrmem
== NULL
)) {
94 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
95 SCLogError(SC_ERR_MEM_ALLOC
, "SCStrndup failed: %s, while trying "
96 "to allocate %"PRIuMAX
" bytes", strerror(errno
), (uintmax_t)(n
+ 1));
97 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
103 void *SCMallocAlignedFunc(const size_t size
, const size_t align
)
105 #if defined(__WIN32) || defined(_WIN32)
106 void *ptrmem
= _mm_malloc(size
, align
);
107 if (unlikely(ptrmem
== NULL
)) {
108 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
109 SCLogError(SC_ERR_MEM_ALLOC
, "SCMallocAligned(posix_memalign) failed: %s, while trying "
110 "to allocate %"PRIuMAX
" bytes, alignment %"PRIuMAX
, strerror(errno
), (uintmax_t)size
, (uintmax_t)align
);
111 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
116 int r
= posix_memalign(&ptrmem
, align
, size
);
117 if (unlikely(r
!= 0 || ptrmem
== NULL
)) {
118 if (ptrmem
!= NULL
) {
122 if (SC_ATOMIC_GET(engine_stage
) == SURICATA_INIT
) {
123 SCLogError(SC_ERR_MEM_ALLOC
, "SCMallocAligned(posix_memalign) failed: %s, while trying "
124 "to allocate %"PRIuMAX
" bytes, alignment %"PRIuMAX
, strerror(errno
), (uintmax_t)size
, (uintmax_t)align
);
125 FatalError(SC_ERR_FATAL
, "Out of memory. The engine cannot be initialized. Exiting...");
132 void SCFreeAlignedFunc(void *ptr
)
134 #if defined(__WIN32) || defined(_WIN32)