]>
Commit | Line | Data |
---|---|---|
3b877929 VJ |
1 | /* Copyright (C) 2007-2020 Open Information Security Foundation |
2 | * | |
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 | |
5 | * Software Foundation. | |
6 | * | |
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. | |
11 | * | |
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 | |
15 | * 02110-1301, USA. | |
16 | */ | |
17 | ||
18 | #include "suricata-common.h" | |
19 | #include "suricata.h" | |
415c9929 VJ |
20 | #include "util-atomic.h" |
21 | ||
d4613e5c VJ |
22 | #if defined(_WIN32) || defined(__WIN32) |
23 | #include <mm_malloc.h> | |
24 | #endif | |
25 | ||
415c9929 | 26 | SC_ATOMIC_EXTERN(unsigned int, engine_stage); |
3b877929 VJ |
27 | |
28 | void *SCMallocFunc(const size_t sz) | |
29 | { | |
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..."); | |
37 | } | |
38 | } | |
39 | return ptrmem; | |
40 | } | |
41 | ||
42 | void *SCReallocFunc(void *ptr, const size_t size) | |
43 | { | |
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..."); | |
50 | } | |
51 | } | |
52 | return ptrmem; | |
53 | } | |
54 | ||
55 | void *SCCallocFunc(const size_t nm, const size_t sz) | |
56 | { | |
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..."); | |
63 | } | |
64 | } | |
65 | return ptrmem; | |
66 | } | |
67 | ||
68 | char *SCStrdupFunc(const char *s) | |
69 | { | |
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..."); | |
77 | } | |
78 | } | |
79 | return ptrmem; | |
80 | } | |
81 | ||
82 | char *SCStrndupFunc(const char *s, size_t n) | |
83 | { | |
84 | #ifdef HAVE_STRNDUP | |
85 | char *ptrmem = strndup(s, n); | |
86 | #else | |
87 | const size_t sz = n + 1; | |
88 | char *ptrmem = (char *)malloc(sz); | |
89 | if (likely(ptrmem != NULL)) { | |
90 | strlcpy(ptrmem, s, sz); | |
91 | } | |
92 | #endif | |
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..."); | |
98 | } | |
99 | } | |
100 | return ptrmem; | |
101 | } | |
102 | ||
103 | void *SCMallocAlignedFunc(const size_t size, const size_t align) | |
104 | { | |
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..."); | |
112 | } | |
113 | } | |
114 | #else | |
115 | void *ptrmem = NULL; | |
116 | int r = posix_memalign(&ptrmem, align, size); | |
117 | if (unlikely(r != 0 || ptrmem == NULL)) { | |
118 | if (ptrmem != NULL) { | |
119 | free(ptrmem); | |
120 | ptrmem = NULL; | |
121 | } | |
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..."); | |
126 | } | |
127 | } | |
128 | #endif | |
129 | return ptrmem; | |
130 | } | |
131 | ||
132 | void SCFreeAlignedFunc(void *ptr) | |
133 | { | |
134 | #if defined(__WIN32) || defined(_WIN32) | |
135 | _mm_free(ptr); | |
136 | #else | |
137 | free(ptr); | |
138 | #endif | |
139 | } |