diff options
author | Michael Jennings <mej@kainx.org> | 2002-03-03 06:47:48 +0000 |
---|---|---|
committer | Michael Jennings <mej@kainx.org> | 2002-03-03 06:47:48 +0000 |
commit | 3d44098858755b40ae675589d2eba76cd96308eb (patch) | |
tree | 8eeb91af52914eaf0297e01020824171bb3c1197 | |
parent | f617675d777a3ef665dac8a27e0a461b6f2c6e09 (diff) | |
download | libast-3d44098858755b40ae675589d2eba76cd96308eb.tar.gz |
Sun Mar 3 01:44:51 2002 Michael Jennings (mej)
Added precision to the profiling stuff, and threw in the beginnings of
support for interface classes.
SVN revision: 6015
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | acconfig.h | 1 | ||||
-rw-r--r-- | include/libast.h | 4 | ||||
-rw-r--r-- | include/libast/.cvsignore | 28 | ||||
-rw-r--r-- | include/libast/Makefile.am | 2 | ||||
-rw-r--r-- | include/libast/if.h | 80 | ||||
-rw-r--r-- | include/libast_internal.h | 10 | ||||
-rw-r--r-- | src/.indent.pro | 1 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/if.c | 31 | ||||
-rw-r--r-- | src/tok.c | 214 | ||||
-rw-r--r-- | test/perf.c | 139 | ||||
-rw-r--r-- | test/perf.h | 25 |
14 files changed, 372 insertions, 173 deletions
@@ -138,3 +138,8 @@ Mon Feb 18 21:54:25 2002 Michael Jennings (mej) Mmmmm...reusability. :-) ---------------------------------------------------------------------- +Sun Mar 3 01:44:51 2002 Michael Jennings (mej) + +Added precision to the profiling stuff, and threw in the beginnings of +support for interface classes. +---------------------------------------------------------------------- diff --git a/Makefile.am b/Makefile.am index 0298d21..272f78c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,4 +10,7 @@ EXTRA_DIST = README test: all cd test && $(MAKE) test +perf: all + cd test && $(MAKE) perf + .PHONY: test @@ -4,7 +4,6 @@ #undef DEBUG #undef CONFIG_SEARCH_PATH #undef AUTHORS -#undef CONFIG_BUFF #undef LIBAST_X11_SUPPORT #undef LIBAST_IMLIB2_SUPPORT #undef LIBAST_MMX_SUPPORT diff --git a/include/libast.h b/include/libast.h index fb91517..b1570ff 100644 --- a/include/libast.h +++ b/include/libast.h @@ -35,9 +35,6 @@ # ifndef _BSD_SOURCE # define _BSD_SOURCE # endif -# ifndef inline -# define inline __inline__ -# endif #endif #include <stdio.h> @@ -72,6 +69,7 @@ #include <libast/types.h> #include <libast/obj.h> +#include <libast/if.h> #include <libast/str.h> #include <libast/tok.h> diff --git a/include/libast/.cvsignore b/include/libast/.cvsignore index 34ce7e3..69836b0 100644 --- a/include/libast/.cvsignore +++ b/include/libast/.cvsignore @@ -1,29 +1,3 @@ -*.log -config.sub -config.cache -config.guess -configure -ltconfig -ltmain.sh Makefile Makefile.in -aclocal.m4 -config.status -config.h -libtool -stamp-h* -libast.spec -config.h.in -missing -mkinstalldirs -install-sh -libast*.tar.gz -*.sddf -Makefile -Makefile.in -.deps -.libs -lib*.la -*.da -*.lo -libast-config +types.h diff --git a/include/libast/Makefile.am b/include/libast/Makefile.am index 139cb78..20d5ce2 100644 --- a/include/libast/Makefile.am +++ b/include/libast/Makefile.am @@ -1,6 +1,6 @@ # $Id: Makefile.am,v 1.6 2001/09/22 16:25:29 mej Exp $ -EXTRA_HEADERS = obj.h str.h tok.h types.h +EXTRA_HEADERS = if.h obj.h str.h tok.h types.h install-exec-hook: $(mkinstalldirs) $(DESTDIR)$(includedir)/$(PACKAGE) diff --git a/include/libast/if.h b/include/libast/if.h new file mode 100644 index 0000000..e4d280d --- /dev/null +++ b/include/libast/if.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 1997-2002, Michael Jennings + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies of the Software, its documentation and marketing & publicity + * materials, and acknowledgment shall be given in the documentation, materials + * and software packages that this Software was used. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef _LIBAST_IF_H_ +#define _LIBAST_IF_H_ + +/* + * interface goop + */ + +/* Standard typecast macros.... */ +#define SPIF_IFINST(obj) ((spif_ifinst_t) (obj)) +#define SPIF_IFCLASS(cls) ((spif_ifclass_t) (cls)) + +/* Access the implementation class member of an object. */ +#define SPIF_IFINST_IFCLASS(obj) (SPIF_IFINST(obj)->ifclass) + +/* Call a method on an instance of an implementation class */ +#define SPIF_IF_CALL_METHOD(obj, meth) (SPIF_IFINST_IFCLASS(obj)->(meth)) + +/* Calls to the basic functions. */ +#define SPIF_IFCLASS_NEW(cls) (SPIF_IFCLASS(cls)->new) +#define SPIF_IFINST_NEW(obj) (SPIF_IF_CALL_METHOD((obj), (new))) +#define SPIF_IFCLASS_INIT(cls) (SPIF_IFCLASS(cls)->init) +#define SPIF_IFINST_INIT(obj) (SPIF_IF_CALL_METHOD((obj), (init))) +#define SPIF_IFCLASS_DONE(cls) (SPIF_IFCLASS(cls)->done) +#define SPIF_IFINST_DONE(obj) (SPIF_IF_CALL_METHOD((obj), (done))) +#define SPIF_IFCLASS_DEL(cls) (SPIF_IFCLASS(cls)->del) +#define SPIF_IFINST_DEL(obj) (SPIF_IF_CALL_METHOD((obj), (del))) +#define SPIF_IFCLASS_SHOW(cls) (SPIF_IFCLASS(cls)->show) +#define SPIF_IFINST_SHOW(obj) (SPIF_IF_CALL_METHOD((obj), (show))) + +/* The ifclass object contains the function pointers for the interface methods. */ +typedef struct spif_ifclass_t_struct *spif_ifclass_t; +typedef struct spif_ifclass_t_struct spif_const_ifclass_t; + +/* Generic function pointers. */ +typedef spif_obj_t (*spif_newfunc_t)(void); +typedef spif_bool_t (*spif_memberfunc_t)(spif_obj_t); +typedef void * (*spif_func_t)(); + +struct spif_ifclass_t_struct { + spif_newfunc_t new; + spif_memberfunc_t init; + spif_memberfunc_t done; + spif_memberfunc_t del; + spif_func_t show; +}; + +/* ifinst is the parent class for instances of interface classes. */ +typedef struct spif_ifinst_t_struct *spif_ifinst_t; +typedef struct spif_ifinst_t_struct spif_const_ifinst_t; + +struct spif_ifinst_t_struct { + spif_obj_t parent; + spif_ifclass_t ifclass; +}; + + +#endif /* _LIBAST_IF_H_ */ diff --git a/include/libast_internal.h b/include/libast_internal.h index c4d944c..debf881 100644 --- a/include/libast_internal.h +++ b/include/libast_internal.h @@ -35,9 +35,6 @@ # ifndef _BSD_SOURCE # define _BSD_SOURCE # endif -# ifndef inline -# define inline __inline__ -# endif #endif #include "config.h" @@ -59,8 +56,11 @@ /* Macros for specifying the classname variables for each class type. Each subclass of spif_obj_t must define this variable using these macros. */ -#define SPIF_DECL_CNVAR(type) "!spif_" #type "_t!" -#define SPIF_DECL_CLASSNAME(type) spif_classname_t spif_ ## type ## _classname = SPIF_DECL_CNVAR(type) +#define SPIF_DECL_CNVALUE(type) "!spif_" #type "_t!" +#define SPIF_DECL_CLASSNAME(type) spif_classname_t spif_ ## type ## _classname = SPIF_DECL_CNVALUE(type) + +/* The declaration of an interface class. */ +#define SPIF_DECL_IFCLASS(if, type) spif_const_ifclass_t spif_ifclass_ ## if ## _ ## type /******************************** MSGS GOOP ***********************************/ diff --git a/src/.indent.pro b/src/.indent.pro new file mode 100644 index 0000000..8bd2d42 --- /dev/null +++ b/src/.indent.pro @@ -0,0 +1 @@ +-bap -bbo -br -brs -ce -ci4 -cli2 -cs -di1 -hnl -i4 -l140 -lp -lps -nbc -npcs -nss -nsob -pmt -psl -ts 999 diff --git a/src/Makefile.am b/src/Makefile.am index 742d207..2923103 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,6 +4,6 @@ lib_LTLIBRARIES = libast.la INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/include/$(PACKAGE) -libast_la_SOURCES = conf.c debug.c file.c mem.c msgs.c obj.c str.c strings.c snprintf.c tok.c +libast_la_SOURCES = conf.c debug.c file.c if.c mem.c msgs.c obj.c str.c strings.c snprintf.c tok.c libast_la_LDFLAGS = -version-info 2:0:1 diff --git a/src/if.c b/src/if.c new file mode 100644 index 0000000..13f715e --- /dev/null +++ b/src/if.c @@ -0,0 +1,31 @@ +/* + * Copyright (C) 1997-2002, Michael Jennings + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies of the Software, its documentation and marketing & publicity + * materials, and acknowledgment shall be given in the documentation, materials + * and software packages that this Software was used. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +static const char cvs_ident[] = "$Id$"; + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <libast_internal.h> + @@ -35,119 +35,211 @@ SPIF_DECL_CLASSNAME(tok); spif_tok_t spif_tok_new(void) { - spif_tok_t self; + spif_tok_t self; - self = SPIF_ALLOC(tok); - spif_tok_init(self); - return self; + self = SPIF_ALLOC(tok); + spif_tok_init(self); + return self; } spif_tok_t spif_tok_new_from_ptr(spif_charptr_t old) { - spif_tok_t self; + spif_tok_t self; - self = SPIF_ALLOC(tok); - spif_tok_init_from_ptr(self, old); - return self; + self = SPIF_ALLOC(tok); + spif_tok_init_from_ptr(self, old); + return self; } spif_tok_t -spif_tok_new_from_fp(FILE *fp) +spif_tok_new_from_fp(FILE * fp) { - spif_tok_t self; + spif_tok_t self; - self = SPIF_ALLOC(tok); - spif_tok_init_from_fp(self, fp); - return self; + self = SPIF_ALLOC(tok); + spif_tok_init_from_fp(self, fp); + return self; } spif_tok_t spif_tok_new_from_fd(int fd) { - spif_tok_t self; + spif_tok_t self; - self = SPIF_ALLOC(tok); - spif_tok_init_from_fd(self, fd); - return self; + self = SPIF_ALLOC(tok); + spif_tok_init_from_fd(self, fd); + return self; } spif_bool_t spif_tok_del(spif_tok_t self) { - spif_tok_done(self); - SPIF_DEALLOC(self); - return TRUE; + spif_tok_done(self); + SPIF_DEALLOC(self); + return TRUE; } spif_bool_t spif_tok_init(spif_tok_t self) { - spif_str_init(SPIF_STR(self)); - spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); - self->count = 0; - self->token = ((spif_str_t *) (NULL)); - self->sep = SPIF_NULL_TYPE(str); - return TRUE; + spif_str_init(SPIF_STR(self)); + spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); + self->count = 0; + self->token = ((spif_str_t *) (NULL)); + self->sep = SPIF_NULL_TYPE(str); + return TRUE; } spif_bool_t spif_tok_init_from_ptr(spif_tok_t self, spif_charptr_t old) { - spif_str_init_from_ptr(SPIF_STR(self), old); - spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); - self->count = 0; - self->token = ((spif_str_t *) (NULL)); - self->sep = SPIF_NULL_TYPE(str); - return TRUE; + spif_str_init_from_ptr(SPIF_STR(self), old); + spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); + self->count = 0; + self->token = ((spif_str_t *) (NULL)); + self->sep = SPIF_NULL_TYPE(str); + return TRUE; } spif_bool_t -spif_tok_init_from_fp(spif_tok_t self, FILE *fp) +spif_tok_init_from_fp(spif_tok_t self, FILE * fp) { - spif_str_init_from_fp(SPIF_STR(self), fp); - spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); - self->count = 0; - self->token = ((spif_str_t *) (NULL)); - self->sep = SPIF_NULL_TYPE(str); - return TRUE; + spif_str_init_from_fp(SPIF_STR(self), fp); + spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); + self->count = 0; + self->token = ((spif_str_t *) (NULL)); + self->sep = SPIF_NULL_TYPE(str); + return TRUE; } spif_bool_t spif_tok_init_from_fd(spif_tok_t self, int fd) { - spif_str_init_from_fd(SPIF_STR(self), fd); - spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); - self->count = 0; - self->token = ((spif_str_t *) (NULL)); - self->sep = SPIF_NULL_TYPE(str); - return TRUE; + spif_str_init_from_fd(SPIF_STR(self), fd); + spif_obj_set_classname(SPIF_OBJ(self), SPIF_CLASSNAME_TYPE(tok)); + self->count = 0; + self->token = ((spif_str_t *) (NULL)); + self->sep = SPIF_NULL_TYPE(str); + return TRUE; } spif_bool_t spif_tok_done(spif_tok_t self) { - if (self->token) { - size_t i; + if (self->token) { + size_t i; + + for (i = 0; i < self->count; i++) { + spif_str_done(SPIF_STR(self->token[i])); + } + FREE(self->token); + self->token = ((spif_str_t *) (NULL)); + self->count = 0; + } + if (!SPIF_OBJ_ISNULL(self->sep)) { + spif_str_done(SPIF_STR(self->sep)); + self->sep = SPIF_NULL_TYPE(str); + } + spif_str_done(SPIF_STR(self)); + spif_str_init(SPIF_STR(self)); + return TRUE; +} + +#define IS_DELIM(c) ((delim != NULL) ? (strchr(delim, (c)) != NULL) : (isspace(c))) +#define IS_QUOTE(c) (quote && quote == (c)) - for (i = 0; i < self->count; i++) { - spif_str_done(SPIF_STR(self->token[i])); +spif_bool_t +spif_tok_eval(spif_tok_t self) +{ +#if 0 + char **slist; + register const char *pstr; + register char *pdest; + char quote = 0; + unsigned short cnt = 0; + unsigned long len; + + REQUIRE_RVAL(str != NULL, (char **) NULL); + + if ((slist = (char **) MALLOC(sizeof(char *))) == NULL) { + print_error("split(): Unable to allocate memory -- %s\n", strerror(errno)); + return ((char **) NULL); } - FREE(self->token); - self->token = ((spif_str_t *) (NULL)); - self->count = 0; - } - if (!SPIF_OBJ_ISNULL(self->sep)) { - spif_str_done(SPIF_STR(self->sep)); - self->sep = SPIF_NULL_TYPE(str); - } - spif_str_done(SPIF_STR(self)); - spif_str_init(SPIF_STR(self)); - return TRUE; + + /* Before we do anything, skip leading "whitespace." */ + for (pstr = str; *pstr && IS_DELIM(*pstr); pstr++); + + /* The outermost for loop is where we traverse the string. Each new + word brings us back to the top where we resize our string list. */ + for (; *pstr; cnt++) { + /* First, resize the list to two bigger than our count. Why two? + One for the string we're about to do, and one for a trailing NULL. */ + if ((slist = (char **) REALLOC(slist, sizeof(char *) * (cnt + 2))) == NULL) { + print_error("split(): Unable to allocate memory -- %s\n", strerror(errno)); + return ((char **) NULL); + } + + /* The string we're about to create can't possibly be larger than the remainder + of the string we have yet to parse, so allocate that much space to start. */ + len = strlen(pstr) + 1; + if ((slist[cnt] = (char *) MALLOC(len)) == NULL) { + print_error("split(): Unable to allocate memory -- %s.\n", strerror(errno)); + return ((char **) NULL); + } + pdest = slist[cnt]; + + /* This for loop is where we process each character. */ + for (; *pstr && (quote || !IS_DELIM(*pstr));) { + if (*pstr == '\"' || *pstr == '\'') { + /* It's a quote character, so set or reset the quote variable. */ + if (quote) { + if (quote == *pstr) { + quote = 0; + } else { + /* It's a single quote inside double quotes, or vice versa. Leave it alone. */ + *pdest++ = *pstr++; + } + } else { + quote = *pstr; + } + pstr++; + } else { + /* Handle any backslashes that are escaping delimiters or quotes. */ + if ((*pstr == '\\') && (IS_DELIM(*(pstr + 1)) || IS_QUOTE(*(pstr + 1)))) { + /* Incrementing pstr here moves us past the backslash so that the line + below will copy the next character to the new token, no questions asked. */ + pstr++; + } + *pdest++ = *pstr++; + } + } + /* Add the trailing \0 to terminate the new string. */ + *pdest = 0; + + /* Reallocate the new string to be just the right size. */ + len = strlen(slist[cnt]) + 1; + slist[cnt] = (char *) REALLOC(slist[cnt], len); + + /* Move past any trailing "whitespace." */ + for (; *pstr && IS_DELIM(*pstr); pstr++); + } + if (cnt == 0) { + return NULL; + } else { + /* The last element of slist[] should be NULL. */ + slist[cnt] = 0; + return slist; + } +#endif + USE_VAR(self); + return TRUE; } spif_bool_t spif_tok_show(spif_tok_t self, spif_charptr_t name) { - return TRUE; + USE_VAR(self); + USE_VAR(name); + return TRUE; } diff --git a/test/perf.c b/test/perf.c index 3f9ba0e..cea73e0 100644 --- a/test/perf.c +++ b/test/perf.c @@ -30,8 +30,9 @@ static const char cvs_ident[] = "$Id$"; #include <libast.h> #include "perf.h" -unsigned short tnum = 0; -time_t time1, time2; +unsigned long tnum = 0; +struct timeval time1, time2; +double time_diff; long prof_counter; int perf_macros(void); @@ -49,19 +50,19 @@ perf_macros(void) unsigned long sl1 = 0x98765432, sl2 = 0xffeeddff; PERF_BEGIN("MEMSET() macro"); - MEMSET(memset_test, '!', CONST_STRLEN(memset_test)); + PERF_TEST( MEMSET(memset_test, '!', CONST_STRLEN(memset_test)); ); PERF_END(); PERF_BEGIN("SWAP() macro"); - SWAP(sc1, sc2); - SWAP(si1, si2); - SWAP(sl1, sl2); + PERF_TEST( SWAP(sc1, sc2); ); + PERF_TEST( SWAP(si1, si2); ); + PERF_TEST( SWAP(sl1, sl2); ); PERF_END(); PERF_BEGIN("BEG_STRCASECMP() macro"); - BEG_STRCASECMP("this", "this is a test"); - BEG_STRCASECMP("thx", "this is another test"); - BEG_STRCASECMP("this is still another test", "this is"); + PERF_TEST( BEG_STRCASECMP("this", "this is a test"); ); + PERF_TEST( BEG_STRCASECMP("thx", "this is another test"); ); + PERF_TEST( BEG_STRCASECMP("this is still another test", "this is"); ); PERF_END(); PERF_ENDED("macro"); @@ -77,10 +78,10 @@ perf_strings(void) char **slist; PERF_BEGIN("left_str() function"); - s1 = left_str("bugger all", 3); - s2 = left_str("testing 1-2-3", 7); - s3 = left_str(NULL, 0); - s4 = left_str("eat me", 0); + PERF_TEST( s1 = left_str("bugger all", 3); ); + PERF_TEST( s2 = left_str("testing 1-2-3", 7); ); + PERF_TEST( s3 = left_str(NULL, 0); ); + PERF_TEST( s4 = left_str("eat me", 0); ); FREE(s1); FREE(s2); FREE(s3); @@ -88,10 +89,10 @@ perf_strings(void) PERF_END(); PERF_BEGIN("mid_str() function"); - s1 = mid_str("pneumonoultramicroscopicsilicovolcanoconiosis", 8, 16); - s2 = mid_str("abc", 7, 5); - s3 = mid_str(NULL, 0, 0); - s4 = mid_str("what the heck", -5, 42); + PERF_TEST( s1 = mid_str("pneumonoultramicroscopicsilicovolcanoconiosis", 8, 16); ); + PERF_TEST( s2 = mid_str("abc", 7, 5); ); + PERF_TEST( s3 = mid_str(NULL, 0, 0); ); + PERF_TEST( s4 = mid_str("what the heck", -5, 42); ); FREE(s1); FREE(s2); FREE(s3); @@ -99,10 +100,10 @@ perf_strings(void) PERF_END(); PERF_BEGIN("right_str() function"); - s1 = right_str("bugger all", 3); - s2 = right_str("testing 1-2-3", 5); - s3 = right_str(NULL, 0); - s4 = right_str("eat me", 0); + PERF_TEST( s1 = right_str("bugger all", 3); ); + PERF_TEST( s2 = right_str("testing 1-2-3", 5); ); + PERF_TEST( s3 = right_str(NULL, 0); ); + PERF_TEST( s4 = right_str("eat me", 0); ); FREE(s1); FREE(s2); FREE(s3); @@ -111,24 +112,24 @@ perf_strings(void) #ifdef HAVE_REGEX_H PERF_BEGIN("regexp_match() function"); - regexp_match("One particular string", "part"); - regexp_match("Some other strange string", "^[A-Za-z]+$"); - regexp_match("some-rpm-package-1.0.1-4.src.rpm", "^(.*)-([^-]+)-([^-])\\.([a-z0-9]+)\\.rpm$"); - regexp_match("/the/path/to/some/odd/file.txt", "/this/should/not/match"); - regexp_match("1600x1200", "[[:digit:]]+x[[:digit:]]+"); - regexp_match("xxx", NULL); - regexp_match_r("AbCdEfGhIjKlMnOpQrStUvWxYz", "[[:upper:]]", &r); - regexp_match_r("abcdefjhijklmnopqrstuvwxyz", NULL, &r); - regexp_match_r("aaaaa", "[[:lower:]]", &r); + PERF_TEST( regexp_match("One particular string", "part"); ); + PERF_TEST( regexp_match("Some other strange string", "^[A-Za-z]+$"); ); + PERF_TEST( regexp_match("some-rpm-package-1.0.1-4.src.rpm", "^(.*)-([^-]+)-([^-])\\.([a-z0-9]+)\\.rpm$"); ); + PERF_TEST( regexp_match("/the/path/to/some/odd/file.txt", "/this/should/not/match"); ); + PERF_TEST( regexp_match("1600x1200", "[[:digit:]]+x[[:digit:]]+"); ); + PERF_TEST( regexp_match("xxx", NULL); ); + PERF_TEST( regexp_match_r("AbCdEfGhIjKlMnOpQrStUvWxYz", "[[:upper:]]", &r); ); + PERF_TEST( regexp_match_r("abcdefjhijklmnopqrstuvwxyz", NULL, &r); ); + PERF_TEST( regexp_match_r("aaaaa", "[[:lower:]]", &r); ); PERF_END(); #endif PERF_BEGIN("split() function"); - slist = split(" ", "Splitting a string on spaces"); - slist = split(NULL, " a\t \ta a a a a a "); - slist = split(NULL, " first \"just the second\" third \'fourth and \'\"fifth to\"gether last"); - slist = split(NULL, "\'don\\\'t\' try this at home \"\" "); - slist = split(":", "A:B:C:D:::E"); + PERF_TEST( slist = split(" ", "Splitting a string on spaces"); ); + PERF_TEST( slist = split(NULL, " a\t \ta a a a a a "); ); + PERF_TEST( slist = split(NULL, " first \"just the second\" third \'fourth and \'\"fifth to\"gether last"); ); + PERF_TEST( slist = split(NULL, "\'don\\\'t\' try this at home \"\" "); ); + PERF_TEST( slist = split(":", "A:B:C:D:::E"); ); PERF_END(); PERF_ENDED("string"); @@ -142,13 +143,13 @@ perf_obj(void) spif_classname_t cname; PERF_BEGIN("spif_obj_create_delete"); - testobj = spif_obj_new(); - spif_obj_del(testobj); + PERF_TEST( testobj = spif_obj_new(); ); + PERF_TEST( spif_obj_del(testobj); ); PERF_END(); testobj = spif_obj_new(); PERF_BEGIN("spif_obj_get_classname"); - cname = spif_obj_get_classname(testobj); + PERF_TEST( cname = spif_obj_get_classname(testobj); ); PERF_END(); spif_obj_del(testobj); @@ -169,94 +170,94 @@ perf_str(void) spif_charptr_t foo; PERF_BEGIN("spif_str_new_del"); - teststr = spif_str_new(); - spif_str_del(teststr); + PERF_TEST( teststr = spif_str_new(); ); + PERF_TEST( spif_str_del(teststr); ); PERF_END(); teststr = spif_str_new(); PERF_BEGIN("spif_obj_get_classname"); - cname = spif_obj_get_classname(SPIF_OBJ(teststr)); + PERF_TEST( cname = spif_obj_get_classname(SPIF_OBJ(teststr)); ); PERF_END(); spif_str_del(teststr); PERF_BEGIN("spif_str_new_del_from_ptr"); - teststr = spif_str_new_from_ptr(tmp); - spif_str_del(teststr); + PERF_TEST( teststr = spif_str_new_from_ptr(tmp); ); + PERF_TEST( spif_str_del(teststr); ); PERF_END(); PERF_BEGIN("spif_str_new_del_from_buff"); - teststr = spif_str_new_from_buff(buff, sizeof(buff)); - spif_str_del(teststr); + PERF_TEST( teststr = spif_str_new_from_buff(buff, sizeof(buff)); ); + PERF_TEST( spif_str_del(teststr); ); PERF_END(); PERF_BEGIN("spif_str_dup"); - teststr = spif_str_new_from_ptr(tmp); - test2str = spif_str_dup(teststr); - spif_str_del(teststr); - spif_str_del(test2str); + PERF_TEST( teststr = spif_str_new_from_ptr(tmp); ); + PERF_TEST( test2str = spif_str_dup(teststr); ); + PERF_TEST( spif_str_del(teststr); ); + PERF_TEST( spif_str_del(test2str); ); PERF_END(); teststr = spif_str_new_from_ptr(tmp2); PERF_BEGIN("spif_str_index"); - spif_str_index(teststr, '#'); + PERF_TEST( spif_str_index(teststr, '#'); ); PERF_END(); PERF_BEGIN("spif_str_rindex"); - spif_str_rindex(teststr, '#'); + PERF_TEST( spif_str_rindex(teststr, '#'); ); PERF_END(); test2str = spif_str_new_from_ptr("ring"); PERF_BEGIN("spif_str_find"); - spif_str_find(teststr, test2str); + PERF_TEST( spif_str_find(teststr, test2str); ); PERF_END(); spif_str_del(test2str); PERF_BEGIN("spif_str_find_from_ptr"); - spif_str_find_from_ptr(teststr, "in"); + PERF_TEST( spif_str_find_from_ptr(teststr, "in"); ); PERF_END(); spif_str_del(teststr); teststr = spif_str_new_from_ptr(tmp); PERF_BEGIN("spif_str_substr"); - test2str = spif_str_substr(teststr, 2, 5); - spif_str_del(test2str); - test2str = spif_str_substr(teststr, -4, 4); - spif_str_del(test2str); + PERF_TEST( test2str = spif_str_substr(teststr, 2, 5); ); + PERF_TEST( spif_str_del(test2str); ); + PERF_TEST( test2str = spif_str_substr(teststr, -4, 4); ); + PERF_TEST( spif_str_del(test2str); ); PERF_END(); spif_str_del(teststr); teststr = spif_str_new_from_ptr(tmp); PERF_BEGIN("spif_str_substr_to_ptr"); - foo = spif_str_substr_to_ptr(teststr, 2, 5); + PERF_TEST( foo = spif_str_substr_to_ptr(teststr, 2, 5); ); FREE(foo); - foo = spif_str_substr_to_ptr(teststr, -4, 4); + PERF_TEST( foo = spif_str_substr_to_ptr(teststr, -4, 4); ); FREE(foo); PERF_END(); spif_str_del(teststr); teststr = spif_str_new_from_ptr("11001001"); PERF_BEGIN("spif_str_to_num"); - spif_str_to_num(teststr, 10); + PERF_TEST( spif_str_to_num(teststr, 10); ); PERF_END(); spif_str_del(teststr); teststr = spif_str_new_from_ptr("3.1415"); PERF_BEGIN("spif_str_to_float"); - spif_str_to_float(teststr); + PERF_TEST( spif_str_to_float(teststr); ); PERF_END(); spif_str_del(teststr); teststr = spif_str_new_from_ptr(" \n \r\f \t testing 1 2 3 \v\r \n"); PERF_BEGIN("spif_str_trim"); - spif_str_trim(teststr); + PERF_TEST( spif_str_trim(teststr); ); PERF_END(); spif_str_del(teststr); teststr = spif_str_new_from_buff(buff, sizeof(buff)); PERF_BEGIN("spif_str_reverse"); - spif_str_reverse(teststr); + PERF_TEST( spif_str_reverse(teststr); ); PERF_END(); spif_str_del(teststr); @@ -276,8 +277,8 @@ perf_tok(void) spif_charptr_t foo; PERF_BEGIN("spif_tok_new_del_from_ptr"); - testtok = spif_tok_new_from_ptr(tmp); - spif_tok_del(testtok); + PERF_TEST( testtok = spif_tok_new_from_ptr(tmp); ); + PERF_TEST( spif_tok_del(testtok); ); PERF_END(); PERF_ENDED("spif_tok_t"); @@ -288,13 +289,13 @@ int main(int argc, char *argv[]) { int ret = 0; - time_t t1,t2; + struct timeval t1, t2; USE_VAR(argc); USE_VAR(argv); - time(&t1); + gettimeofday(&t1, NULL); if ((ret = perf_macros()) != 0) { return ret; @@ -312,8 +313,8 @@ main(int argc, char *argv[]) return ret; } - time(&t2); + gettimeofday(&t2, NULL); - printf("All profiling done, time elapsed %d.\n\n", t2-t1); + printf("All profiling done, %6.5f seconds total.\n\n", TDIFF(t1, t2)); return 0; } diff --git a/test/perf.h b/test/perf.h index 073c70f..beb1703 100644 --- a/test/perf.h +++ b/test/perf.h @@ -24,11 +24,26 @@ #ifndef _LIBAST_PERF_H_ # define _LIBAST_PERF_H_ -# define PERF_ITERATIONS 100000 +# define TDIFF(t1, t2) (((t2).tv_sec + ((double) (t2).tv_usec / 1000000.0)) \ + - ((t1).tv_sec + ((double) (t1).tv_usec / 1000000.0))) -# define PERF_BEGIN(s) do {tnum = 1; printf("Profiling " s "..."); time(&time1); \ - for (prof_counter=0; prof_counter< PERF_ITERATIONS; prof_counter++) { -# define PERF_END() } time(&time2); printf("time elapsed %d seconds\n", (int)time2-time1);} while (0); -# define PERF_ENDED(s) printf(s " profiling done.\n\n"); return 0; +# define PERF_ITERATIONS 100000 + +# define PERF_BEGIN(s) do { \ + tnum = 0; \ + printf("Profiling " s "..."); \ + fflush(stdout); \ + gettimeofday(&time1, NULL); \ + for (prof_counter = 0; prof_counter < PERF_ITERATIONS; prof_counter++) { + +# define PERF_TEST(x) do { tnum++; x ; } while (0) + +# define PERF_END() } \ + gettimeofday(&time2, NULL); \ + time_diff = TDIFF(time1, time2); \ + printf("%lu iterations in %6.5g seconds, %6.5g seconds per iteration\n", \ + tnum, time_diff, time_diff / tnum); \ + } while (0) +# define PERF_ENDED(s) printf(s " profiling done.\n\n"); return 0; #endif |