forked from github/server
putting the crit-bit tree to use. findingfunctions is faster this way than that hand-rolled list
This commit is contained in:
parent
53b9de957c
commit
2638f42eca
1 changed files with 16 additions and 19 deletions
|
@ -19,35 +19,32 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include <platform.h>
|
#include <platform.h>
|
||||||
#include "functions.h"
|
#include "functions.h"
|
||||||
|
|
||||||
|
#include <util/critbit.h>
|
||||||
|
|
||||||
/* libc includes */
|
/* libc includes */
|
||||||
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct function_list {
|
static critbit_tree cb_functions;
|
||||||
struct function_list *next;
|
|
||||||
pf_generic fun;
|
|
||||||
const char *name;
|
|
||||||
} function_list;
|
|
||||||
|
|
||||||
static function_list *functionlist;
|
|
||||||
|
|
||||||
pf_generic get_function(const char *name)
|
pf_generic get_function(const char *name)
|
||||||
{
|
{
|
||||||
function_list *fl = functionlist;
|
const void * matches;
|
||||||
if (name == NULL)
|
pf_generic result;
|
||||||
return NULL;
|
if (cb_find_prefix(&cb_functions, name, strlen(name)+1, &matches, 1, 0)) {
|
||||||
while (fl && strcmp(fl->name, name) != 0)
|
cb_get_kv(matches, &result, sizeof(result));
|
||||||
fl = fl->next;
|
return result;
|
||||||
if (fl)
|
}
|
||||||
return fl->fun;
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void register_function(pf_generic fun, const char *name)
|
void register_function(pf_generic fun, const char *name)
|
||||||
{
|
{
|
||||||
function_list *fl = (function_list *) malloc(sizeof(function_list));
|
char buffer[64];
|
||||||
fl->next = functionlist;
|
size_t len = strlen(name);
|
||||||
fl->fun = fun;
|
|
||||||
fl->name = strdup(name);
|
assert(len<sizeof(buffer)-sizeof(fun));
|
||||||
functionlist = fl;
|
cb_new_kv(name, &fun, sizeof(fun), buffer);
|
||||||
|
cb_insert(&cb_functions, buffer, len+1+sizeof(fun));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue