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