diff --git a/src/util-mem.h b/src/util-mem.h index 379c8b220e..ea2ef88931 100644 --- a/src/util-mem.h +++ b/src/util-mem.h @@ -207,10 +207,39 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage); (void*)ptrmem; \ }) +/** \brief wrapper for allocing aligned mem + * \param a size + * \param b alignement + */ +#define SCMallocAligned(a, b) ({ \ + void *ptrmem = NULL; \ + \ + if (posix_memalign(&ptrmem, (b), (a)) != 0) { \ + 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; \ +}) + #define SCFree(a) ({ \ free(a); \ }) +/** \brief Free aligned memory + * + * Not needed for mem alloc'd by posix_memalign, + * but for possible future use of _mm_malloc needing + * _mm_free. + */ +#define SCFreeAligned(a) ({ \ + free(a); \ +}) + + #endif /* DBG_MEM_ALLOC */ #endif /* __UTIL_MEM_H__ */