Add a new function ng_findhook() for finding a node's hook;

if the node type provides a more efficient implementation than
the normal linear scan, use it.

Reviewed by:	julian
This commit is contained in:
Archie Cobbs 1999-12-03 21:17:30 +00:00
parent b17f083b0f
commit 899e9c4e44
2 changed files with 26 additions and 14 deletions

View File

@ -245,6 +245,7 @@ void ng_cutlinks(node_p node);
int ng_con_nodes(node_p node,
const char *name, node_p node2, const char *name2);
void ng_destroy_hook(hook_p hook);
hook_p ng_findhook(node_p node, const char *name);
node_p ng_findname(node_p node, const char *name);
struct ng_type *ng_findtype(const char *type);
int ng_make_node(const char *type, node_p *nodepp);

View File

@ -662,11 +662,9 @@ ng_add_hook(node_p node, const char *name, hook_p *hookp)
TRAP_ERROR;
return (EINVAL);
}
LIST_FOREACH(hook, &node->hooks, hooks) {
if (strcmp(hook->name, name) == 0) {
TRAP_ERROR;
return (EEXIST);
}
if (ng_findhook(node, name) != NULL) {
TRAP_ERROR;
return (EEXIST);
}
/* Allocate the hook and link it up */
@ -740,6 +738,26 @@ ng_connect(hook_p hook1, hook_p hook2)
return (0);
}
/*
* Find a hook
*
* Node types may supply their own optimized routines for finding
* hooks. If none is supplied, we just do a linear search.
*/
hook_p
ng_findhook(node_p node, const char *name)
{
hook_p hook;
if (node->type->findhook != NULL)
return (*node->type->findhook)(node, name);
LIST_FOREACH(hook, &node->hooks, hooks) {
if (hook->name != NULL && strcmp(hook->name, name) == 0)
return (hook);
}
return (NULL);
}
/*
* Destroy a hook
*
@ -1060,10 +1078,7 @@ ng_path2node(node_p here, const char *address, node_p *destp, char **rtnp)
continue;
/* We have a segment, so look for a hook by that name */
LIST_FOREACH(hook, &node->hooks, hooks) {
if (hook->name && strcmp(hook->name, segment) == 0)
break;
}
hook = ng_findhook(node, segment);
/* Can't get there from here... */
if (hook == NULL
@ -1236,11 +1251,7 @@ ng_generic_msg(node_p here, struct ng_mesg *msg, const char *retaddr,
return (EINVAL);
}
rmh->ourhook[sizeof(rmh->ourhook) - 1] = '\0';
LIST_FOREACH(hook, &here->hooks, hooks) {
if (hook->name && strcmp(hook->name, rmh->ourhook) == 0)
break;
}
if (hook)
if ((hook = ng_findhook(here, rmh->ourhook)) != NULL)
ng_destroy_hook(hook);
break;
}