Sat, 19 Jun 2004 00:04:43 +0200
trunk: changeset 1574
Added routines for storing commonly used strings and referring to them
by keys.
Makefile | file | annotate | diff | comparison | revisions | |
rb.h | file | annotate | diff | comparison | revisions | |
stringstore.c | file | annotate | diff | comparison | revisions | |
stringstore.h | file | annotate | diff | comparison | revisions |
--- a/Makefile Sun May 02 17:14:06 2004 +0200 +++ b/Makefile Sat Jun 19 00:04:43 2004 +0200 @@ -11,7 +11,8 @@ CFLAGS += $(C89_SOURCE) $(POSIX_SOURCE) SOURCES=misc.c output.c util.c optparser.c parser.c tokenizer.c \ - map.c obj.c objlist.c errorlog.c symlist.c rb.c + map.c obj.c objlist.c errorlog.c symlist.c rb.c \ + stringstore.c ifdef LIBTU_NO_ERRMSG DEFINES += -DLIBTU_NO_ERRMSG
--- a/rb.h Sun May 02 17:14:06 2004 +0200 +++ b/rb.h Sat Jun 19 00:04:43 2004 +0200 @@ -66,6 +66,7 @@ struct rb_node *lext; } k; union { + int ival; void *val; struct rb_node *rext; } v;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stringstore.c Sat Jun 19 00:04:43 2004 +0200 @@ -0,0 +1,92 @@ +/* + * libtu/stringstore.c + * + * Copyright (c) Tuomo Valkonen 2004. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#include <stdlib.h> + +#include "misc.h" +#include "output.h" +#include "rb.h" +#include "stringstore.h" + + +static Rb_node stringstore=NULL; + + +StringId stringstore_find(const char *str) +{ + Rb_node node; + int found=0; + + if(stringstore==NULL) + return STRINGID_NONE; + + node=rb_find_key_n(stringstore, str, &found); + + if(!found) + return STRINGID_NONE; + + return (StringId)node; +} + + +StringId stringstore_alloc(const char *str) +{ + Rb_node node=(Rb_node)stringstore_find(str); + char *s; + + if(node!=NULL){ + node->v.ival++; + return node; + } + + if(stringstore==NULL){ + stringstore=make_rb(); + if(stringstore==NULL) + return STRINGID_NONE; + } + + s=scopy(str); + + if(s==NULL) + return STRINGID_NONE; + + node=rb_insert(stringstore, s, NULL); + + if(node==NULL) + return STRINGID_NONE; + + node->v.ival=1; + + return (StringId)node; +} + + +void stringstore_free(StringId id) +{ + Rb_node node=(Rb_node)id; + + if(node==NULL){ + warn("Attempt to free un-allocated string from stringstore."); + return; + } + + if(node->v.ival<=0){ + warn("Stringstore reference count corrupted."); + return; + } + + node->v.ival--; + + if(node->v.ival==0){ + char *s=(char*)(node->k.key); + rb_delete_node(node); + free(s); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stringstore.h Sat Jun 19 00:04:43 2004 +0200 @@ -0,0 +1,21 @@ +/* + * libtu/stringstore.h + * + * Copyright (c) Tuomo Valkonen 2004. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#ifndef LIBTU_STRINGSTORE_H +#define LIBTU_STRINGSTORE_H + +typedef void* StringId; + +#define STRINGID_NONE ((StringId)0) + +extern StringId stringstore_find(const char *str); +extern StringId stringstore_alloc(const char *str); +extern void stringstore_free(StringId id); + +#endif /* LIBTU_STRINGSTORE_H */