Fix updated memory api using debug mode by default. Small cleanups.

remotes/origin/master-1.0.x
Victor Julien 15 years ago
parent 9f4fae5b1a
commit 692eb935ea

@ -170,7 +170,7 @@ static void SignalHandlerSighup(/*@unused@*/ int sig) { sighup_count = 1; sigfla
#define _GLOBAL_MEM_
/* This counter doesn't complain realloc's(), it's gives
* an aproximation for the startup */
uint64_t global_mem = 0;
size_t global_mem = 0;
#ifdef DBG_MEM_ALLOC_SKIP_STARTUP
uint8_t print_mem_flag = 0;
#else
@ -661,7 +661,7 @@ int main(int argc, char **argv)
if (run_mode == MODE_UNITTEST) {
#ifdef DBG_MEM_ALLOC
SCLogInfo("Memory used at startup: %"PRIu64, global_mem);
SCLogInfo("Memory used at startup: %"PRIdMAX, (intmax_t)global_mem);
#endif
/* test and initialize the unittesting subsystem */
if(regex_arg == NULL){
@ -745,7 +745,7 @@ int main(int argc, char **argv)
}
#ifdef DBG_MEM_ALLOC
SCLogInfo("Total memory used (without SCFree()): %"PRIu64, global_mem);
SCLogInfo("Total memory used (without SCFree()): %"PRIdMAX, (intmax_t)global_mem);
#endif
exit(EXIT_SUCCESS);
@ -906,7 +906,7 @@ int main(int argc, char **argv)
TmThreadContinueThreads();
#ifdef DBG_MEM_ALLOC
SCLogInfo("Memory used at startup: %"PRIu64, global_mem);
SCLogInfo("Memory used at startup: %"PRIdMAX, (intmax_t)global_mem);
#ifdef DBG_MEM_ALLOC_SKIP_STARTUP
print_mem_flag = 1;
#endif
@ -970,7 +970,7 @@ int main(int argc, char **argv)
HTPAtExitPrintStats();
#ifdef DBG_MEM_ALLOC
SCLogInfo("Total memory used (without SCFree()): %"PRIu64, global_mem);
SCLogInfo("Total memory used (without SCFree()): %"PRIdMAX, (intmax_t)global_mem);
#ifdef DBG_MEM_ALLOC_SKIP_STARTUP
print_mem_flag = 0;
#endif

@ -27,73 +27,94 @@
* but there are more.
*/
//#ifndef __UTIL_MEM_H__
//#define __UTIL_MEM_H__
#ifndef __UTIL_MEM_H__
#define __UTIL_MEM_H__
/* Use this only if you want to debug memory allocation and free()
* It will log a lot of lines more, so think that is a performance killer */
/* Uncomment this if you want to print memory allocations and free's() */
#define DBG_MEM_ALLOC
//#define DBG_MEM_ALLOC
#ifdef DBG_MEM_ALLOC
/* Uncomment this if you want to print mallocs at the startup (recommended) */
#define DBG_MEM_ALLOC_SKIP_STARTUP
#define SCMalloc(a) ({ \
void *ptrmem = NULL; \
extern uint64_t global_mem; \
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
\
ptrmem = malloc(a); \
if (ptrmem == NULL && a > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying to allocate %"PRIu64" bytes", strerror(errno), (uint64_t)a); \
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)a); \
} \
\
global_mem += a; \
if (print_mem_flag == 1) \
SCLogInfo("SCMalloc return at %p of size %"PRIu64, ptrmem, (uint64_t)a); \
SCLogInfo("SCMalloc return at %p of size %"PRIdMAX, \
ptrmem, (intmax_t)a); \
\
(void*)ptrmem; \
})
#define SCRealloc(x, a) ({ \
void *ptrmem = NULL; \
extern uint64_t global_mem; \
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
\
ptrmem = realloc(x, a); \
if (ptrmem == NULL && a > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying to allocate %"PRIu64" bytes", strerror(errno), (uint64_t)a); \
SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)a); \
} \
\
global_mem += a; \
if (print_mem_flag == 1) \
SCLogInfo("SCRealloc return at %p (old:%p) of size %"PRIu64, ptrmem, x, (uint64_t)a); \
SCLogInfo("SCRealloc return at %p (old:%p) of size %"PRIdMAX, \
ptrmem, x, (intmax_t)a); \
\
(void*)ptrmem; \
})
#define SCCalloc(nm, a) ({ \
void *ptrmem = NULL; \
extern uint64_t global_mem; \
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
\
ptrmem = calloc(nm, a); \
if (ptrmem == NULL && a > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying to allocate %"PRIu64" bytes", strerror(errno), (uint64_t)a); \
SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)a); \
} \
\
global_mem += a*nm; \
if (print_mem_flag == 1) \
SCLogInfo("SCCalloc return at %p of size %"PRIu64" nm %"PRIu64, ptrmem, (uint64_t)a, (uint64_t)nm); \
SCLogInfo("SCCalloc return at %p of size %"PRIdMAX" nm %"PRIdMAX, \
ptrmem, (intmax_t)a, (intmax_t)nm); \
\
(void*)ptrmem; \
})
#define SCStrdup(a) ({ \
char *ptrmem = NULL; \
extern uint64_t global_mem; \
extern size_t global_mem; \
extern uint8_t print_mem_flag; \
size_t len = strlen(a); \
\
ptrmem = strdup(a); \
if (ptrmem == NULL && len > 0) { \
SCLogError(SC_ERR_MEM_ALLOC, "Strdup of size %"PRIu64" failed! exiting.", (uint64_t)len); \
exit(EXIT_FAILURE); \
SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \
"to allocate %"PRIu64" bytes", strerror(errno), (intmax_t)len); \
} \
\
global_mem += len; \
if (print_mem_flag == 1) \
SCLogInfo("SCStrdup return at %p of size %"PRIu64, ptrmem, (uint64_t)len); \
SCLogInfo("SCStrdup return at %p of size %"PRIdMAX, \
ptrmem, (intmax_t)len); \
\
(void*)ptrmem; \
})
@ -104,6 +125,47 @@
free(a); \
})
#else /* DBG_MEM_ALLOC */
#define SCMalloc(a) ({ \
void *ptrmem = malloc(a); \
if (ptrmem == NULL) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCMalloc failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)a); \
} \
(void*)ptrmem; \
})
#define SCRealloc(x, a) ({ \
void *ptrmem = realloc(x, a); \
if (ptrmem == NULL) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCRealloc failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)a); \
} \
(void*)ptrmem; \
})
#define SCCalloc(nm, a) ({ \
void *ptrmem = calloc(nm, a); \
if (ptrmem == NULL) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCCalloc failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)a); \
} \
(void*)ptrmem; \
})
#define SCStrdup(a) ({ \
char *ptrmem = strdup(a); \
if (ptrmem == NULL) { \
SCLogError(SC_ERR_MEM_ALLOC, "SCStrdup failed: %s, while trying " \
"to allocate %"PRIdMAX" bytes", strerror(errno), (intmax_t)strlen(a)); \
} \
(void*)ptrmem; \
})
#define SCFree(a) free((a))
#endif /* DBG_MEM_ALLOC */
//#endif /* __UTIL_MEM_H__ */
#endif /* __UTIL_MEM_H__ */

Loading…
Cancel
Save