changed to prototype of qse_mmgr_t function to accept mmgr itself instead of mmgr->ctx and fixed a memory leak bug under a certain failure condition

This commit is contained in:
hyung-hwan 2014-07-09 15:01:16 +00:00
parent 36b20b4169
commit 82a639045f
10 changed files with 100 additions and 41 deletions

View File

@ -904,24 +904,41 @@ qse_htb_walk_t add_global (qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
return QSE_HTB_WALK_FORWARD; return QSE_HTB_WALK_FORWARD;
} }
static void* xma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
{
return qse_xma_alloc (mmgr->ctx, size);
}
static void* xma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{
return qse_xma_realloc (mmgr->ctx, ptr, size);
}
static void xma_free (qse_mmgr_t* mmgr, void* ptr)
{
qse_xma_free (mmgr->ctx, ptr);
}
static qse_mmgr_t xma_mmgr = static qse_mmgr_t xma_mmgr =
{ {
(qse_mmgr_alloc_t)qse_xma_alloc, xma_alloc,
(qse_mmgr_realloc_t)qse_xma_realloc, xma_realloc,
(qse_mmgr_free_t)qse_xma_free, xma_free,
QSE_NULL QSE_NULL
}; };
#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_alloc_count = 0;
static qse_ulong_t debug_mmgr_realloc_count = 0; static qse_ulong_t debug_mmgr_realloc_count = 0;
static qse_ulong_t debug_mmgr_free_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 (qse_mmgr_t* mmgr, qse_size_t size)
{ {
void* ptr; void* ptr;
struct arg_t* arg = (struct arg_t*)ctx; struct arg_t* arg = (struct arg_t*)mmgr->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;
ptr = malloc (size); ptr = malloc (size);
@ -929,10 +946,10 @@ static void* debug_mmgr_alloc (void* ctx, qse_size_t size)
return ptr; return ptr;
} }
static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size) static void* debug_mmgr_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{ {
void* rptr; void* rptr;
struct arg_t* arg = (struct arg_t*)ctx; struct arg_t* arg = (struct arg_t*)mmgr->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;
rptr = realloc (ptr, size); rptr = realloc (ptr, size);
@ -944,7 +961,7 @@ static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size)
return rptr; return rptr;
} }
static void debug_mmgr_free (void* ctx, void* ptr) static void debug_mmgr_free (qse_mmgr_t* mmgr, void* ptr)
{ {
debug_mmgr_free_count++; debug_mmgr_free_count++;
free (ptr); free (ptr);
@ -1004,7 +1021,7 @@ static int awk_main (int argc, qse_char_t* argv[])
if (arg.failmalloc > 0) if (arg.failmalloc > 0)
{ {
debug_mmgr.ctx = &arg; debug_mmgr.ctx = &arg;
mmgr = &debug_mmgr; mmgr = &debug_mmgr;
} }
else else
#endif #endif

View File

@ -90,7 +90,7 @@ static qse_ulong_t debug_mmgr_alloc_count = 0;
static qse_ulong_t debug_mmgr_realloc_count = 0; static qse_ulong_t debug_mmgr_realloc_count = 0;
static qse_ulong_t debug_mmgr_free_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 (qse_mmgr_t* mmgr, qse_size_t size)
{ {
void* ptr; void* ptr;
debug_mmgr_count++; debug_mmgr_count++;
@ -100,7 +100,7 @@ static void* debug_mmgr_alloc (void* ctx, qse_size_t size)
return ptr; return ptr;
} }
static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size) static void* debug_mmgr_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{ {
void* rptr; void* rptr;
debug_mmgr_count++; debug_mmgr_count++;
@ -114,7 +114,7 @@ static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size)
return rptr; return rptr;
} }
static void debug_mmgr_free (void* ctx, void* ptr) static void debug_mmgr_free (qse_mmgr_t* mmgr, void* ptr)
{ {
debug_mmgr_free_count++; debug_mmgr_free_count++;
free (ptr); free (ptr);
@ -129,11 +129,26 @@ static qse_mmgr_t debug_mmgr =
}; };
#endif #endif
static void* xma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
{
return qse_xma_alloc (mmgr->ctx, size);
}
static void* xma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{
return qse_xma_realloc (mmgr->ctx, ptr, size);
}
static void xma_free (qse_mmgr_t* mmgr, void* ptr)
{
qse_xma_free (mmgr->ctx, ptr);
}
static qse_mmgr_t xma_mmgr = static qse_mmgr_t xma_mmgr =
{ {
(qse_mmgr_alloc_t)qse_xma_alloc, xma_alloc,
(qse_mmgr_realloc_t)qse_xma_realloc, xma_realloc,
(qse_mmgr_free_t)qse_xma_free, xma_free,
QSE_NULL QSE_NULL
}; };

View File

@ -67,7 +67,7 @@ static qse_ulong_t debug_mmgr_alloc_count = 0;
static qse_ulong_t debug_mmgr_realloc_count = 0; static qse_ulong_t debug_mmgr_realloc_count = 0;
static qse_ulong_t debug_mmgr_free_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 (qse_mmgr_t* mmgr, qse_size_t size)
{ {
void* ptr; void* ptr;
debug_mmgr_count++; debug_mmgr_count++;
@ -77,7 +77,7 @@ static void* debug_mmgr_alloc (void* ctx, qse_size_t size)
return ptr; return ptr;
} }
static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size) static void* debug_mmgr_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{ {
void* rptr; void* rptr;
debug_mmgr_count++; debug_mmgr_count++;
@ -91,7 +91,7 @@ static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size)
return rptr; return rptr;
} }
static void debug_mmgr_free (void* ctx, void* ptr) static void debug_mmgr_free (qse_mmgr_t* mmgr, void* ptr)
{ {
debug_mmgr_free_count++; debug_mmgr_free_count++;
free (ptr); free (ptr);
@ -106,11 +106,26 @@ static qse_mmgr_t debug_mmgr =
}; };
#endif #endif
static void* xma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
{
return qse_xma_alloc (mmgr->ctx, size);
}
static void* xma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{
return qse_xma_realloc (mmgr->ctx, ptr, size);
}
static void xma_free (qse_mmgr_t* mmgr, void* ptr)
{
qse_xma_free (mmgr->ctx, ptr);
}
static qse_mmgr_t xma_mmgr = static qse_mmgr_t xma_mmgr =
{ {
(qse_mmgr_alloc_t)qse_xma_alloc, xma_alloc,
(qse_mmgr_realloc_t)qse_xma_realloc, xma_realloc,
(qse_mmgr_free_t)qse_xma_free, xma_free,
QSE_NULL QSE_NULL
}; };

View File

@ -90,17 +90,17 @@ protected:
/// ///
/// bridge function from the #qse_mmgr_t type the allocMem() function. /// bridge function from the #qse_mmgr_t type the allocMem() function.
/// ///
static void* alloc_mem (void* ctx, size_t n); static void* alloc_mem (mmgr_t* mmgr, size_t n);
/// ///
/// bridge function from the #qse_mmgr_t type the reallocMem() function. /// bridge function from the #qse_mmgr_t type the reallocMem() function.
/// ///
static void* realloc_mem (void* ctx, void* ptr, size_t n); static void* realloc_mem (mmgr_t* mmgr, void* ptr, size_t n);
/// ///
/// bridge function from the #qse_mmgr_t type the freeMem() function. /// bridge function from the #qse_mmgr_t type the freeMem() function.
/// ///
static void free_mem (void* ctx, void* ptr); static void free_mem (mmgr_t* mmgr, void* ptr);
}; };
///////////////////////////////// /////////////////////////////////

View File

@ -42,18 +42,18 @@
* The QSE_MMGR_ALLOC() macro allocates a memory block of the @a size bytes * The QSE_MMGR_ALLOC() macro allocates a memory block of the @a size bytes
* using the @a mmgr memory manager. * using the @a mmgr memory manager.
*/ */
#define QSE_MMGR_ALLOC(mmgr,size) ((mmgr)->alloc((mmgr)->ctx,size)) #define QSE_MMGR_ALLOC(mmgr,size) ((mmgr)->alloc(mmgr,size))
/** /**
* The QSE_MMGR_REALLOC() macro resizes a memory block pointed to by @a ptr * The QSE_MMGR_REALLOC() macro resizes a memory block pointed to by @a ptr
* to the @a size bytes using the @a mmgr memory manager. * to the @a size bytes using the @a mmgr memory manager.
*/ */
#define QSE_MMGR_REALLOC(mmgr,ptr,size) ((mmgr)->realloc((mmgr)->ctx,ptr,size)) #define QSE_MMGR_REALLOC(mmgr,ptr,size) ((mmgr)->realloc(mmgr,ptr,size))
/** /**
* The QSE_MMGR_FREE() macro deallocates the memory block pointed to by @a ptr. * The QSE_MMGR_FREE() macro deallocates the memory block pointed to by @a ptr.
*/ */
#define QSE_MMGR_FREE(mmgr,ptr) ((mmgr)->free((mmgr)->ctx,ptr)) #define QSE_MMGR_FREE(mmgr,ptr) ((mmgr)->free(mmgr,ptr))
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -654,20 +654,23 @@ struct qse_xptl_t
}; };
typedef struct qse_xptl_t qse_xptl_t; typedef struct qse_xptl_t qse_xptl_t;
typedef struct qse_mmgr_t qse_mmgr_t;
/** /**
* allocate a memory chunk of the size @a n. * allocate a memory chunk of the size @a n.
* @return pointer to a memory chunk on success, QSE_NULL on failure. * @return pointer to a memory chunk on success, QSE_NULL on failure.
*/ */
typedef void* (*qse_mmgr_alloc_t) (void* ctx, qse_size_t n); typedef void* (*qse_mmgr_alloc_t) (qse_mmgr_t* mmgr, qse_size_t n);
/** /**
* resize a memory chunk pointed to by @a ptr to the size @a n. * resize a memory chunk pointed to by @a ptr to the size @a n.
* @return pointer to a memory chunk on success, QSE_NULL on failure. * @return pointer to a memory chunk on success, QSE_NULL on failure.
*/ */
typedef void* (*qse_mmgr_realloc_t) (void* ctx, void* ptr, qse_size_t n); typedef void* (*qse_mmgr_realloc_t) (qse_mmgr_t* mmgr, void* ptr, qse_size_t n);
/** /**
* free a memory chunk pointed to by @a ptr. * free a memory chunk pointed to by @a ptr.
*/ */
typedef void (*qse_mmgr_free_t) (void* ctx, void* ptr); typedef void (*qse_mmgr_free_t) (qse_mmgr_t* mmgr, void* ptr);
/** /**
* The qse_mmgr_t type defines the memory management interface. * The qse_mmgr_t type defines the memory management interface.
@ -687,7 +690,6 @@ struct qse_mmgr_t
qse_mmgr_free_t free; /**< disposal function */ qse_mmgr_free_t free; /**< disposal function */
void* ctx; /**< user-defined data pointer */ void* ctx; /**< user-defined data pointer */
}; };
typedef struct qse_mmgr_t qse_mmgr_t;
typedef qse_size_t (*qse_cmgr_mbtowc_t) ( typedef qse_size_t (*qse_cmgr_mbtowc_t) (
const qse_mchar_t* mb, const qse_mchar_t* mb,

View File

@ -262,6 +262,7 @@ int StdAwk::__build_environ (Run* run, void* envptr)
if (kptr == QSE_NULL || vptr == QSE_NULL) if (kptr == QSE_NULL || vptr == QSE_NULL)
{ {
if (kptr) QSE_MMGR_FREE (mmgr, kptr); if (kptr) QSE_MMGR_FREE (mmgr, kptr);
if (vptr) QSE_MMGR_FREE (mmgr, vptr);
/* mbstowcsdup() may fail for invalid encoding. /* mbstowcsdup() may fail for invalid encoding.
* so setting the error code to ENOMEM may not * so setting the error code to ENOMEM may not
@ -282,6 +283,7 @@ int StdAwk::__build_environ (Run* run, void* envptr)
if (kptr == QSE_NULL || vptr == QSE_NULL) if (kptr == QSE_NULL || vptr == QSE_NULL)
{ {
if (kptr) QSE_MMGR_FREE (mmgr, kptr); if (kptr) QSE_MMGR_FREE (mmgr, kptr);
if (vptr) QSE_MMGR_FREE (mmgr, vptr);
/* mbstowcsdup() may fail for invalid encoding. /* mbstowcsdup() may fail for invalid encoding.
* so setting the error code to ENOMEM may not * so setting the error code to ENOMEM may not

View File

@ -1769,11 +1769,12 @@ static int __build_environ (
if (kptr == QSE_NULL || vptr == QSE_NULL) if (kptr == QSE_NULL || vptr == QSE_NULL)
{ {
if (kptr) QSE_MMGR_FREE (rtx->awk->mmgr, kptr); if (kptr) QSE_MMGR_FREE (rtx->awk->mmgr, kptr);
if (vptr) QSE_MMGR_FREE (rtx->awk->mmgr, vptr);
qse_awk_rtx_refdownval (rtx, v_env); qse_awk_rtx_refdownval (rtx, v_env);
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1; return -1;
} }
*eq = QSE_MT('='); *eq = QSE_MT('=');
#else #else
@ -1787,6 +1788,7 @@ static int __build_environ (
if (kptr == QSE_NULL || vptr == QSE_NULL) if (kptr == QSE_NULL || vptr == QSE_NULL)
{ {
if (kptr) QSE_MMGR_FREE (rtx->awk->mmgr, kptr); if (kptr) QSE_MMGR_FREE (rtx->awk->mmgr, kptr);
if (vptr) QSE_MMGR_FREE (rtx->awk->mmgr, vptr);
qse_awk_rtx_refdownval (rtx, v_env); qse_awk_rtx_refdownval (rtx, v_env);
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL); qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);

View File

@ -24,19 +24,19 @@
QSE_BEGIN_NAMESPACE(QSE) QSE_BEGIN_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////
void* Mmgr::alloc_mem (void* ctx, size_t n) void* Mmgr::alloc_mem (mmgr_t* mmgr, size_t n)
{ {
return ((Mmgr*)ctx)->allocMem (n); return ((Mmgr*)mmgr->ctx)->allocMem (n);
} }
void* Mmgr::realloc_mem (void* ctx, void* ptr, size_t n) void* Mmgr::realloc_mem (mmgr_t* mmgr, void* ptr, size_t n)
{ {
return ((Mmgr*)ctx)->reallocMem (ptr, n); return ((Mmgr*)mmgr->ctx)->reallocMem (ptr, n);
} }
void Mmgr::free_mem (void* ctx, void* ptr) void Mmgr::free_mem (mmgr_t* mmgr, void* ptr)
{ {
((Mmgr*)ctx)->freeMem (ptr); ((Mmgr*)mmgr->ctx)->freeMem (ptr);
} }
///////////////////////////////// /////////////////////////////////

View File

@ -22,6 +22,12 @@
#if defined(_WIN32) #if defined(_WIN32)
# include <windows.h> # include <windows.h>
/*
#elif defined(__OS2__)
# define INCL_DOSMEMMGR
# include <os2.h>
# include <bsememf.h>
*/
#else #else
# include <stdlib.h> # include <stdlib.h>
#endif #endif
@ -439,7 +445,7 @@ void* qse_memrmem (const void* hs, qse_size_t hl, const void* nd, qse_size_t nl)
return QSE_NULL; return QSE_NULL;
} }
static void* mmgr_alloc (void* data, qse_size_t n) static void* mmgr_alloc (qse_mmgr_t* mmgr, qse_size_t n)
{ {
#if defined(_WIN32) #if defined(_WIN32)
HANDLE heap; HANDLE heap;
@ -452,7 +458,7 @@ static void* mmgr_alloc (void* data, qse_size_t n)
#endif #endif
} }
static void* mmgr_realloc (void* data, void* ptr, qse_size_t n) static void* mmgr_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t n)
{ {
#if defined(_WIN32) #if defined(_WIN32)
HANDLE heap; HANDLE heap;
@ -466,7 +472,7 @@ static void* mmgr_realloc (void* data, void* ptr, qse_size_t n)
#endif #endif
} }
static void mmgr_free (void* data, void* ptr) static void mmgr_free (qse_mmgr_t* mmgr, void* ptr)
{ {
#if defined(_WIN32) #if defined(_WIN32)
HANDLE heap; HANDLE heap;