suricata
util-mem.c
Go to the documentation of this file.
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"
20 #include "util-atomic.h"
21 #include "util-debug.h"
22 
23 #if defined(_WIN32) || defined(__WIN32)
24 #include <mm_malloc.h>
25 #endif
26 
27 SC_ATOMIC_EXTERN(unsigned int, engine_stage);
28 
29 void *SCMallocFunc(const size_t sz)
30 {
31  void *ptrmem = malloc(sz);
32  if (unlikely(ptrmem == NULL)) {
33  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
34  uintmax_t scmalloc_size_ = (uintmax_t)sz;
35  SCLogError("SCMalloc failed: %s, while trying "
36  "to allocate %" PRIuMAX " bytes",
37  strerror(errno), scmalloc_size_);
38  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
39  }
40  }
41  return ptrmem;
42 }
43 
44 void *SCReallocFunc(void *ptr, const size_t size)
45 {
46  void *ptrmem = realloc(ptr, size);
47  if (unlikely(ptrmem == NULL)) {
48  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
49  SCLogError("SCRealloc failed: %s, while trying "
50  "to allocate %" PRIuMAX " bytes",
51  strerror(errno), (uintmax_t)size);
52  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
53  }
54  }
55  return ptrmem;
56 }
57 
58 void *SCCallocFunc(const size_t nm, const size_t sz)
59 {
60  void *ptrmem = calloc(nm, sz);
61  if (unlikely(ptrmem == NULL)) {
62  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
63  SCLogError("SCCalloc failed: %s, while trying "
64  "to allocate %" PRIuMAX " bytes",
65  strerror(errno), (uintmax_t)nm * sz);
66  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
67  }
68  }
69  return ptrmem;
70 }
71 
72 char *SCStrdupFunc(const char *s)
73 {
74  char *ptrmem = strdup(s);
75  if (unlikely(ptrmem == NULL)) {
76  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
77  size_t _scstrdup_len = strlen(s);
78  SCLogError("SCStrdup failed: %s, while trying "
79  "to allocate %" PRIuMAX " bytes",
80  strerror(errno), (uintmax_t)_scstrdup_len);
81  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
82  }
83  }
84  return ptrmem;
85 }
86 
87 char *SCStrndupFunc(const char *s, size_t n)
88 {
89 #ifdef HAVE_STRNDUP
90  char *ptrmem = strndup(s, n);
91 #else
92  const size_t sz = n + 1;
93  char *ptrmem = (char *)malloc(sz);
94  if (likely(ptrmem != NULL)) {
95  strlcpy(ptrmem, s, sz);
96  }
97 #endif
98  if (unlikely(ptrmem == NULL)) {
99  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
100  SCLogError("SCStrndup failed: %s, while trying "
101  "to allocate %" PRIuMAX " bytes",
102  strerror(errno), (uintmax_t)(n + 1));
103  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
104  }
105  }
106  return ptrmem;
107 }
108 
109 void *SCMallocAlignedFunc(const size_t size, const size_t align)
110 {
111 #if defined(__WIN32) || defined(_WIN32)
112  void *ptrmem = _mm_malloc(size, align);
113  if (unlikely(ptrmem == NULL)) {
114  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
115  SCLogError("SCMallocAligned(posix_memalign) failed: %s, while trying "
116  "to allocate %" PRIuMAX " bytes, alignment %" PRIuMAX,
117  strerror(errno), (uintmax_t)size, (uintmax_t)align);
118  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
119  }
120  }
121 #else
122  void *ptrmem = NULL;
123  int r = posix_memalign(&ptrmem, align, size);
124  if (unlikely(r != 0 || ptrmem == NULL)) {
125  if (ptrmem != NULL) {
126  free(ptrmem);
127  ptrmem = NULL;
128  }
129  if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
130  SCLogError("SCMallocAligned(posix_memalign) failed: %s, while trying "
131  "to allocate %" PRIuMAX " bytes, alignment %" PRIuMAX,
132  strerror(errno), (uintmax_t)size, (uintmax_t)align);
133  FatalError("Out of memory. The engine cannot be initialized. Exiting...");
134  }
135  }
136 #endif
137  return ptrmem;
138 }
139 
140 void SCFreeAlignedFunc(void *ptr)
141 {
142 #if defined(__WIN32) || defined(_WIN32)
143  _mm_free(ptr);
144 #else
145  free(ptr);
146 #endif
147 }
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
SCMallocFunc
void * SCMallocFunc(const size_t sz)
Definition: util-mem.c:29
util-debug.h
SCStrdupFunc
char * SCStrdupFunc(const char *s)
Definition: util-mem.c:72
util-atomic.h
MacSet_::size
int size
Definition: util-macset.c:56
SCMallocAlignedFunc
void * SCMallocAlignedFunc(const size_t size, const size_t align)
wrapper for allocing aligned mem
Definition: util-mem.c:109
SCStrndupFunc
char * SCStrndupFunc(const char *s, size_t n)
Definition: util-mem.c:87
SCCallocFunc
void * SCCallocFunc(const size_t nm, const size_t sz)
Definition: util-mem.c:58
suricata-common.h
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SCFreeAlignedFunc
void SCFreeAlignedFunc(void *ptr)
Free aligned memory.
Definition: util-mem.c:140
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
suricata.h
likely
#define likely(expr)
Definition: util-optimize.h:32
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(unsigned int, engine_stage)
SCReallocFunc
void * SCReallocFunc(void *ptr, const size_t size)
Definition: util-mem.c:44
SURICATA_INIT
@ SURICATA_INIT
Definition: suricata.h:95