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 
22 #if defined(_WIN32) || defined(__WIN32)
23 #include <mm_malloc.h>
24 #endif
25 
26 SC_ATOMIC_EXTERN(unsigned int, engine_stage);
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 }
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SURICATA_INIT
@ SURICATA_INIT
Definition: suricata.h:95
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:28
SCStrdupFunc
char * SCStrdupFunc(const char *s)
Definition: util-mem.c:68
util-atomic.h
MacSet_::size
int size
Definition: util-macset.c:55
SCMallocAlignedFunc
void * SCMallocAlignedFunc(const size_t size, const size_t align)
wrapper for allocing aligned mem
Definition: util-mem.c:103
SCStrndupFunc
char * SCStrndupFunc(const char *s, size_t n)
Definition: util-mem.c:82
SCCallocFunc
void * SCCallocFunc(const size_t nm, const size_t sz)
Definition: util-mem.c:55
suricata-common.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
FatalError
#define FatalError(x,...)
Definition: util-debug.h:530
SCFreeAlignedFunc
void SCFreeAlignedFunc(void *ptr)
Free aligned memory.
Definition: util-mem.c:132
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
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:376
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(unsigned int, engine_stage)
SCReallocFunc
void * SCReallocFunc(void *ptr, const size_t size)
Definition: util-mem.c:42