0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 12:48:43 +00:00

Conf: Fix symbol lookup

The symbol table used just symbol name as a key, and used a trick with
active flag to find symbols in active scopes with one hash table lookup.

The disadvantage is that it can degenerate to O(n) for negative queries
in situations where are many symbols with the same name in different
scopes.

Thanks to Yanko Kaneti for the bugreport.
This commit is contained in:
Ondrej Zajicek 2023-04-27 17:09:00 +02:00
parent ca0f239c72
commit 9b471e72d7
3 changed files with 16 additions and 12 deletions

View File

@ -73,10 +73,10 @@ static uint cf_hash(const byte *c);
#define KW_FN(k) cf_hash(k) #define KW_FN(k) cf_hash(k)
#define KW_ORDER 8 /* Fixed */ #define KW_ORDER 8 /* Fixed */
#define SYM_KEY(n) n->name, n->scope->active #define SYM_KEY(n) n->name, n->scope
#define SYM_NEXT(n) n->next #define SYM_NEXT(n) n->next
#define SYM_EQ(a,s1,b,s2) !strcmp(a,b) && s1 == s2 #define SYM_EQ(a,s1,b,s2) !strcmp(a,b) && s1 == s2
#define SYM_FN(k,s) cf_hash(k) #define SYM_FN(k,s) cf_hash(k) ^ ptr_hash(s)
#define SYM_ORDER 6 /* Initial */ #define SYM_ORDER 6 /* Initial */
#define SYM_REHASH sym_rehash #define SYM_REHASH sym_rehash
@ -602,29 +602,31 @@ cf_new_symbol(const byte *c)
} }
/** /**
* cf_find_symbol - find a symbol by name * cf_find_local_symbol - find a symbol by name
* @cfg: specificed config * @cfg: specificed config
* @scope: specified symbol scope
* @c: symbol name * @c: symbol name
* *
* This functions searches the symbol table in the config @cfg for a symbol of * This functions searches the symbol table in the config @cfg for a symbol of
* given name. First it examines the current scope, then the second recent one * given name. First it examines the scope @scope, then the parent scope
* and so on until it either finds the symbol and returns a pointer to its * and so on until it either finds the symbol and returns a pointer to its
* &symbol structure or reaches the end of the scope chain and returns %NULL to * &symbol structure or reaches the end of the scope chain and returns %NULL to
* signify no match. * signify no match.
*/ */
struct symbol * struct symbol *
cf_find_symbol(const struct config *cfg, const byte *c) cf_find_local_symbol(const struct config *cfg, const struct sym_scope *scope, const byte *c)
{ {
struct symbol *s; struct symbol *s;
if (cfg->sym_hash.data && if (cfg->sym_hash.data)
(s = HASH_FIND(cfg->sym_hash, SYM, c, 1))) for (; scope; scope = scope->next)
if (s = HASH_FIND(cfg->sym_hash, SYM, c, scope))
return s; return s;
/* In CLI command parsing, fallback points to the current config, otherwise it is NULL. */ /* In CLI command parsing, fallback points to the current config, otherwise it is NULL. */
if (cfg->fallback && if (cfg->fallback &&
cfg->fallback->sym_hash.data && cfg->fallback->sym_hash.data &&
(s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, 1))) (s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, cfg->fallback->root_scope)))
return s; return s;
return NULL; return NULL;
@ -642,7 +644,7 @@ cf_find_symbol(const struct config *cfg, const byte *c)
struct symbol * struct symbol *
cf_get_symbol(const byte *c) cf_get_symbol(const byte *c)
{ {
return cf_find_symbol(new_config, c) ?: cf_new_symbol(c); return cf_find_local_symbol(new_config, conf_this_scope, c) ?: cf_new_symbol(c);
} }
/** /**

View File

@ -187,7 +187,9 @@ int cf_lex(void);
void cf_lex_init(int is_cli, struct config *c); void cf_lex_init(int is_cli, struct config *c);
void cf_lex_unwind(void); void cf_lex_unwind(void);
struct symbol *cf_find_symbol(const struct config *cfg, const byte *c); struct symbol *cf_find_local_symbol(const struct config *cfg, const struct sym_scope *scope, const byte *c);
static inline struct symbol *cf_find_symbol(const struct config *cfg, const byte *c)
{ return cf_find_local_symbol(cfg, cfg->root_scope, c); }
struct symbol *cf_get_symbol(const byte *c); struct symbol *cf_get_symbol(const byte *c);
struct symbol *cf_default_name(char *template, int *counter); struct symbol *cf_default_name(char *template, int *counter);

View File

@ -237,7 +237,7 @@ mem_hash(const void *p, uint s)
} }
static inline uint static inline uint
ptr_hash(void *ptr) ptr_hash(const void *ptr)
{ {
uintptr_t p = (uintptr_t) ptr; uintptr_t p = (uintptr_t) ptr;
return p ^ (p << 8) ^ (p >> 16); return p ^ (p << 8) ^ (p >> 16);