fixed bugs in xma.c

This commit is contained in:
hyung-hwan 2010-07-29 07:27:03 +00:00
parent f467ec9930
commit a1965a6544
8 changed files with 252 additions and 94 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c 336 2010-07-24 12:43:26Z hyunghwan.chung $ * $Id: awk.c 337 2010-07-28 13:27:03Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -69,6 +69,7 @@ struct arg_t
int opton; int opton;
int optoff; int optoff;
qse_ulong_t memlimit;
}; };
struct gvmv_t struct gvmv_t
@ -358,6 +359,7 @@ static void print_usage (QSE_FILE* out, const qse_char_t* argv0)
qse_fprintf (out, QSE_T(" -o/--deparsed-file deparsedfile set the deparsing output file\n")); qse_fprintf (out, QSE_T(" -o/--deparsed-file deparsedfile set the deparsing output file\n"));
qse_fprintf (out, QSE_T(" -F/--field-separator string set a field separator(FS)\n")); qse_fprintf (out, QSE_T(" -F/--field-separator string set a field separator(FS)\n"));
qse_fprintf (out, QSE_T(" -v/--assign var=value add a global variable with a value\n")); qse_fprintf (out, QSE_T(" -v/--assign var=value add a global variable with a value\n"));
qse_fprintf (out, QSE_T(" -m/--memory-limit number limit the memory usage (bytes)\n"));
for (j = 0; opttab[j].name != QSE_NULL; j++) for (j = 0; opttab[j].name != QSE_NULL; j++)
{ {
@ -392,13 +394,14 @@ static int comparg (int argc, qse_char_t* argv[], struct arg_t* arg)
{ QSE_T(":field-separator"), QSE_T('F') }, { QSE_T(":field-separator"), QSE_T('F') },
{ QSE_T(":deparsed-file"), QSE_T('o') }, { QSE_T(":deparsed-file"), QSE_T('o') },
{ QSE_T(":assign"), QSE_T('v') }, { QSE_T(":assign"), QSE_T('v') },
{ QSE_T(":memory-limit"), QSE_T('m') },
{ QSE_T("help"), QSE_T('h') } { QSE_T("help"), QSE_T('h') }
}; };
static qse_opt_t opt = static qse_opt_t opt =
{ {
QSE_T("dc:f:F:o:v:h"), QSE_T("dc:f:F:o:v:m:h"),
lng lng
}; };
@ -520,6 +523,12 @@ static int comparg (int argc, qse_char_t* argv[], struct arg_t* arg)
break; break;
} }
case QSE_T('m'):
{
arg->memlimit = qse_strtoulong (opt.arg);
break;
}
case QSE_T('\0'): case QSE_T('\0'):
{ {
/* a long option with no corresponding short option */ /* a long option with no corresponding short option */
@ -688,7 +697,15 @@ 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 int real_awk_main (int argc, qse_char_t* argv[], qse_mmgr_t* mmgr) static qse_mmgr_t xma_mmgr =
{
qse_xma_alloc,
QSE_NULL,
qse_xma_free,
QSE_NULL
};
static int awk_main (int argc, qse_char_t* argv[])
{ {
qse_awk_t* awk = QSE_NULL; qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL; qse_awk_rtx_t* rtx = QSE_NULL;
@ -696,7 +713,6 @@ static int real_awk_main (int argc, qse_char_t* argv[], qse_mmgr_t* mmgr)
#if 0 #if 0
qse_awk_rcb_t rcb; qse_awk_rcb_t rcb;
#endif #endif
int i; int i;
struct arg_t arg; struct arg_t arg;
int ret = -1; int ret = -1;
@ -704,6 +720,7 @@ static int real_awk_main (int argc, qse_char_t* argv[], qse_mmgr_t* mmgr)
/* TODO: change it to support multiple source files */ /* TODO: change it to support multiple source files */
qse_awk_parsestd_in_t psin; qse_awk_parsestd_in_t psin;
qse_awk_parsestd_out_t psout; qse_awk_parsestd_out_t psout;
qse_mmgr_t* mmgr = QSE_NULL;
memset (&arg, 0, QSE_SIZEOF(arg)); memset (&arg, 0, QSE_SIZEOF(arg));
@ -724,8 +741,19 @@ static int real_awk_main (int argc, qse_char_t* argv[], qse_mmgr_t* mmgr)
psout.u.file = arg.osf; psout.u.file = arg.osf;
} }
if (arg.memlimit > 0)
{
xma_mmgr.udd = qse_xma_open (QSE_NULL, 0, arg.memlimit);
if (xma_mmgr.udd == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open memory heap\n"));
goto oops;
}
mmgr = &xma_mmgr;
}
awk = qse_awk_openstdwithmmgr (mmgr, 0); awk = qse_awk_openstdwithmmgr (mmgr, 0);
//awk = qse_awk_openstd (0); /*awk = qse_awk_openstd (0);*/
if (awk == QSE_NULL) if (awk == QSE_NULL)
{ {
qse_printf (QSE_T("ERROR: cannot open awk\n")); qse_printf (QSE_T("ERROR: cannot open awk\n"));
@ -814,38 +842,14 @@ static int real_awk_main (int argc, qse_char_t* argv[], qse_mmgr_t* mmgr)
} }
oops: oops:
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx); if (rtx) qse_awk_rtx_close (rtx);
if (awk != QSE_NULL) qse_awk_close (awk); if (awk) qse_awk_close (awk);
if (xma_mmgr.udd) qse_xma_close (xma_mmgr.udd);
freearg (&arg); freearg (&arg);
return ret; return ret;
} }
static int awk_main (int argc, qse_char_t* argv[])
{
int n;
qse_mmgr_t mmgr =
{
qse_xma_alloc,
QSE_NULL,
qse_xma_free,
QSE_NULL
};
mmgr.udd = qse_xma_open (QSE_NULL, 0, 1000000);
if (mmgr.udd == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open memory heap\n"));
return -1;
}
n = real_awk_main (argc, argv, &mmgr);
qse_xma_close (mmgr.udd);
return n;
}
int qse_main (int argc, qse_achar_t* argv[]) int qse_main (int argc, qse_achar_t* argv[])
{ {
return qse_runmain (argc, argv, awk_main); return qse_runmain (argc, argv, awk_main);

View File

@ -24,6 +24,7 @@
#include <qse/cmn/chr.h> #include <qse/cmn/chr.h>
#include <qse/cmn/opt.h> #include <qse/cmn/opt.h>
#include <qse/cmn/sio.h> #include <qse/cmn/sio.h>
#include <qse/cmn/xma.h>
#include <qse/cmn/misc.h> #include <qse/cmn/misc.h>
#include <qse/cmn/stdio.h> #include <qse/cmn/stdio.h>
#include <qse/cmn/main.h> #include <qse/cmn/main.h>
@ -32,6 +33,15 @@ static const qse_char_t* g_script_file = QSE_NULL;
static qse_char_t* g_script = QSE_NULL; static qse_char_t* g_script = QSE_NULL;
static const qse_char_t* g_infile = QSE_NULL; static const qse_char_t* g_infile = QSE_NULL;
static int g_option = 0; static int g_option = 0;
static qse_ulong_t g_memlimit = 0;
static qse_mmgr_t xma_mmgr =
{
qse_xma_alloc,
QSE_NULL,
qse_xma_free,
QSE_NULL
};
static qse_ssize_t in ( static qse_ssize_t in (
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_t* sed, qse_sed_io_cmd_t cmd,
@ -130,14 +140,15 @@ static void print_usage (QSE_FILE* out, int argc, qse_char_t* argv[])
qse_fprintf (out, QSE_T(" -r allow {n,m} in a regular expression\n")); qse_fprintf (out, QSE_T(" -r allow {n,m} in a regular expression\n"));
qse_fprintf (out, QSE_T(" -s allow text on the same line as c, a, i\n")); qse_fprintf (out, QSE_T(" -s allow text on the same line as c, a, i\n"));
qse_fprintf (out, QSE_T(" -l ensure a newline at text end\n")); qse_fprintf (out, QSE_T(" -l ensure a newline at text end\n"));
qse_fprintf (out, QSE_T(" -f file specifie a s script file\n")); qse_fprintf (out, QSE_T(" -f file specify a script file\n"));
qse_fprintf (out, QSE_T(" -m number specify the maximum amount of memory to use in bytes\n"));
} }
static int handle_args (int argc, qse_char_t* argv[]) static int handle_args (int argc, qse_char_t* argv[])
{ {
static qse_opt_t opt = static qse_opt_t opt =
{ {
QSE_T("hnarslf:"), QSE_T("hnarslf:m:"),
QSE_NULL QSE_NULL
}; };
qse_cint_t c; qse_cint_t c;
@ -193,6 +204,10 @@ static int handle_args (int argc, qse_char_t* argv[])
case QSE_T('f'): case QSE_T('f'):
g_script_file = opt.arg; g_script_file = opt.arg;
break; break;
case QSE_T('m'):
g_memlimit = qse_strtoulong (opt.arg);
break;
} }
} }
@ -248,6 +263,7 @@ qse_char_t* load_script_file (const qse_char_t* file)
int sed_main (int argc, qse_char_t* argv[]) int sed_main (int argc, qse_char_t* argv[])
{ {
qse_mmgr_t* mmgr = QSE_NULL;
qse_sed_t* sed = QSE_NULL; qse_sed_t* sed = QSE_NULL;
int ret = -1; int ret = -1;
@ -257,7 +273,18 @@ int sed_main (int argc, qse_char_t* argv[])
ret = -1; ret = -1;
sed = qse_sed_open (QSE_NULL, 0); if (g_memlimit > 0)
{
xma_mmgr.udd = qse_xma_open (QSE_NULL, 0, g_memlimit);
if (xma_mmgr.udd == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open memory heap\n"));
goto oops;
}
mmgr = &xma_mmgr;
}
sed = qse_sed_open (mmgr, 0);
if (sed == QSE_NULL) if (sed == QSE_NULL)
{ {
qse_fprintf (QSE_STDERR, QSE_T("cannot open a stream editor\n")); qse_fprintf (QSE_STDERR, QSE_T("cannot open a stream editor\n"));
@ -325,7 +352,8 @@ int sed_main (int argc, qse_char_t* argv[])
ret = 0; ret = 0;
oops: oops:
if (sed != QSE_NULL) qse_sed_close (sed); if (sed) qse_sed_close (sed);
if (xma_mmgr.udd) qse_xma_close (xma_mmgr.udd);
if (g_script_file != QSE_NULL && g_script != QSE_NULL) if (g_script_file != QSE_NULL && g_script != QSE_NULL)
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), g_script); QSE_MMGR_FREE (QSE_MMGR_GETDFL(), g_script);
return ret; return ret;

View File

@ -27,13 +27,18 @@
typedef struct qse_xma_t qse_xma_t; typedef struct qse_xma_t qse_xma_t;
typedef struct qse_xma_blk_t qse_xma_blk_t; typedef struct qse_xma_blk_t qse_xma_blk_t;
#define QSE_XMA_FIXED 32
#define QSE_XMA_SIZE_BITS ((QSE_SIZEOF_SIZE_T*8)-1)
struct qse_xma_t struct qse_xma_t
{ {
QSE_DEFINE_COMMON_FIELDS (xma) QSE_DEFINE_COMMON_FIELDS (xma)
qse_xma_blk_t* head; qse_xma_blk_t* head;
qse_xma_blk_t* xfree[100]; qse_xma_blk_t* xfree[QSE_XMA_FIXED + QSE_XMA_SIZE_BITS + 1];
qse_size_t bdec;
#ifdef QSE_XMA_ENABLE_STAT
struct struct
{ {
qse_size_t total; qse_size_t total;
@ -42,6 +47,7 @@ struct qse_xma_t
qse_size_t nused; qse_size_t nused;
qse_size_t nfree; qse_size_t nfree;
} stat; } stat;
#endif
}; };
#ifdef __cplusplus #ifdef __cplusplus
@ -75,6 +81,12 @@ void* qse_xma_alloc (
qse_size_t size qse_size_t size
); );
void* qse_xma_realloc (
qse_xma_t* xma,
void* b,
qse_size_t size
);
void qse_xma_free ( void qse_xma_free (
qse_xma_t* xma, qse_xma_t* xma,
void* b void* b

View File

@ -1,5 +1,5 @@
/* /*
* $Id: types.h 287 2009-09-15 10:01:02Z hyunghwan.chung $ * $Id: types.h 337 2010-07-28 13:27:03Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -239,10 +239,12 @@ typedef qse_uint8_t qse_byte_t;
* The qse_size_t type defines an unsigned integer type that is as large as * The qse_size_t type defines an unsigned integer type that is as large as
* to hold a pointer value. * to hold a pointer value.
*/ */
#ifdef __SIZE_TYPE__ #if defined(__SIZE_TYPE__) && defined(__SIZEOF_SIZE_T__)
typedef __SIZE_TYPE__ qse_size_t; typedef __SIZE_TYPE__ qse_size_t;
# define QSE_SIZEOF_SIZE_T __SIZEOF_SIZE_T__
#else #else
typedef qse_uint_t qse_size_t; typedef qse_uint_t qse_size_t;
# define QSE_SIZEOF_SIZE_T QSE_SIZEOF_INT_T
#endif #endif
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* $Id: syscall.h 287 2009-09-15 10:01:02Z hyunghwan.chung $ * $Id: syscall.h 337 2010-07-28 13:27:03Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -50,6 +50,9 @@
#ifdef HAVE_SYS_RESOURCE_H #ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h> # include <sys/resource.h>
#endif #endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#if defined(QSE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H) #if defined(QSE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H)
# include <sys/syscall.h> # include <sys/syscall.h>

View File

@ -27,12 +27,21 @@
#define SYS_TO_USR(_) (((qse_xma_blk_t*)_) + 1) #define SYS_TO_USR(_) (((qse_xma_blk_t*)_) + 1)
#define USR_TO_SYS(_) (((qse_xma_blk_t*)_) - 1) #define USR_TO_SYS(_) (((qse_xma_blk_t*)_) - 1)
/*
* the xfree array is divided into three region
* 0 ....................... FIXED ......................... XFIMAX-1 ... XFIMAX
* | small fixed-size chains | large chains | huge chain |
*/
#define FIXED QSE_XMA_FIXED
#define XFIMAX(xma) (QSE_COUNTOF(xma->xfree)-1) #define XFIMAX(xma) (QSE_COUNTOF(xma->xfree)-1)
#define _XFI(size) (((size) / ALIGN) - 1)
#define _XFI_LARGE(xma,size) (szlog2(size) - (xma)->bdec + FIXED)
#define XFI(xma,size) ((_XFI(size) < FIXED)? _XFI(size): _XFI_LARGE(xma,size))
struct qse_xma_blk_t struct qse_xma_blk_t
{ {
qse_size_t avail: 1; qse_size_t avail: 1;
qse_size_t size: (sizeof(qse_size_t)*8)-1; /**< block size */ qse_size_t size: QSE_XMA_SIZE_BITS;/**< block size */
struct struct
{ {
@ -49,6 +58,46 @@ struct qse_xma_blk_t
QSE_IMPLEMENT_COMMON_FUNCTIONS (xma) QSE_IMPLEMENT_COMMON_FUNCTIONS (xma)
static QSE_INLINE qse_size_t szlog2 (qse_size_t n)
{
/*
* 2**x = n;
* x = log2(n);
* -------------------------------------------
* unsigned int x = 0;
* while((n >> x) > 1) ++x;
* return x;
*/
#define BITS (QSE_SIZEOF_SIZE_T * 8)
int x = BITS - 1;
#if QSE_SIZEOF_SIZE_T >= 32
# error qse_size_t too large. unsupported platform
#endif
#if QSE_SIZEOF_SIZE_T >= 16
if ((n & (~(qse_size_t)0 << (BITS-64))) == 0) { x -= 64; n <<= 64; }
#endif
#if QSE_SIZEOF_SIZE_T >= 8
if ((n & (~(qse_size_t)0 << (BITS-32))) == 0) { x -= 32; n <<= 32; }
#endif
#if QSE_SIZEOF_SIZE_T >= 4
if ((n & (~(qse_size_t)0 << (BITS-16))) == 0) { x -= 16; n <<= 16; }
#endif
#if QSE_SIZEOF_SIZE_T >= 2
if ((n & (~(qse_size_t)0 << (BITS-8))) == 0) { x -= 8; n <<= 8; }
#endif
#if QSE_SIZEOF_SIZE_T >= 1
if ((n & (~(qse_size_t)0 << (BITS-4))) == 0) { x -= 4; n <<= 4; }
#endif
if ((n & (~(qse_size_t)0 << (BITS-2))) == 0) { x -= 2; n <<= 2; }
if ((n & (~(qse_size_t)0 << (BITS-1))) == 0) { x -= 1; }
return x;
#undef BITS
}
qse_xma_t* qse_xma_open (qse_mmgr_t* mmgr, qse_size_t ext, qse_size_t size) qse_xma_t* qse_xma_open (qse_mmgr_t* mmgr, qse_size_t ext, qse_size_t size)
{ {
qse_xma_t* xma; qse_xma_t* xma;
@ -84,34 +133,41 @@ void qse_xma_close (qse_xma_t* xma)
qse_xma_t* qse_xma_init (qse_xma_t* xma, qse_mmgr_t* mmgr, qse_size_t size) qse_xma_t* qse_xma_init (qse_xma_t* xma, qse_mmgr_t* mmgr, qse_size_t size)
{ {
qse_xma_blk_t* free; qse_xma_blk_t* free;
qse_size_t xfi;
/* adjust 'size' to be large enough to hold a single smallest block */
if (size < HDRSIZE + ALIGN) size = HDRSIZE + ALIGN; if (size < HDRSIZE + ALIGN) size = HDRSIZE + ALIGN;
/* allocate a memory chunk to use for actual memory allocation */
free = QSE_MMGR_ALLOC (mmgr, size); free = QSE_MMGR_ALLOC (mmgr, size);
if (free == QSE_NULL) return QSE_NULL; if (free == QSE_NULL) return QSE_NULL;
/* make the entire region available. the allocatable block begins /* initialize the header part of the free chunk */
* past the xma structure */ free->avail = 1;
free->size = size - HDRSIZE; /* size excluding the block header */ free->size = size - HDRSIZE; /* size excluding the block header */
free->f.prev = QSE_NULL; free->f.prev = QSE_NULL;
free->f.next = QSE_NULL; free->f.next = QSE_NULL;
free->b.next = QSE_NULL; free->b.next = QSE_NULL;
free->b.prev = QSE_NULL; free->b.prev = QSE_NULL;
free->avail = 1;
QSE_MEMSET (xma, 0, QSE_SIZEOF(*xma)); QSE_MEMSET (xma, 0, QSE_SIZEOF(*xma));
xma->mmgr = mmgr; xma->mmgr = mmgr;
xma->bdec = szlog2(FIXED*ALIGN); /* precalculate the decrement value */
/* the entire region is a free block */ /* the entire chunk is a free block */
xma->xfree[XFIMAX(xma)] = free; xfi = XFI(xma,free->size);
xma->head = free; if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma);
xma->xfree[xfi] = free; /* locate it at the right slot */
xma->head = free; /* store it for furture reference */
/* initialize some statistical variables */ /* initialize some statistical variables */
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.total = size; xma->stat.total = size;
xma->stat.alloc = 0; xma->stat.alloc = 0;
xma->stat.avail = size - HDRSIZE; xma->stat.avail = size - HDRSIZE;
xma->stat.nfree = 1; xma->stat.nfree = 1;
xma->stat.nused = 0; xma->stat.nused = 0;
#endif
return xma; return xma;
} }
@ -121,9 +177,9 @@ void qse_xma_fini (qse_xma_t* xma)
QSE_MMGR_FREE (xma->mmgr, xma->head); QSE_MMGR_FREE (xma->mmgr, xma->head);
} }
static QSE_INLINE void attach_block_to_freelist (qse_xma_t* xma, qse_xma_blk_t* b) static QSE_INLINE void attach_to_freelist (qse_xma_t* xma, qse_xma_blk_t* b)
{ {
qse_size_t xfi = (b->size / ALIGN) - 1; qse_size_t xfi = XFI(xma,b->size);
if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma); if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma);
b->f.prev = QSE_NULL; b->f.prev = QSE_NULL;
@ -132,21 +188,26 @@ static QSE_INLINE void attach_block_to_freelist (qse_xma_t* xma, qse_xma_blk_t*
xma->xfree[xfi] = b; xma->xfree[xfi] = b;
} }
static QSE_INLINE void detach_block_from_freelist (qse_xma_t* xma, qse_xma_blk_t* b) static QSE_INLINE void detach_from_freelist (qse_xma_t* xma, qse_xma_blk_t* b)
{ {
qse_xma_blk_t* x, * y; qse_xma_blk_t* p, * n;
x = b->f.prev; p = b->f.prev;
y = b->f.next; n = b->f.next;
if (x) x->f.next = y; if (p)
{
p->f.next = n;
}
else else
{ {
qse_size_t xfi = (b->size / ALIGN) - 1; qse_size_t xfi = XFI(xma,b->size);
if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma); if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma);
xma->xfree[xfi] = y;
QSE_ASSERT (b == xma->xfree[xfi]);
xma->xfree[xfi] = n;
} }
if (y) y->f.prev = x; if (n) n->f.prev = p;
} }
static qse_xma_blk_t* alloc_from_freelist ( static qse_xma_blk_t* alloc_from_freelist (
@ -158,45 +219,48 @@ static qse_xma_blk_t* alloc_from_freelist (
{ {
if (free->size >= size) if (free->size >= size)
{ {
qse_size_t rem; detach_from_freelist (xma, free);
rem = free->size - size; qse_size_t rem = free->size - size;
if (rem > HDRSIZE) if (rem > HDRSIZE)
{ {
qse_xma_blk_t* tmp; qse_xma_blk_t* tmp;
/* the remaining part is large enough to hold /* the remaining part is large enough to hold
* another block. let's split it */ * another block. let's split it
*/
/* shrink the size of the 'free' block */
free->size = size;
/* let 'tmp' point to the remaining part */
tmp = (qse_xma_blk_t*)(((qse_byte_t*)(free + 1)) + size); tmp = (qse_xma_blk_t*)(((qse_byte_t*)(free + 1)) + size);
/* initialize some fields */
tmp->avail = 1; tmp->avail = 1;
tmp->size = rem - HDRSIZE; tmp->size = rem - HDRSIZE;
/* link 'tmp' to the block list */
tmp->b.next = free->b.next; tmp->b.next = free->b.next;
tmp->b.prev = free; tmp->b.prev = free;
if (free->b.next) free->b.next->b.prev = tmp; if (free->b.next) free->b.next->b.prev = tmp;
free->b.next = tmp; free->b.next = tmp;
free->size = size;
tmp->f.next = free->f.next; /* add the remaining part to the free list */
free->f.next = tmp; attach_to_freelist (xma, tmp);
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.avail -= HDRSIZE; xma->stat.avail -= HDRSIZE;
#endif
} }
#ifdef QSE_XMA_ENABLE_STAT
else else
{ {
/* decrement the number of free blocks as the current /* decrement the number of free blocks as the current
* block is allocated as a whole without being split */ * block is allocated as a whole without being split */
xma->stat.nfree--; xma->stat.nfree--;
} }
#endif
if (free->f.next) free->f.next->f.prev = free->f.prev;
if (free->f.prev) free->f.prev->f.next = free->f.next;
else
{
QSE_ASSERT (xma->xfree[xfi] == free);
xma->xfree[xfi] = free->f.next;
}
free->avail = 0; free->avail = 0;
/* /*
@ -204,9 +268,11 @@ static qse_xma_blk_t* alloc_from_freelist (
free->f.prev = QSE_NULL; free->f.prev = QSE_NULL;
*/ */
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.nused++; xma->stat.nused++;
xma->stat.alloc += free->size; xma->stat.alloc += free->size;
xma->stat.avail -= free->size; xma->stat.avail -= free->size;
#endif
return free; return free;
} }
} }
@ -226,38 +292,61 @@ void* qse_xma_alloc (qse_xma_t* xma, qse_size_t size)
size = ((size + ALIGN - 1) / ALIGN) * ALIGN; size = ((size + ALIGN - 1) / ALIGN) * ALIGN;
QSE_ASSERT (size >= ALIGN); QSE_ASSERT (size >= ALIGN);
xfi = (size / ALIGN) - 1; xfi = XFI(xma,size);
if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma); if (xfi > XFIMAX(xma)) xfi = XFIMAX(xma);
if (xfi < XFIMAX(xma) && xma->xfree[xfi]) /*if (xfi < XFIMAX(xma) && xma->xfree[xfi])*/
if (xfi < FIXED && xma->xfree[xfi])
{ {
/* try the best fit */ /* try the best fit */
//printf ("FOUND A BEST FIT...............\n");
free = xma->xfree[xfi]; free = xma->xfree[xfi];
QSE_ASSERT (free->avail != 0); QSE_ASSERT (free->avail != 0);
QSE_ASSERT (free->size == size); QSE_ASSERT (free->size == size);
detach_block_from_freelist (xma, free); detach_from_freelist (xma, free);
free->avail = 0; free->avail = 0;
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.nfree--; xma->stat.nfree--;
xma->stat.nused++; xma->stat.nused++;
xma->stat.alloc += free->size; xma->stat.alloc += free->size;
xma->stat.avail -= free->size; xma->stat.avail -= free->size;
#endif
}
else if (xfi == XFIMAX(xma))
{
/* huge block */
free = alloc_from_freelist (xma, XFIMAX(xma), size);
if (free == QSE_NULL) return QSE_NULL;
} }
else else
{ {
/* traverses the chain of free blocks */ if (xfi >= FIXED)
free = alloc_from_freelist (xma, XFIMAX(xma), size); {
/* get the block from its own large chain */
free = alloc_from_freelist (xma, xfi, size);
if (free == QSE_NULL) if (free == QSE_NULL)
{ {
/* borrow a large block from the huge block chain */
free = alloc_from_freelist (xma, XFIMAX(xma), size);
}
}
else
{
/* borrow a small block from the huge block chain */
free = alloc_from_freelist (xma, XFIMAX(xma), size);
if (free == QSE_NULL) xfi = FIXED - 1;
}
if (free == QSE_NULL)
{
/* try each large block chain left */
for (++xfi; xfi < XFIMAX(xma) - 1; xfi++) for (++xfi; xfi < XFIMAX(xma) - 1; xfi++)
{ {
free = alloc_from_freelist (xma, xfi, size); free = alloc_from_freelist (xma, xfi, size);
if (free != QSE_NULL) break; if (free != QSE_NULL) break;
} }
if (free == QSE_NULL) return QSE_NULL; if (free == QSE_NULL) return QSE_NULL;
} }
} }
@ -265,15 +354,23 @@ void* qse_xma_alloc (qse_xma_t* xma, qse_size_t size)
return SYS_TO_USR(free); return SYS_TO_USR(free);
} }
void* qse_xma_realloc (qse_xma_t* xma, void* b, qse_size_t size)
{
/* TODO */
return QSE_NULL;
}
void qse_xma_free (qse_xma_t* xma, void* b) void qse_xma_free (qse_xma_t* xma, void* b)
{ {
qse_xma_blk_t* blk = USR_TO_SYS(b); qse_xma_blk_t* blk = USR_TO_SYS(b);
/*QSE_ASSERT (blk->f.next == QSE_NULL);*/ /*QSE_ASSERT (blk->f.next == QSE_NULL);*/
#ifdef QSE_XMA_ENABLE_STAT
/* update statistical variables */ /* update statistical variables */
xma->stat.nused--; xma->stat.nused--;
xma->stat.alloc -= blk->size; xma->stat.alloc -= blk->size;
#endif
if ((blk->b.prev && blk->b.prev->avail) && if ((blk->b.prev && blk->b.prev->avail) &&
(blk->b.next && blk->b.next->avail)) (blk->b.next && blk->b.next->avail))
@ -304,17 +401,19 @@ void qse_xma_free (qse_xma_t* xma, void* b)
qse_xma_blk_t* z = y->b.next; qse_xma_blk_t* z = y->b.next;
qse_size_t bs = blk->size + HDRSIZE + y->size + HDRSIZE; qse_size_t bs = blk->size + HDRSIZE + y->size + HDRSIZE;
detach_block_from_freelist (xma, x); detach_from_freelist (xma, x);
detach_block_from_freelist (xma, y); detach_from_freelist (xma, y);
x->size += bs; x->size += bs;
x->b.next = z; x->b.next = z;
if (z) z->b.prev = x; if (z) z->b.prev = x;
attach_block_to_freelist (xma, x); attach_to_freelist (xma, x);
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.nfree--; xma->stat.nfree--;
xma->stat.avail += bs; xma->stat.avail += bs;
#endif
} }
else if (blk->b.next && blk->b.next->avail) else if (blk->b.next && blk->b.next->avail)
{ {
@ -344,7 +443,7 @@ void qse_xma_free (qse_xma_t* xma, void* b)
qse_size_t bs = x->size + HDRSIZE; qse_size_t bs = x->size + HDRSIZE;
/* detach x from the free list */ /* detach x from the free list */
detach_block_from_freelist (xma, x); detach_from_freelist (xma, x);
/* update the block availability */ /* update the block availability */
blk->avail = 1; blk->avail = 1;
@ -357,9 +456,11 @@ void qse_xma_free (qse_xma_t* xma, void* b)
blk->b.next = y; blk->b.next = y;
/* attach blk to the free list */ /* attach blk to the free list */
attach_block_to_freelist (xma, blk); attach_to_freelist (xma, blk);
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.avail += bs; xma->stat.avail += bs;
#endif
} }
else if (blk->b.prev && blk->b.prev->avail) else if (blk->b.prev && blk->b.prev->avail)
{ {
@ -389,26 +490,31 @@ void qse_xma_free (qse_xma_t* xma, void* b)
qse_xma_blk_t* y = blk->b.next; qse_xma_blk_t* y = blk->b.next;
qse_size_t bs = blk->size + HDRSIZE; qse_size_t bs = blk->size + HDRSIZE;
detach_block_from_freelist (xma, x); detach_from_freelist (xma, x);
x->size += blk->size + HDRSIZE; x->size += bs;
x->b.next = y; x->b.next = y;
if (y) y->b.prev = x; if (y) y->b.prev = x;
attach_block_to_freelist (xma, x); attach_to_freelist (xma, x);
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.avail += bs; xma->stat.avail += bs;
#endif
} }
else else
{ {
blk->avail = 1; blk->avail = 1;
attach_block_to_freelist (xma, blk); attach_to_freelist (xma, blk);
#ifdef QSE_XMA_ENABLE_STAT
xma->stat.nfree++; xma->stat.nfree++;
xma->stat.avail += blk->size; xma->stat.avail += blk->size;
#endif
} }
} }
#if 0
void qse_xma_dump (qse_xma_t* xma) void qse_xma_dump (qse_xma_t* xma)
{ {
qse_xma_blk_t* tmp; qse_xma_blk_t* tmp;
@ -438,7 +544,6 @@ void qse_xma_dump (qse_xma_t* xma)
printf ("Total : %18llu bytes\n", asum + fsum + isum); printf ("Total : %18llu bytes\n", asum + fsum + isum);
} }
#if 0
int main () int main ()
{ {
int i; int i;

View File

@ -48,6 +48,8 @@ print_usage()
TMPFILE="${TMPFILE:=./regress.temp}" TMPFILE="${TMPFILE:=./regress.temp}"
OUTFILE="${OUTFILE:=./regress.out}" OUTFILE="${OUTFILE:=./regress.out}"
GLOBALOPTS="-m 500000"
PROGS=" PROGS="
cou-001.awk!cou.dat!! cou-001.awk!cou.dat!!
cou-002.awk!cou.dat!! cou-002.awk!cou.dat!!
@ -198,8 +200,8 @@ run_scripts()
[ -z "${redinfile}" ] && redinfile="/dev/stdin" [ -z "${redinfile}" ] && redinfile="/dev/stdin"
echo_title "${valgrind} ${QSEAWK} ${awkopts} -f ${orgscript} ${datafile} <${redinfile} 2>&1" echo_title "${valgrind} ${QSEAWK} ${GLOBALOPTS} ${awkopts} -f ${orgscript} ${datafile} <${redinfile} 2>&1"
${valgrind} ${QSEAWK} -o "${script}.dp" ${awkopts} -f ${script} ${datafile} <${redinfile} 2>&1 ${valgrind} ${QSEAWK} ${GLOBALOPTS} -o "${script}.dp" ${awkopts} -f ${script} ${datafile} <${redinfile} 2>&1
done < "${TMPFILE}" done < "${TMPFILE}"

View File

@ -48,6 +48,8 @@ print_usage()
TMPFILE="${TMPFILE:=./regress.temp}" TMPFILE="${TMPFILE:=./regress.temp}"
OUTFILE="${OUTFILE:=./regress.out}" OUTFILE="${OUTFILE:=./regress.out}"
GLOBALOPTS="-m 500000"
PROGS=" PROGS="
s001.sed/s001.dat//-n s001.sed/s001.dat//-n
s002.sed/s002.dat// s002.sed/s002.dat//
@ -85,8 +87,8 @@ run_scripts()
[ -z "${redinfile}" ] && redinfile="/dev/stdin" [ -z "${redinfile}" ] && redinfile="/dev/stdin"
echo_title "${valgrind} ${QSESED} ${options} -f ${script} ${datafile} <${redinfile} 2>&1" echo_title "${valgrind} ${QSESED} ${GLOBALOPTS} ${options} -f ${script} ${datafile} <${redinfile} 2>&1"
${valgrind} ${QSESED} ${options} -f ${script} ${datafile} <${redinfile} 2>&1 ${valgrind} ${QSESED} ${GLOBALOPTS} ${options} -f ${script} ${datafile} <${redinfile} 2>&1
done < "${TMPFILE}" done < "${TMPFILE}"