suricata
util-mem.h File Reference
#include "util-atomic.h"
Include dependency graph for util-mem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SCMalloc(a)
 
#define SCRealloc(x, a)
 
#define SCCalloc(nm, a)
 
#define SCStrdup(a)
 
#define SCStrndup(a, b)
 
#define SCFree(a)
 
#define SCMallocAligned(a, b)
 wrapper for allocing aligned mem More...
 
#define SCFreeAligned(a)
 Free aligned memory. More...
 

Functions

 SC_ATOMIC_EXTERN (unsigned int, engine_stage)
 

Detailed Description

Author
Pablo Rincon Crespo pablo.nosp@m..rin.nosp@m.con.c.nosp@m.resp.nosp@m.o@gma.nosp@m.il.c.nosp@m.om
Bill Meeks billm.nosp@m.eeks.nosp@m.8@gma.nosp@m.il.c.nosp@m.om

Utility Macros for memory management

Todo:
Add wrappers for functions that allocate/free memory here. Currently we have malloc, calloc, realloc, strdup, strndup and free, but there are more.

Definition in file util-mem.h.

Macro Definition Documentation

◆ SCCalloc

#define SCCalloc (   nm,
 
)
Value:
({ \
void *ptrmem = NULL; \
\
ptrmem = calloc((nm), (a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
(void*)ptrmem; \
})

Definition at line 253 of file util-mem.h.

◆ SCFree

#define SCFree (   a)
Value:
({ \
free(a); \
})

Definition at line 322 of file util-mem.h.

◆ SCFreeAligned

#define SCFreeAligned (   a)
Value:
({ \
free(a); \
})

Free aligned memory.

Not needed for mem alloc'd by posix_memalign, but for possible future use of _mm_malloc needing _mm_free.

Definition at line 388 of file util-mem.h.

◆ SCMalloc

#define SCMalloc (   a)
Value:
({ \
void *ptrmem = NULL; \
\
ptrmem = malloc((a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
uintmax_t scmalloc_size_ = (uintmax_t)(a); \
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), scmalloc_size_); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
(void*)ptrmem; \
})

Definition at line 222 of file util-mem.h.

◆ SCMallocAligned

#define SCMallocAligned (   a,
 
)
Value:
({ \
void *ptrmem = NULL; \
\
int _r = posix_memalign(&ptrmem, (b), (a)); \
if (_r != 0 || ptrmem == NULL) { \
if (ptrmem != NULL) { \
free(ptrmem); \
ptrmem = NULL; \
} \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCMallocAligned(posix_memalign) failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes, alignment %"PRIuMAX, strerror(errno), (uintmax_t)a, (uintmax_t)b); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
(void*)ptrmem; \
})

wrapper for allocing aligned mem

Parameters
asize
balignement

Definition at line 363 of file util-mem.h.

◆ SCRealloc

#define SCRealloc (   x,
 
)
Value:
({ \
void *ptrmem = NULL; \
\
ptrmem = realloc((x), (a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)(a)); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
(void*)ptrmem; \
})

Definition at line 238 of file util-mem.h.

◆ SCStrdup

#define SCStrdup (   a)
Value:
({ \
char *ptrmem = NULL; \
\
ptrmem = strdup((a)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
size_t _scstrdup_len = strlen((a)); \
SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)_scstrdup_len); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} \
(void*)ptrmem; \
})

Definition at line 268 of file util-mem.h.

◆ SCStrndup

#define SCStrndup (   a,
 
)
Value:
({ \
char *ptrmem = NULL; \
size_t _len = (b); \
\
size_t _scstrndup_len = _len + 1; \
ptrmem = (char *)malloc(_scstrndup_len); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
SCLogError(SC_ERR_MEM_ALLOC, "SCStrndup failed: %s, while trying " \
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)_scstrndup_len); \
SCLogError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting..."); \
exit(EXIT_FAILURE); \
} \
} else { \
strlcpy(ptrmem, (a), _scstrndup_len); \
*(ptrmem + _len) = '\0'; \
} \
(void*)ptrmem; \
})

Definition at line 285 of file util-mem.h.

Function Documentation

◆ SC_ATOMIC_EXTERN()

SC_ATOMIC_EXTERN ( unsigned int  ,
engine_stage   
)
SURICATA_INIT
@ SURICATA_INIT
Definition: suricata.h:101
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:192