From 6f690357726c48d1aa8caee7777c6322d025503f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 8 Aug 2008 05:02:08 +0000 Subject: [PATCH] defined ASE_MEMXXX and started adding copier and freeer --- ase/cmd/awk/awk.c | 69 ++++++++++++++------ ase/include/ase/cmn/sll.h | 104 ++++++++++++++++++++++++------ ase/lib/cmn/makefile.am | 3 +- ase/lib/cmn/makefile.in | 7 ++- ase/lib/cmn/mem.h | 26 ++++++++ ase/lib/cmn/sll.c | 129 ++++++++++++++++++++++++++++++-------- 6 files changed, 271 insertions(+), 67 deletions(-) create mode 100644 ase/lib/cmn/mem.h diff --git a/ase/cmd/awk/awk.c b/ase/cmd/awk/awk.c index 720df83b..1cb354fb 100644 --- a/ase/cmd/awk/awk.c +++ b/ase/cmd/awk/awk.c @@ -1,8 +1,9 @@ /* - * $Id: awk.c 313 2008-08-03 14:06:43Z baconevi $ + * $Id: awk.c 318 2008-08-07 11:02:08Z baconevi $ */ #include +#include #include #include @@ -133,6 +134,7 @@ static ase_ssize_t awk_srcio_in ( struct srcio_t* srcio = (struct srcio_t*)arg; ase_cint_t c; +#if 0 if (cmd == ASE_AWK_IO_OPEN) { if (srcio->input_file == ASE_NULL) return 0; @@ -169,6 +171,7 @@ static ase_ssize_t awk_srcio_in ( { } */ +#endif return -1; } @@ -933,6 +936,11 @@ static int bfn_sleep ( return 0; } +static void out_of_memory (void) +{ + ase_fprintf (ASE_STDERR, ASE_T("Error: out of memory\n")); +} + #if 0 static void handle_args (argc, argv) { @@ -1048,7 +1056,7 @@ static void handle_args (argc, argv) } #endif -static int handle_args (int argc, ase_char_t* argv[], struct srcio_t* srcio) +static int handle_args (int argc, ase_char_t* argv[], ase_sll_t* sf) { ase_cint_t c; static ase_opt_lng_t lng[] = @@ -1098,9 +1106,18 @@ static int handle_args (int argc, ase_char_t* argv[], struct srcio_t* srcio) return 1; case ASE_T('f'): - srcio.type = SIO_FILE; - srcio.data.file.name = opt.arg; + { + ase_size_t sz = ase_strlen(opt.arg) + 1; + sz *= ASE_SIZEOF(*opt.arg); + + if (ase_sll_append(sf, opt.arg, sz) == ASE_NULL) + { + out_of_memory (); + return -1; + } + break; + } case ASE_T('F'): ase_printf (ASE_T("[field separator] = %s\n"), opt.arg); @@ -1134,6 +1151,8 @@ static int handle_args (int argc, ase_char_t* argv[], struct srcio_t* srcio) } } +ase_printf (ASE_T("[%d]\n"), (int)ase_sll_getsize(sf)); +#if 0 if (srcio->input_file == ASE_NULL) { /* the first is the source code... */ @@ -1142,6 +1161,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct srcio_t* srcio) { /* the remainings are data file names */ } +#endif while (opt.ind < argc) @@ -1160,17 +1180,17 @@ static int handle_args (int argc, ase_char_t* argv[], struct srcio_t* srcio) return 0; } -typedef struct extensrcion_t +typedef struct extension_t { ase_mmgr_t mmgr; ase_awk_prmfns_t prmfns; } -extensrcion_t; +extension_t; static void* fuser (void* org, void* space) { - extensrcion_t* ext = (extensrcion_t*)space; - /* remember the memory manager into the extensrcion */ + extension_t* ext = (extension_t*)space; + /* remember the memory manager into the extension */ ext->mmgr = *(ase_mmgr_t*)org; return &ext->mmgr; } @@ -1179,7 +1199,7 @@ static ase_awk_t* open_awk (void) { ase_awk_t* awk; ase_mmgr_t mmgr; - extensrcion_t* extensrcion; + extension_t* extension; memset (&mmgr, 0, ASE_SIZEOF(mmgr)); mmgr.malloc = custom_awk_malloc; @@ -1198,24 +1218,24 @@ static ase_awk_t* open_awk (void) mmgr.custom_data = ASE_NULL; #endif - awk = ase_awk_open (&mmgr, ASE_SIZEOF(extensrcion_t), fuser); + awk = ase_awk_open (&mmgr, ASE_SIZEOF(extension_t), fuser); if (awk == ASE_NULL) { #ifdef _WIN32 HeapDestroy (mmgr_data.heap); #endif ase_printf (ASE_T("ERROR: cannot open awk\n")); - return -1; + return ASE_NULL; } ase_awk_setccls (awk, ASE_GETCCLS()); - extensrcion = (extensrcion_t*) ase_awk_getextensrcion (awk); - extensrcion->prmfns.pow = custom_awk_pow; - extensrcion->prmfns.sprintf = custom_awk_sprintf; - extensrcion->prmfns.dprintf = custom_awk_dprintf; - extensrcion->prmfns.custom_data = ASE_NULL; - ase_awk_setprmfns (awk, &extensrcion->prmfns); + extension = (extension_t*) ase_awk_getextension (awk); + extension->prmfns.pow = custom_awk_pow; + extension->prmfns.sprintf = custom_awk_sprintf; + extension->prmfns.dprintf = custom_awk_dprintf; + extension->prmfns.custom_data = ASE_NULL; + ase_awk_setprmfns (awk, &extension->prmfns); ase_awk_setoption (awk, ASE_AWK_IMPLICIT | ASE_AWK_EXTIO | ASE_AWK_NEWLINE | ASE_AWK_BASEONE | ASE_AWK_PABLOCK); @@ -1226,7 +1246,7 @@ static int awk_main (int argc, ase_char_t* argv[]) { ase_awk_t* awk; ase_mmgr_t mmgr; - extensrcion_t* extensrcion; + extension_t* extension; ase_awk_srcios_t srcios; struct srcio_t srcio = { NULL, NULL }; @@ -1240,8 +1260,17 @@ static int awk_main (int argc, ase_char_t* argv[]) ase_awk_runarg_t runarg[128]; int deparse = 0; + ase_sll_t* sf; - i = handle_args (argc, argv, &srcio); + sf = ase_sll_open (ASE_GETMMGR()); + if (sf == ASE_NULL) + { + out_of_memory (); + return -1; + } +// TODO: destroy sf.... + + i = handle_args (argc, argv, sf); if (i == -1) { print_usage (argv[0]); @@ -1253,11 +1282,13 @@ static int awk_main (int argc, ase_char_t* argv[]) runarg[runarg_count].ptr = NULL; runarg[runarg_count].len = 0; +#if 0 if (mode != 0 || srcio.input_file == NULL) { print_usage (argv[0]); return -1; } +#endif awk = open_awk (); diff --git a/ase/include/ase/cmn/sll.h b/ase/include/ase/cmn/sll.h index adf6b581..a3615f5d 100644 --- a/ase/include/ase/cmn/sll.h +++ b/ase/include/ase/cmn/sll.h @@ -14,32 +14,100 @@ * Singly Linked List */ typedef struct ase_sll_t ase_sll_t; +typedef struct ase_sll_node_t ase_sll_node_t; -struct ase_sll_t -{ - /* map owner. passed to freeval and sameval as the first argument */ - void* owner; +typedef void* (*ase_sll_copier_t) (ase_sll_t* sll, void* data, ase_size_t len); +typedef void (*ase_sll_freeer_t) (ase_sll_t* sll, void* data, ase_size_t len); +typedef int (*ase_sll_walker_t) (ase_sll_t* sll, void* data, ase_size_t len); - ase_sll_node_t* head; - ase_sll_node_t* tail; - - ase_size_t size; - - /* the mmgr pointed at by mmgr should be valid - * while the list is alive as the contents are - * not replicated */ - ase_mmgr_t* mmgr; -}; +#define ASE_SLL_COPIER_INLINE ase_sll_copyinline #ifdef __cplusplus extern "C" { #endif -ase_sll_t* ase_sll_open (ase_mmgr_t* mmgr); -void ase_sll_close (ase_sll_t* sll); -void ase_sll_clear (ase_sll_t* sll); +/* + * NAME creates a new singly linked list + * RETURNS a pointer to a newly created singly linked list + */ +ase_sll_t* ase_sll_open ( + ase_mmgr_t* mmgr /* memory manager */ +); -ase_size_t ase_sll_getsize (ase_sll_t* sll); +/* + * NAME creates a new singly linked list with extension + * RETURNS a pointer to a newly created singly linked list + */ +ase_sll_t* ase_sll_openx ( + ase_mmgr_t* mmgr /* memory manager */ , + ase_size_t extension /* size of extension in bytes */, + ase_fuser_t fuser +); + +/* + * NAME destroys a singly linked list + */ +void ase_sll_close ( + ase_sll_t* sll /* a singly linked list */ +); + +/* + * NAME deletes all elements of a singly linked list + */ +void ase_sll_clear ( + ase_sll_t* sll /* a singly linked list */ +); + +/* + * NAME specifies how to clone an element + * + * DESCRIPTION + * A special copier ASE_SLL_COPIER_INLINE is provided. This copier enables + * you to copy the data inline to the internal node. No freeer is invoked + * when the node is freeed. + * + * You may set the copier to ASE_NULL to perform no special operation + * when the data pointer is rememebered. + */ +void ase_sll_setcopier ( + ase_sll_t* sll /* a singly linked list */, + ase_sll_copier_t copier /* a element copier */ +); + +/* + * NAME specifies how to destroy an element + * + * DESCRIPTION + * The freeer is called when a node containing the element is destroyed. + */ +void ase_sll_setfreeer ( + ase_sll_t* sll /* a singly linked list */, + ase_sll_freeer_t freeer /* a element freeer */ +); + +/* + * Returns the pointer to the extension area + */ +void* ase_sll_getextension ( + ase_sll_t* sll /* a singly linked list */ +); + +/* + * Gets the number of elements held in a singly linked list + * RETURNS the number of elements the list holds + */ +ase_size_t ase_sll_getsize ( + ase_sll_t* sll /* a singly linked list */ +); + +/* Traverses s singly linked list */ +void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker); + +/* + * Causes a singly linked list to copy in data to a node. + * Use ASE_SLL_COPIER_INLINE instead. + */ +void* ase_sll_copyinline (ase_sll_t* sll, void* data, ase_size_t len); #ifdef __cplusplus } diff --git a/ase/lib/cmn/makefile.am b/ase/lib/cmn/makefile.am index 82a02c14..58a316ac 100644 --- a/ase/lib/cmn/makefile.am +++ b/ase/lib/cmn/makefile.am @@ -3,6 +3,7 @@ AUTOMAKE_OPTIONS = nostdinc AM_CFLAGS = -I$(top_builddir)/include lib_LTLIBRARIES = libasecmn.la -libasecmn_la_SOURCES = map.c mem.c misc.c rex.c str_bas.c str_cnv.c str_dyn.c +libasecmn_la_SOURCES = mem.h \ + map.c mem.c misc.c rex.c str_bas.c str_cnv.c str_dyn.c sll.c libasecmn_la_LDFLAGS = -version-info 1:0:0 diff --git a/ase/lib/cmn/makefile.in b/ase/lib/cmn/makefile.in index c71c3bce..a0d7074d 100644 --- a/ase/lib/cmn/makefile.in +++ b/ase/lib/cmn/makefile.in @@ -56,7 +56,7 @@ libLTLIBRARIES_INSTALL = $(INSTALL) LTLIBRARIES = $(lib_LTLIBRARIES) libasecmn_la_LIBADD = am_libasecmn_la_OBJECTS = map.lo mem.lo misc.lo rex.lo str_bas.lo \ - str_cnv.lo str_dyn.lo + str_cnv.lo str_dyn.lo sll.lo libasecmn_la_OBJECTS = $(am_libasecmn_la_OBJECTS) DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/autoconf/depcomp @@ -195,7 +195,9 @@ target_alias = @target_alias@ AUTOMAKE_OPTIONS = nostdinc AM_CFLAGS = -I$(top_builddir)/include lib_LTLIBRARIES = libasecmn.la -libasecmn_la_SOURCES = map.c mem.c misc.c rex.c str_bas.c str_cnv.c str_dyn.c +libasecmn_la_SOURCES = mem.h \ + map.c mem.c misc.c rex.c str_bas.c str_cnv.c str_dyn.c sll.c + libasecmn_la_LDFLAGS = -version-info 1:0:0 all: all-am @@ -270,6 +272,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mem.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/misc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sll.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_bas.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_cnv.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_dyn.Plo@am__quote@ diff --git a/ase/lib/cmn/mem.h b/ase/lib/cmn/mem.h new file mode 100644 index 00000000..810ca0aa --- /dev/null +++ b/ase/lib/cmn/mem.h @@ -0,0 +1,26 @@ +/* + * $Id$ + * + * {License} + */ + +#ifndef _ASE_LIB_CMN_MEM_H_ +#define _ASE_LIB_CMN_MEM_H_ + +#ifdef USE_STDC + +#include +#define ASE_MEMCPY(dst,src,len) memcpy(dst,src,len) +#define ASE_MEMCMP(p1,p2,len) memcmp(p1,p2,len) +#define ASE_MEMSET(dst,val,len) memset(dst,val,len) + +#else + +#include +#define ASE_MEMCPY(dst,src,len) ase_memcpy(dst,src,len) +#define ASE_MEMCMP(p1,p2,len) ase_memcmp(p1,p2,len) +#define ASE_MEMSET(dst,val,len) ase_memset(dst,val,len) + +#endif + +#endif diff --git a/ase/lib/cmn/sll.c b/ase/lib/cmn/sll.c index beb4b485..4cccb408 100644 --- a/ase/lib/cmn/sll.c +++ b/ase/lib/cmn/sll.c @@ -5,25 +5,50 @@ */ #include -#include +#include "mem.h" + +struct ase_sll_t +{ + ase_mmgr_t* mmgr; + + ase_sll_copier_t copier; + ase_sll_freeer_t freeer; + + ase_size_t size; + ase_sll_node_t* head; + ase_sll_node_t* tail; +}; + +struct ase_sll_node_t +{ + ase_sll_node_t* data; + ase_size_t size; + ase_sll_node_t* next; +}; + +void* ase_sll_copyinline (ase_sll_t* sll, void* data, ase_size_t size) +{ + /* this is a dummy copier */ + return ASE_NULL; +} ase_sll_t* ase_sll_open (ase_mmgr_t* mmgr) { return ase_sll_openx (mmgr, 0, ASE_NULL); } -ase_sll_openx (ase_mmgr_t* mmgr, ase_size_t extension, ase_fuser_t fuser) +ase_sll_t* ase_sll_openx (ase_mmgr_t* mmgr, ase_size_t extension, ase_fuser_t fuser) { ase_sll_t* sll; - ssll = ASE_MALLOC (mmgr, ASE_SIZEOF(ase_sll_t) + extension); + sll = ASE_MALLOC (mmgr, ASE_SIZEOF(ase_sll_t) + extension); if (sll == ASE_NULL) return ASE_NULL; - ase_memset (sll, 0, ASE_SIZEOF(ase_sll_t) + extension); - if (mmgr_fuser) mmgr = fuser (mmgr, sll + 1); + ASE_MEMSET (sll, 0, ASE_SIZEOF(ase_sll_t) + extension); + if (fuser != ASE_NULL) mmgr = fuser (mmgr, sll + 1); sll->mmgr = mmgr; - return mmgr; + return sll; } void ase_sll_close (ase_sll_t* sll) @@ -38,51 +63,101 @@ void ase_sll_clear (ase_sll_t* sll) { ase_sll_node_t* n = sll->head->next; - // 1 - no need to free expli9citly.... - - // 2 - sll->freeer (sll->head->data); - ASE_FREE (sll->mmgr, + if (sll->freeer != ASE_NULL) + { + sll->freeer (sll, sll->head->data, sll->head->size); + } ASE_FREE (sll->mmgr, sll->head); sll->head = n; } + + sll->tail = ASE_NULL; } -void ass_sll_setcopier (ase_sll_t* sll) +void* ase_sll_getextension (ase_sll_t* sll) { + return sll + 1; } -void ase_sll_setfreeer (ase_sll_t* sll) +ase_size_t ase_sll_getsize (ase_sll_t* sll) { + return sll->size; } -void ase_sll_append (ase_sll_t* sll, void* data) +void ass_sll_setcopier (ase_sll_t* sll, ase_sll_copier_t copier) { + sll->copier = copier; } -ase_sll_node_t* ase_sll_prepend (ase_sll_t* sll, void* data, size_t len) +void ase_sll_setfreeer (ase_sll_t* sll, ase_sll_freeer_t freeer) +{ + sll->freeer = freeer; +} + +static ase_sll_node_t* alloc_node (ase_sll_t* sll, void* data, ase_size_t size) { ase_sll_node_t* n; -// 1 - n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t) + len); - if (n == ASE_NULL) return ASE_NULL; -// TODO: ASE_MEMCPY to define to be memcpy or ase_memcpy or RtlCopyMemory... - ASE_MEMCPY (n + 1, data, len); - n->data = data; - n->len = len; + if (sll->copier == ASE_NULL) + { + n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t)); + if (n == ASE_NULL) return ASE_NULL; + n->data = data; + } + else if (sll->copier == ASE_SLL_COPIER_INLINE) + { + n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t) + size); + if (n == ASE_NULL) return ASE_NULL; -// 2 - n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t)); + ASE_MEMCPY (n + 1, data, size); + n->data = n + 1; + } + else + { + n = ASE_MALLOC (sll->mmgr, ASE_SIZEOF(ase_sll_node_t)); + if (n == ASE_NULL) return ASE_NULL; + n->data = sll->copier (sll, data, size); + } + + n->size = size; + n->next = ASE_NULL; +} + +ase_sll_node_t* ase_sll_prepend (ase_sll_t* sll, void* data, ase_size_t size) +{ + ase_sll_node_t* n = alloc_node (sll, data, size); if (n == ASE_NULL) return ASE_NULL; - n->data = sll->copier (data, len); - n->len = len; n->next = sll->head; + sll->head = n; + if (sll->tail == ASE_NULL) sll->tail = n; + sll->size++; return n; } +ase_sll_node_t* ase_sll_append (ase_sll_t* sll, void* data, ase_size_t size) +{ + ase_sll_node_t* n = alloc_node (sll, data, size); + if (n == ASE_NULL) return ASE_NULL; + + if (sll->tail != ASE_NULL) sll->tail->next = n; + sll->tail = n; + if (sll->head == ASE_NULL) sll->head = n; + sll->size++; + + return n; +} + +void ase_sll_walk (ase_sll_t* sll, ase_sll_walker_t walker) +{ + ase_sll_node_t* n = sll->head; + + while (n != ASE_NULL) + { + walker (sll, n->data, n->size); + n = n->next; + } +}