X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fsubmesh.c;h=4af2be075401b6684f1ec1aea7bbd16d52b24f60;hb=2224a56ea44bffdafbe29ba7b74d1a0adb0cbd95;hp=7d4a326ed733cd9bf551e4f3bf0ea041c34e8934;hpb=0d133a5ff230ad78da3208d32521d7549836187e;p=meshlink diff --git a/src/submesh.c b/src/submesh.c index 7d4a326e..4af2be07 100644 --- a/src/submesh.c +++ b/src/submesh.c @@ -30,44 +30,39 @@ #include "xalloc.h" #include "protocol.h" -void init_submeshes(meshlink_handle_t *mesh) { - mesh->submeshes = list_alloc((list_action_t)free_submesh); +static submesh_t *new_submesh(void) { + return xzalloc(sizeof(submesh_t)); } -void exit_submeshes(meshlink_handle_t *mesh) { - list_delete_list(mesh->submeshes); - mesh->submeshes = NULL; +static void free_submesh(submesh_t *s) { + free(s->name); + free(s); } -submesh_t *new_submesh(void) { - submesh_t *s = xzalloc(sizeof(*s)); - - s->name = NULL; - s->priv = NULL; - - return s; +void init_submeshes(meshlink_handle_t *mesh) { + assert(!mesh->submeshes); + mesh->submeshes = list_alloc((list_action_t)free_submesh); } -void free_submesh(submesh_t *s) { - if(s->name) { - free(s->name); +void exit_submeshes(meshlink_handle_t *mesh) { + if(mesh->submeshes) { + list_delete_list(mesh->submeshes); } - free(s); + mesh->submeshes = NULL; } -static submesh_t *submesh_new(meshlink_handle_t *mesh, const char *submesh) { - submesh_t *s = NULL; +static submesh_t *submesh_add(meshlink_handle_t *mesh, const char *submesh) { + assert(submesh); - s = new_submesh(); + submesh_t *s = new_submesh(); s->name = xstrdup(submesh); - - submesh_add(mesh, (submesh_t *)s); + list_insert_tail(mesh->submeshes, (void *)s); return s; } submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) { - submesh_t *s = NULL; + assert(submesh); if(0 == strcmp(submesh, CORE_MESH)) { logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n"); @@ -81,22 +76,17 @@ submesh_t *create_submesh(meshlink_handle_t *mesh, const char *submesh) { return NULL; } - s = lookup_submesh(mesh, submesh); - - if(s) { + if(lookup_submesh(mesh, submesh)) { logger(NULL, MESHLINK_ERROR, "SubMesh Already exists!\n"); meshlink_errno = MESHLINK_EEXIST; return NULL; } - s = submesh_new(mesh, submesh); - - meshlink_errno = MESHLINK_OK; - return s; + return submesh_add(mesh, submesh); } submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh) { - submesh_t *s = NULL; + assert(submesh); if(0 == strcmp(submesh, CORE_MESH)) { logger(NULL, MESHLINK_ERROR, "Cannot create submesh handle for core mesh!\n"); @@ -110,29 +100,19 @@ submesh_t *lookup_or_create_submesh(meshlink_handle_t *mesh, const char *submesh return NULL; } - s = lookup_submesh(mesh, submesh); + submesh_t *s = lookup_submesh(mesh, submesh); if(s) { meshlink_errno = MESHLINK_OK; return s; } - s = submesh_new(mesh, submesh); - - meshlink_errno = MESHLINK_OK; - return s; -} - -void submesh_add(meshlink_handle_t *mesh, submesh_t *s) { - s->mesh = mesh; - list_insert_tail(mesh->submeshes, (void *)s); -} - -void submesh_del(meshlink_handle_t *mesh, submesh_t *s) { - list_delete(mesh->submeshes, (void *)s); + return submesh_add(mesh, submesh); } submesh_t *lookup_submesh(struct meshlink_handle *mesh, const char *submesh_name) { + assert(submesh_name); + submesh_t *submesh = NULL; if(!mesh->submeshes) { @@ -155,4 +135,4 @@ bool submesh_allows_node(const submesh_t *submesh, const node_t *node) { } else { return false; } -} \ No newline at end of file +}