redefined some memory allocation functions

added the ismap builtin function to awk
This commit is contained in:
hyung-hwan 2019-05-02 11:29:51 +00:00
parent f9e5c3920d
commit 006042f6cb
8 changed files with 139 additions and 131 deletions

View File

@ -31,6 +31,7 @@
#include <qse/macros.h>
#include <qse/cmn/htb.h>
#include <qse/cmn/str.h>
#include <qse/cmn/mem.h>
/** \file
* An embeddable AWK interpreter is defined in this header file.
@ -129,6 +130,17 @@ struct qse_awk_alt_t
*/
typedef struct qse_awk_rtx_t qse_awk_rtx_t;
#define QSE_AWK_RTX_HDR \
int id; \
qse_awk_t* awk
typedef struct qse_awk_rtx_alt_t qse_awk_rtx_alt_t;
struct qse_awk_rtx_alt_t
{
/* ensure that qse_awk_rtx_alt_t matches the beginning part of qse_awk_rtx_t */
QSE_AWK_RTX_HDR;
};
/**
* The qse_awk_loc_t type defines a structure to hold location.
*/
@ -1872,10 +1884,15 @@ QSE_EXPORT void* qse_awk_callocmem (
/**
* The qse_awk_freemem() function frees dynamic memory allocated.
*/
QSE_EXPORT void qse_awk_freemem (
qse_awk_t* awk, /**< awk */
void* ptr /**< memory block to free */
);
#if defined(QSE_HAVE_INLINE)
static QSE_INLINE void qse_awk_freemem (qse_awk_t* awk, void* ptr)
{
QSE_MMGR_FREE (((qse_awk_alt_t*)(awk))->mmgr, ptr);
}
#else
# define qse_awk_freemem(awk, ptr) QSE_MMGR_FREE(((qse_awk_alt_t*)(awk))->mmgr, ptr);
#endif
/**
* The qse_awk_strdup() function is used to duplicate a string using
@ -2986,10 +3003,14 @@ QSE_EXPORT void* qse_awk_rtx_callocmem (
* The qse_awk_rtx_freemem() function frees a memory block pointed to by \a ptr
* using the memory manager of a runtime ocntext \a rtx.
*/
QSE_EXPORT void qse_awk_rtx_freemem (
qse_awk_rtx_t* rtx, /**< runtime context */
void* ptr /**< memory block pointer */
);
#if defined(QSE_HAVE_INLINE)
static QSE_INLINE void qse_awk_rtx_freemem (qse_awk_rtx_t* rtx, void* ptr)
{
qse_awk_freemem (((qse_awk_rtx_alt_t*)rtx)->awk, ptr);
}
#else
# define qse_awk_rtx_freemem(rtx,ptr) qse_awk_freemem(((qse_awk_rtx_alt_t*)rtx)->awk, ptr)
#endif
/**
* The qse_getawknilval() function returns the pointer to the predefined

View File

@ -132,8 +132,8 @@
# define QSE_MAP_FREEER_DEFAULT QSE_RBT_FREEER_DEFAULT
# define QSE_MAP_COMPER_DEFAULT QSE_RBT_COMPER_DEFAULT
# define QSE_MAP_KEEPER_DEFAULT QSE_RBT_KEEPER_DEFAULT
/*# define QSE_MAP_SIZER_DEFAULT
# define QSE_MAP_HASHER_DEFAULT*/
# define QSE_MAP_SIZER_DEFAULT (QSE_NULL)
# define QSE_MAP_HASHER_DEFAULT (QSE_NULL)
# define QSE_MAP_SIZE(map) QSE_RBT_SIZE(map)
# define QSE_MAP_KCOPIER(map) QSE_RBT_KCOPIER(map)
# define QSE_MAP_VCOPIER(map) QSE_RBT_VCOPIER(map)

View File

@ -304,7 +304,8 @@ struct qse_awk_chain_t
struct qse_awk_rtx_t
{
int id;
QSE_AWK_RTX_HDR;
qse_htb_t* named;
void** stack;
@ -415,8 +416,6 @@ struct qse_awk_rtx_t
} depth;
qse_awk_errinf_t errinf;
qse_awk_t* awk;
qse_awk_rtx_ecb_t* ecb;
};

View File

@ -31,6 +31,7 @@ static int fnc_fflush (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
static int fnc_int (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
static int fnc_typename (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
static int fnc_isnil (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
static int fnc_ismap (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
static int fnc_asort (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
#define A_MAX QSE_TYPE_MAX(int)
@ -60,6 +61,7 @@ static qse_awk_fnc_t sysfnctab[] =
/* type info/conversion */
{ {QSE_T("int"), 3}, 0, { {1, 1, QSE_NULL}, fnc_int, 0 }, QSE_NULL},
{ {QSE_T("isnil"), 5}, 0, { {1, 1, QSE_NULL}, fnc_isnil, 0 }, QSE_NULL},
{ {QSE_T("ismap"), 5}, 0, { {1, 1, QSE_NULL}, fnc_ismap, 0 }, QSE_NULL},
{ {QSE_T("typename"), 8}, 0, { {1, 1, QSE_NULL}, fnc_typename, 0 }, QSE_NULL},
/* array sort */
@ -1513,26 +1515,49 @@ static int fnc_isnil (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
return 0;
}
static int fnc_ismap (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
qse_awk_val_t* a0;
qse_awk_val_t* r;
a0 = qse_awk_rtx_getarg(rtx, 0);
r = qse_awk_rtx_makeintval(rtx, QSE_AWK_RTX_GETVALTYPE(rtx, a0) == QSE_AWK_VAL_MAP);
if (r == QSE_NULL) return -1;
qse_awk_rtx_setretval (rtx, r);
return 0;
}
static int fnc_asort (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
{
qse_size_t nargs;
qse_awk_val_t* a0, * a2;
qse_awk_val_t* r;
int n;
qse_awk_int_t rv = 0; /* as if no element in the map */
qse_awk_val_map_itr_t itr;
qse_awk_fun_t* fun;
qse_size_t msz, i;
const qse_awk_val_t** va;
nargs = qse_awk_rtx_getnargs(rtx);
a0 = qse_awk_rtx_getarg(rtx, 0);
if (QSE_AWK_RTX_GETVALTYPE(rtx, a0) != QSE_AWK_VAL_MAP)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
if (QSE_AWK_RTX_GETVALTYPE(rtx, a0) == QSE_AWK_VAL_NIL)
{
/* treat it as an empty value */
goto done;
}
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOTMAP, QSE_NULL);
return -1;
}
if (nargs >= 2)
{
qse_awk_fun_t* fun;
a2 = qse_awk_rtx_getarg(rtx, 2);
if (QSE_AWK_RTX_GETVALTYPE(rtx, a2) != QSE_AWK_VAL_FUN)
{
@ -1547,12 +1572,46 @@ static int fnc_asort (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINVAL, QSE_NULL);
return -1;
}
/* TODO: complete this function */
}
r = qse_awk_rtx_makeintval(rtx, QSE_AWK_RTX_GETVALTYPE(rtx, a0) == QSE_AWK_VAL_NIL);
if (!qse_awk_rtx_getfirstmapvalitr(rtx, a0, &itr)) goto done; /* map empty */
msz = qse_htb_getsize(((qse_awk_val_map_t*)a0)->map);
QSE_ASSERT (msz > 0);
va = (const qse_awk_val_t**)qse_awk_rtx_allocmem(rtx, msz * QSE_SIZEOF(qse_awk_val_t*));
if (!va) return -1;
i = 0;
do
{
va[i++] = QSE_AWK_VAL_MAP_ITR_VAL(&itr);
}
while (qse_awk_rtx_getnextmapvalitr(rtx, a0, &itr));
{
#if 0
/* TODO: complete this function */
r = qse_awk_rtx_callfun(rtx, fun, valargs, nargs);
if (qse_awk_rtx_valtoint(rtx, r, &rv) <= -1) return -1;
if (rv > 0)
{
}
else if (rv < 0)
{
}
else
{
}
#endif
}
rv = msz;
qse_awk_rtx_freemem (rtx, va);
done:
r = qse_awk_rtx_makeintval(rtx, rv);
qse_awk_rtx_setretval (rtx, r);
return 0;
}

View File

@ -56,11 +56,6 @@ void* qse_awk_reallocmem (qse_awk_t* awk, void* ptr, qse_size_t size)
return nptr;
}
void qse_awk_freemem (qse_awk_t* awk, void* ptr)
{
QSE_AWK_FREE (awk, ptr);
}
qse_char_t* qse_awk_strdup (qse_awk_t* awk, const qse_char_t* s)
{
qse_char_t* ptr = QSE_AWK_STRDUP(awk, s);
@ -928,8 +923,3 @@ void* qse_awk_rtx_callocmem (qse_awk_rtx_t* rtx, qse_size_t size)
return ptr;
}
void qse_awk_rtx_freemem (qse_awk_rtx_t* rtx, void* ptr)
{
QSE_AWK_FREE (rtx->awk, ptr);
}

View File

@ -1,3 +1,4 @@
/* this file is supposed to be included by val.c */
int awk_rtx_strtonum (qse_awk_rtx_t* rtx, int option, const char_t* ptr, qse_size_t len, qse_awk_int_t* l, qse_awk_flt_t* r)
{

View File

@ -61,7 +61,7 @@ qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_awk_int_t v)
if (QSE_AWK_IN_QUICKINT_RANGE(v)) return QSE_AWK_QUICKINT_TO_VTR(v);
if (rtx->vmgr.ifree == QSE_NULL)
if (!rtx->vmgr.ifree)
{
qse_awk_val_ichunk_t* c;
/*qse_awk_val_int_t* x;*/
@ -72,15 +72,9 @@ qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_awk_int_t v)
* aligned memory access - using the code commented out
* will cause a fault on such a platform */
/* c = QSE_AWK_ALLOC (run->awk,
QSE_SIZEOF(qse_awk_val_chunk_t)+
QSE_SIZEOF(qse_awk_val_int_t)*CHUNKSIZE); */
c = QSE_AWK_ALLOC (rtx->awk, QSE_SIZEOF(qse_awk_val_ichunk_t));
if (c == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
/* c = qse_awk_rtx_allocmem(rtx, QSE_SIZEOF(qse_awk_val_chunk_t) + QSE_SIZEOF(qse_awk_val_int_t)*CHUNKSIZE); */
c = qse_awk_rtx_allocmem(rtx, QSE_SIZEOF(qse_awk_val_ichunk_t));
if (!c) return QSE_NULL;
c->next = rtx->vmgr.ichunk;
/*run->vmgr.ichunk = c;*/
@ -127,15 +121,9 @@ qse_awk_val_t* qse_awk_rtx_makefltval (qse_awk_rtx_t* rtx, qse_awk_flt_t v)
/*qse_awk_val_flt_t* x;*/
qse_size_t i;
/* c = QSE_AWK_ALLOC (run->awk,
QSE_SIZEOF(qse_awk_val_chunk_t)+
QSE_SIZEOF(qse_awk_val_flt_t)*CHUNKSIZE); */
c = QSE_AWK_ALLOC (rtx->awk, QSE_SIZEOF(qse_awk_val_rchunk_t));
if (c == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
/* c = qse_awk_rtx_allocmem (rtx, QSE_SIZEOF(qse_awk_val_chunk_t) + QSE_SIZEOF(qse_awk_val_flt_t) * CHUNKSIZE); */
c = qse_awk_rtx_allocmem(rtx, QSE_SIZEOF(qse_awk_val_rchunk_t));
if (!c) return QSE_NULL;
c->next = rtx->vmgr.rchunk;
/*run->vmgr.rchunk = c;*/
@ -193,12 +181,8 @@ qse_awk_val_t* qse_awk_rtx_makestrvalwithxstr (qse_awk_rtx_t* rtx, const qse_cst
}
#endif
val = (qse_awk_val_str_t*)QSE_AWK_ALLOC(rtx->awk, QSE_SIZEOF(qse_awk_val_str_t) + (rlen + 1) * QSE_SIZEOF(qse_char_t));
if (val == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_str_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(qse_awk_val_str_t) + (rlen + 1) * QSE_SIZEOF(qse_char_t));
if (!val) return QSE_NULL;
#ifdef ENABLE_FEATURE_SCACHE
init:
@ -338,15 +322,8 @@ qse_awk_val_t* qse_awk_rtx_makestrval2 (qse_awk_rtx_t* rtx, const qse_char_t* st
}
#endif
val = (qse_awk_val_str_t*) QSE_AWK_ALLOC (
rtx->awk,
QSE_SIZEOF(qse_awk_val_str_t) +
(rlen+1)*QSE_SIZEOF(qse_char_t));
if (val == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_str_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(qse_awk_val_str_t) + ((rlen + 1) * QSE_SIZEOF(qse_char_t)));
if (!val) return QSE_NULL;
#ifdef ENABLE_FEATURE_SCACHE
init:
@ -402,12 +379,8 @@ qse_awk_val_t* qse_awk_rtx_makembsval (qse_awk_rtx_t* rtx, const qse_mchar_t* pt
qse_awk_val_mbs_t* val = QSE_NULL;
qse_size_t xsz = len * QSE_SIZEOF(*ptr);
val = (qse_awk_val_mbs_t*)qse_awk_callocmem(rtx->awk, QSE_SIZEOF(qse_awk_val_mbs_t) + xsz + QSE_SIZEOF(*ptr));
if (val == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_mbs_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(qse_awk_val_mbs_t) + xsz + QSE_SIZEOF(*ptr));
if (!val) return QSE_NULL;
val->v_type = QSE_AWK_VAL_MBS;
val->ref = 0;
@ -437,12 +410,8 @@ qse_awk_val_t* qse_awk_rtx_makerexval (qse_awk_rtx_t* rtx, const qse_cstr_t* str
* the total size is just large enough for all these.
*/
totsz = QSE_SIZEOF(*val) + (QSE_SIZEOF(*str->ptr) * (str->len + 1));
val = (qse_awk_val_rex_t*)qse_awk_callocmem(rtx->awk, totsz);
if (val == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_rex_t*)qse_awk_rtx_callocmem(rtx, totsz);
if (!val) return QSE_NULL;
val->v_type = QSE_AWK_VAL_REX;
val->ref = 0;
@ -508,38 +477,24 @@ qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* rtx)
/* CHECK */
/*
val = (qse_awk_val_map_t*) QSE_AWK_ALLOC (
run->awk, QSE_SIZEOF(qse_awk_val_map_t) );
if (val == QSE_NULL)
{
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_map_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(qse_awk_val_map_t));
if (!val) return QSE_NULL;
val->type = QSE_AWK_VAL_MAP;
val->ref = 0;
val->stat = 0;
val->nstr = 0;
val->map = qse_htb_open (
run, 256, 70, free_mapval, same_mapval, run->awk->mmgr);
val->map = qse_htb_open(run, 256, 70, free_mapval, same_mapval, run->awk->mmgr);
if (val->map == QSE_NULL)
{
QSE_AWK_FREE (run->awk, val);
qse_awk_rtx_freemem (rtx, val);
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
*/
val = (qse_awk_val_map_t*) QSE_AWK_ALLOC (
rtx->awk,
QSE_SIZEOF(qse_awk_val_map_t) +
QSE_SIZEOF(qse_htb_t) +
QSE_SIZEOF(rtx));
if (val == QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_map_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(qse_awk_val_map_t) + QSE_SIZEOF(qse_htb_t) + QSE_SIZEOF(rtx));
if (!val) return QSE_NULL;
val->v_type = QSE_AWK_VAL_MAP;
val->ref = 0;
@ -547,11 +502,9 @@ qse_awk_val_t* qse_awk_rtx_makemapval (qse_awk_rtx_t* rtx)
val->nstr = 0;
val->map = (qse_htb_t*)(val + 1);
if (qse_htb_init (
val->map, rtx->awk->mmgr,
256, 70, QSE_SIZEOF(qse_char_t), 1) <= -1)
if (qse_htb_init(val->map, rtx->awk->mmgr, 256, 70, QSE_SIZEOF(qse_char_t), 1) <= -1)
{
QSE_AWK_FREE (rtx->awk, val);
qse_awk_rtx_freemem (rtx->awk, val);
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
@ -568,7 +521,7 @@ qse_awk_val_t* qse_awk_rtx_makemapvalwithdata (qse_awk_rtx_t* rtx, qse_awk_val_m
qse_awk_val_map_data_t* p;
map = qse_awk_rtx_makemapval(rtx);
if (map == QSE_NULL) return QSE_NULL;
if (!map) return QSE_NULL;
for (p = data; p->key.ptr; p++)
{
@ -677,7 +630,6 @@ qse_awk_val_map_itr_t* qse_awk_rtx_getfirstmapvalitr (
qse_awk_rtx_t* rtx, qse_awk_val_t* map, qse_awk_val_map_itr_t* itr)
{
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, map) == QSE_AWK_VAL_MAP);
itr->pair = qse_htb_getfirstpair (((qse_awk_val_map_t*)map)->map, &itr->buckno);
return itr->pair? itr: QSE_NULL;
}
@ -686,13 +638,11 @@ qse_awk_val_map_itr_t* qse_awk_rtx_getnextmapvalitr (
qse_awk_rtx_t* rtx, qse_awk_val_t* map, qse_awk_val_map_itr_t* itr)
{
QSE_ASSERT (QSE_AWK_RTX_GETVALTYPE (rtx, map) == QSE_AWK_VAL_MAP);
itr->pair = qse_htb_getnextpair (((qse_awk_val_map_t*)map)->map, itr->pair, &itr->buckno);
return itr->pair? itr: QSE_NULL;
}
qse_awk_val_t* qse_awk_rtx_makerefval (
qse_awk_rtx_t* rtx, int id, qse_awk_val_t** adr)
qse_awk_val_t* qse_awk_rtx_makerefval (qse_awk_rtx_t* rtx, int id, qse_awk_val_t** adr)
{
qse_awk_val_ref_t* val;
@ -702,12 +652,8 @@ qse_awk_val_t* qse_awk_rtx_makerefval (
}
else
{
val = (qse_awk_val_ref_t*)qse_awk_callocmem(rtx->awk, QSE_SIZEOF(*val));
if (!val)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_ref_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(*val));
if (!val) return QSE_NULL;
}
val->v_type = QSE_AWK_VAL_REF;
@ -723,12 +669,8 @@ qse_awk_val_t* qse_awk_rtx_makefunval (qse_awk_rtx_t* rtx, const qse_awk_fun_t*
{
qse_awk_val_fun_t* val;
val = (qse_awk_val_fun_t*)qse_awk_callocmem(rtx->awk, QSE_SIZEOF(*val));
if (!val)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
val = (qse_awk_val_fun_t*)qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(*val));
if (!val) return QSE_NULL;
val->v_type = QSE_AWK_VAL_FUN;
val->ref = 0;
@ -1224,12 +1166,8 @@ static int val_int_to_str (qse_awk_rtx_t* rtx, const qse_awk_val_int_t* v, qse_a
break;
case QSE_AWK_RTX_VALTOSTR_CPLDUP:
tmp = QSE_AWK_ALLOC(rtx->awk, (rlen + 1) * QSE_SIZEOF(qse_char_t));
if (!tmp)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
tmp = qse_awk_rtx_allocmem(rtx, (rlen + 1) * QSE_SIZEOF(qse_char_t));
if (!tmp) return -1;
tmp[rlen] = QSE_T('\0');
out->u.cpldup.ptr = tmp;