util/mem: move most logic to functions

Reduce macro use and simplify code. Also reduces compiled code
size.
pull/4806/head
Victor Julien 7 years ago
parent 48bb26abe7
commit 3b877929e3

@ -481,7 +481,7 @@ util-lua-smtp.c util-lua-smtp.h \
util-magic.c util-magic.h \
util-memcmp.c util-memcmp.h \
util-memcpy.h \
util-mem.h \
util-mem.c util-mem.h \
util-memrchr.c util-memrchr.h \
util-misc.c util-misc.h \
util-mpm-ac-bs.c util-mpm-ac-bs.h \

@ -0,0 +1,132 @@
/* Copyright (C) 2007-2020 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "suricata-common.h"
#include "suricata.h"
void *SCMallocFunc(const size_t sz)
{
void *ptrmem = malloc(sz);
if (unlikely(ptrmem == NULL)) {
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
uintmax_t scmalloc_size_ = (uintmax_t)sz;
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying "
"to allocate %"PRIuMAX" bytes", strerror(errno), scmalloc_size_);
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
return ptrmem;
}
void *SCReallocFunc(void *ptr, const size_t size)
{
void *ptrmem = realloc(ptr, size);
if (unlikely(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)size);
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
return ptrmem;
}
void *SCCallocFunc(const size_t nm, const size_t sz)
{
void *ptrmem = calloc(nm, sz);
if (unlikely(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)nm*sz);
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
return ptrmem;
}
char *SCStrdupFunc(const char *s)
{
char *ptrmem = strdup(s);
if (unlikely(ptrmem == NULL)) {
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {
size_t _scstrdup_len = strlen(s);
SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying "
"to allocate %"PRIuMAX" bytes", strerror(errno), (uintmax_t)_scstrdup_len);
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
return ptrmem;
}
char *SCStrndupFunc(const char *s, size_t n)
{
#ifdef HAVE_STRNDUP
char *ptrmem = strndup(s, n);
#else
const size_t sz = n + 1;
char *ptrmem = (char *)malloc(sz);
if (likely(ptrmem != NULL)) {
strlcpy(ptrmem, s, sz);
}
#endif
if (unlikely(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)(n + 1));
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
return ptrmem;
}
void *SCMallocAlignedFunc(const size_t size, const size_t align)
{
#if defined(__WIN32) || defined(_WIN32)
void *ptrmem = _mm_malloc(size, align);
if (unlikely(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)size, (uintmax_t)align);
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
#else
void *ptrmem = NULL;
int r = posix_memalign(&ptrmem, align, size);
if (unlikely(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)size, (uintmax_t)align);
FatalError(SC_ERR_FATAL, "Out of memory. The engine cannot be initialized. Exiting...");
}
}
#endif
return ptrmem;
}
void SCFreeAlignedFunc(void *ptr)
{
#if defined(__WIN32) || defined(_WIN32)
_mm_free(ptr);
#else
free(ptr);
#endif
}

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2019 Open Information Security Foundation
/* Copyright (C) 2007-2020 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
@ -51,165 +51,29 @@
SC_ATOMIC_EXTERN(unsigned int, engine_stage);
#define SCMalloc(a) ({ \
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; \
})
#define SCRealloc(x, a) ({ \
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; \
})
#define SCCalloc(nm, a) ({ \
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; \
})
#define SCStrdup(a) ({ \
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; \
})
#ifndef HAVE_STRNDUP
#define SCStrndup(a, b) ({ \
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; \
})
#else /* HAVE_STRNDUP */
#define SCStrndup(a, b) ({ \
char *ptrmem = NULL; \
\
ptrmem = strndup((a), (b)); \
if (ptrmem == NULL) { \
if (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT) {\
size_t _scstrndup_len = (b); \
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); \
} \
} \
(void*)ptrmem; \
})
#endif
void *SCMallocFunc(const size_t sz);
#define SCMalloc(sz) SCMallocFunc((sz))
#define SCFree(a) ({ \
free(a); \
})
void *SCReallocFunc(void *ptr, const size_t size);
#define SCRealloc(ptr, sz) SCReallocFunc((ptr), (sz))
#if defined(__WIN32) || defined(_WIN32)
void *SCCallocFunc(const size_t nm, const size_t sz);
#define SCCalloc(nm, sz) SCCallocFunc((nm), (sz))
/** \brief wrapper for allocing aligned mem
* \param a size
* \param b alignement
*/
#define SCMallocAligned(a, b) ({ \
void *ptrmem = NULL; \
\
ptrmem = _mm_malloc((a), (b)); \
if (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; \
})
char *SCStrdupFunc(const char *s);
#define SCStrdup(s) SCStrdupFunc((s))
/** \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) ({ \
_mm_free(a); \
})
char *SCStrndupFunc(const char *s, size_t n);
#define SCStrndup(s, n) SCStrndupFunc((s), (n))
#else /* !win */
#define SCFree(p) free((p))
/** \brief wrapper for allocing aligned mem
* \param a size
* \param b alignement
*/
#define SCMallocAligned(a, b) ({ \
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; \
})
void *SCMallocAlignedFunc(const size_t size, const size_t align);
#define SCMallocAligned(size, align) SCMallocAlignedFunc((size), (align))
/** \brief Free aligned memory
*
@ -217,11 +81,8 @@ SC_ATOMIC_EXTERN(unsigned int, engine_stage);
* but for possible future use of _mm_malloc needing
* _mm_free.
*/
#define SCFreeAligned(a) ({ \
free(a); \
})
#endif /* __WIN32 */
void SCFreeAlignedFunc(void *ptr);
#define SCFreeAligned(p) SCFreeAlignedFunc((p))
#endif /* CPPCHECK */

Loading…
Cancel
Save