added a few lines of code to print simple memory allocation counts in cmd/awk/awk.c and cmd/sed/sed.c.

got rid of redundant realloc handler check in lib/awk/*.c
This commit is contained in:
hyung-hwan 2011-12-21 06:40:27 +00:00
parent 42c44f9d3d
commit 38d3c22d1f
4 changed files with 69 additions and 49 deletions

View File

@ -794,25 +794,39 @@ static qse_mmgr_t xma_mmgr =
#if defined(QSE_BUILD_DEBUG) #if defined(QSE_BUILD_DEBUG)
static qse_ulong_t debug_mmgr_count = 0; static qse_ulong_t debug_mmgr_count = 0;
static qse_ulong_t debug_mmgr_alloc_count = 0;
static qse_ulong_t debug_mmgr_realloc_count = 0;
static qse_ulong_t debug_mmgr_free_count = 0;
static void* debug_mmgr_alloc (void* ctx, qse_size_t size) static void* debug_mmgr_alloc (void* ctx, qse_size_t size)
{ {
void* ptr;
struct arg_t* arg = (struct arg_t*)ctx; struct arg_t* arg = (struct arg_t*)ctx;
debug_mmgr_count++; debug_mmgr_count++;
if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL; if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL;
return malloc (size); ptr = malloc (size);
if (ptr) debug_mmgr_alloc_count++;
return ptr;
} }
static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size) static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size)
{ {
void* rptr;
struct arg_t* arg = (struct arg_t*)ctx; struct arg_t* arg = (struct arg_t*)ctx;
debug_mmgr_count++; debug_mmgr_count++;
if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL; if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL;
return realloc (ptr, size); rptr = realloc (ptr, size);
if (rptr)
{
if (ptr) debug_mmgr_realloc_count++;
else debug_mmgr_alloc_count++;
}
return rptr;
} }
static void debug_mmgr_free (void* ctx, void* ptr) static void debug_mmgr_free (void* ctx, void* ptr)
{ {
debug_mmgr_free_count++;
free (ptr); free (ptr);
} }
@ -976,6 +990,19 @@ oops:
if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx); if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx);
freearg (&arg); freearg (&arg);
#if defined(QSE_BUILD_DEBUG)
if (arg.failmalloc > 0)
{
qse_fprintf (QSE_STDERR, QSE_T("\n"));
qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
(unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count);
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
}
#endif
return ret; return ret;
} }

View File

@ -74,25 +74,39 @@ static qse_sed_t* g_sed = QSE_NULL;
#if defined(QSE_BUILD_DEBUG) #if defined(QSE_BUILD_DEBUG)
#include <stdlib.h> #include <stdlib.h>
static qse_ulong_t g_failmalloc; static qse_ulong_t g_failmalloc = 0;
static qse_ulong_t debug_mmgr_count = 0; static qse_ulong_t debug_mmgr_count = 0;
static qse_ulong_t debug_mmgr_alloc_count = 0;
static qse_ulong_t debug_mmgr_realloc_count = 0;
static qse_ulong_t debug_mmgr_free_count = 0;
static void* debug_mmgr_alloc (void* ctx, qse_size_t size) static void* debug_mmgr_alloc (void* ctx, qse_size_t size)
{ {
void* ptr;
debug_mmgr_count++; debug_mmgr_count++;
if (debug_mmgr_count % g_failmalloc == 0) return QSE_NULL; if (debug_mmgr_count % g_failmalloc == 0) return QSE_NULL;
return malloc (size); ptr = malloc (size);
if (ptr) debug_mmgr_alloc_count++;
return ptr;
} }
static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size) static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size)
{ {
void* rptr;
debug_mmgr_count++; debug_mmgr_count++;
if (debug_mmgr_count % g_failmalloc == 0) return QSE_NULL; if (debug_mmgr_count % g_failmalloc == 0) return QSE_NULL;
return realloc (ptr, size); rptr = realloc (ptr, size);
if (rptr)
{
if (ptr) debug_mmgr_realloc_count++;
else debug_mmgr_alloc_count++;
}
return rptr;
} }
static void debug_mmgr_free (void* ctx, void* ptr) static void debug_mmgr_free (void* ctx, void* ptr)
{ {
debug_mmgr_free_count++;
free (ptr); free (ptr);
} }
@ -783,6 +797,19 @@ oops:
if (fs) qse_fs_close (fs); if (fs) qse_fs_close (fs);
if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx); if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx);
free_scripts (); free_scripts ();
#if defined(QSE_BUILD_DEBUG)
if (g_failmalloc > 0)
{
qse_fprintf (QSE_STDERR, QSE_T("\n"));
qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
(unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count);
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
}
#endif
return ret; return ret;
} }

View File

@ -423,32 +423,13 @@ static int recomp_record_fields (
* number of fields that the current record can hold, * number of fields that the current record can hold,
* the field spaces are resized */ * the field spaces are resized */
if (run->awk->mmgr->realloc != QSE_NULL) tmp = QSE_AWK_REALLOC (
run->awk, run->inrec.flds,
QSE_SIZEOF(*run->inrec.flds) * max);
if (tmp == QSE_NULL)
{ {
tmp = QSE_AWK_REALLOC ( qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
run->awk, run->inrec.flds, return -1;
QSE_SIZEOF(*run->inrec.flds) * max);
if (tmp == QSE_NULL)
{
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
}
else
{
tmp = QSE_AWK_ALLOC (
run->awk, QSE_SIZEOF(*run->inrec.flds) * max);
if (tmp == QSE_NULL)
{
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
if (run->inrec.flds != QSE_NULL)
{
QSE_MEMCPY (tmp, run->inrec.flds,
QSE_SIZEOF(*run->inrec.flds)*run->inrec.maxflds);
QSE_AWK_FREE (run->awk, run->inrec.flds);
}
} }
run->inrec.flds = tmp; run->inrec.flds = tmp;

View File

@ -6385,25 +6385,10 @@ static int __raw_push (qse_awk_rtx_t* run, void* val)
n = run->stack_limit + STACK_INCREMENT; n = run->stack_limit + STACK_INCREMENT;
if (MMGR(run)->realloc != QSE_NULL) tmp = (void**) QSE_AWK_REALLOC (
{ run->awk, run->stack, n * QSE_SIZEOF(void*));
tmp = (void**) QSE_AWK_REALLOC ( if (tmp == QSE_NULL) return -1;
run->awk, run->stack, n * QSE_SIZEOF(void*));
if (tmp == QSE_NULL) return -1;
}
else
{
tmp = (void**) QSE_AWK_ALLOC (
run->awk, n * QSE_SIZEOF(void*));
if (tmp == QSE_NULL) return -1;
if (run->stack != QSE_NULL)
{
QSE_MEMCPY (
tmp, run->stack,
run->stack_limit * QSE_SIZEOF(void*));
QSE_AWK_FREE (run->awk, run->stack);
}
}
run->stack = tmp; run->stack = tmp;
run->stack_limit = n; run->stack_limit = n;
} }