afb-apiset: manage deletion of apis 41/14341/1
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 27 Apr 2018 13:55:43 +0000 (15:55 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Wed, 13 Jun 2018 15:14:53 +0000 (17:14 +0200)
Change-Id: If8b4a2b8773e5e7ce3ae62839193c611eefcb811
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api.h
src/afb-apiset.c

index 60e95b1..3f2e7f1 100644 (file)
@@ -29,6 +29,7 @@ struct afb_api_itf
        int (*get_verbosity)(void *closure);
        void (*set_verbosity)(void *closure, int level);
        struct json_object *(*describe)(void *closure);
+       void (*unref)(void *closure);
 };
 
 struct afb_api
index a373c20..8d76bbf 100644 (file)
@@ -264,21 +264,28 @@ error:
  */
 int afb_apiset_del(struct afb_apiset *set, const char *name)
 {
-       int i, c;
+       struct api_desc *i, *e;
+       int c;
 
        /* search the api */
-       for (i = 0 ; i < set->count ; i++) {
-               c = strcasecmp(set->apis[i].name, name);
+       i = set->apis;
+       e = i + set->count;
+       while (i != e) {
+               c = strcasecmp(i->name, name);
                if (c == 0) {
+                       if (i->api.itf->unref)
+                               i->api.itf->unref(i->api.closure);
                        set->count--;
-                       while(i < set->count) {
-                               set->apis[i] = set->apis[i + 1];
+                       e--;
+                       while (i != e) {
+                               i[0] = i[1];
                                i++;
                        }
                        return 0;
                }
                if (c > 0)
                        break;
+               i++;
        }
        errno = ENOENT;
        return -1;