diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index af37ebe9..ab64ff1d 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -794,25 +794,39 @@ static qse_mmgr_t xma_mmgr = #if defined(QSE_BUILD_DEBUG) 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) { + void* ptr; struct arg_t* arg = (struct arg_t*)ctx; debug_mmgr_count++; 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) { + void* rptr; struct arg_t* arg = (struct arg_t*)ctx; debug_mmgr_count++; 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) { + debug_mmgr_free_count++; free (ptr); } @@ -976,6 +990,19 @@ oops: if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx); 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; } diff --git a/qse/cmd/sed/sed.c b/qse/cmd/sed/sed.c index 96414798..d144da42 100644 --- a/qse/cmd/sed/sed.c +++ b/qse/cmd/sed/sed.c @@ -74,25 +74,39 @@ static qse_sed_t* g_sed = QSE_NULL; #if defined(QSE_BUILD_DEBUG) #include -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_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) { + void* ptr; debug_mmgr_count++; 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) { + void* rptr; debug_mmgr_count++; 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) { + debug_mmgr_free_count++; free (ptr); } @@ -783,6 +797,19 @@ oops: if (fs) qse_fs_close (fs); if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx); 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; } diff --git a/qse/lib/awk/rec.c b/qse/lib/awk/rec.c index dfc9958b..5b86ec39 100644 --- a/qse/lib/awk/rec.c +++ b/qse/lib/awk/rec.c @@ -423,32 +423,13 @@ static int recomp_record_fields ( * number of fields that the current record can hold, * 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 ( - run->awk, run->inrec.flds, - 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); - } + qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL); + return -1; } run->inrec.flds = tmp; diff --git a/qse/lib/awk/run.c b/qse/lib/awk/run.c index 69316c21..c7550c97 100644 --- a/qse/lib/awk/run.c +++ b/qse/lib/awk/run.c @@ -6385,25 +6385,10 @@ static int __raw_push (qse_awk_rtx_t* run, void* val) 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*)); - 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); - } - } + tmp = (void**) QSE_AWK_REALLOC ( + run->awk, run->stack, n * QSE_SIZEOF(void*)); + if (tmp == QSE_NULL) return -1; + run->stack = tmp; run->stack_limit = n; }