From f8d794373562c34aac15a85281669c936fc3410a Mon Sep 17 00:00:00 2001 From: bg- Date: Fri, 12 Jan 2007 13:55:04 +0000 Subject: [PATCH] * Switch from linear search to binary ditto. * Symbol names is "const char *" but symbol values are "void *" (not "const char *"). --- core/loader/symbols-def.h | 4 ++-- core/loader/symtab.c | 40 +++++++++++++++++---------------------- core/loader/symtab.h | 4 ++-- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/core/loader/symbols-def.h b/core/loader/symbols-def.h index cf199d83c..50bee6beb 100644 --- a/core/loader/symbols-def.h +++ b/core/loader/symbols-def.h @@ -28,14 +28,14 @@ * * This file is part of the Contiki operating system. * - * @(#)$Id: symbols-def.h,v 1.2 2006/12/20 14:36:28 bg- Exp $ + * @(#)$Id: symbols-def.h,v 1.3 2007/01/12 13:55:04 bg- Exp $ */ #ifndef __SYMBOLS_DEF_H__ #define __SYMBOLS_DEF_H__ struct symbols { const char *name; - const char *value; + void *value; }; extern const int symbols_nelts; diff --git a/core/loader/symtab.c b/core/loader/symtab.c index 8082cd1fc..01a1a6964 100644 --- a/core/loader/symtab.c +++ b/core/loader/symtab.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * @(#)$Id: symtab.c,v 1.5 2006/12/20 14:41:28 bg- Exp $ + * @(#)$Id: symtab.c,v 1.6 2007/01/12 13:55:05 bg- Exp $ */ #include "symtab.h" @@ -37,44 +37,38 @@ #include -#define SYMTAB_CONF_BINARY_SEARCH 0 +/* Binary search is twice as large but still small. */ +#ifndef SYMTAB_CONF_BINARY_SEARCH +#define SYMTAB_CONF_BINARY_SEARCH 1 +#endif /*---------------------------------------------------------------------------*/ #if SYMTAB_CONF_BINARY_SEARCH -const char * +void * symtab_lookup(const char *name) { int start, middle, end; int r; start = 0; - end = symbols_nelts; + end = symbols_nelts - 1; /* Last entry is { 0, 0 }. */ - do { + while(start <= end) { /* Check middle, divide */ - middle = (end + start) / 2; - - if(symbols[middle].name == NULL) { - return NULL; - } + middle = (start + end) / 2; r = strcmp(name, symbols[middle].name); - if(r == 0) { + if(r < 0) { + end = middle - 1; + } else if(r > 0) { + start = middle + 1; + } else { return symbols[middle].value; } - if(end == middle || start == middle) { - return NULL; - } - if(r < 0) { - end = middle; - } - if(r > 0) { - start = middle; - } - } while(1); - + } + return NULL; } #else /* SYMTAB_CONF_BINARY_SEARCH */ -const char * +void * symtab_lookup(const char *name) { const struct symbols *s; diff --git a/core/loader/symtab.h b/core/loader/symtab.h index 19a95f954..f01eb6b85 100644 --- a/core/loader/symtab.h +++ b/core/loader/symtab.h @@ -28,11 +28,11 @@ * * This file is part of the Contiki operating system. * - * @(#)$Id: symtab.h,v 1.3 2006/12/19 09:30:57 adamdunkels Exp $ + * @(#)$Id: symtab.h,v 1.4 2007/01/12 13:55:05 bg- Exp $ */ #ifndef __SYMTAB_H__ #define __SYMTAB_H__ -const char *symtab_lookup(const char *name); +void *symtab_lookup(const char *name); #endif /* __SYMTAB_H__ */