ucimap: add helper function for resizing lists and freeing items (both using ucimap...
[project/uci.git] / ucimap.c
index 7478b70..0d16bb4 100644 (file)
--- a/ucimap.c
+++ b/ucimap.c
 #include <limits.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <errno.h>
 #include "ucimap.h"
 #include "uci_internal.h"
 
 struct uci_alloc {
-       enum ucimap_type type;
-       union {
-               void **ptr;
-       } data;
+       void *ptr;
+};
+
+struct uci_alloc_custom {
+       void *section;
+       struct uci_optmap *om;
+       void *ptr;
 };
 
 struct uci_fixup {
@@ -118,25 +122,13 @@ ucimap_init(struct uci_map *map)
 }
 
 static void
-ucimap_free_item(struct uci_alloc *a)
-{
-       switch(a->type & UCIMAP_TYPE) {
-       case UCIMAP_SIMPLE:
-       case UCIMAP_LIST:
-               free(a->data.ptr);
-               break;
-       }
-}
-
-static void
 ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
 {
        struct uci_alloc *a = &sd->allocmap[sd->allocmap_len++];
-       a->type = UCIMAP_SIMPLE;
-       a->data.ptr = ptr;
+       a->ptr = ptr;
 }
 
-static void
+void
 ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
 {
        void *section;
@@ -150,7 +142,15 @@ ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
                sd->sm->free(map, section);
 
        for (i = 0; i < sd->allocmap_len; i++) {
-               ucimap_free_item(&sd->allocmap[i]);
+               free(sd->allocmap[i].ptr);
+       }
+
+       if (sd->alloc_custom) {
+               for (i = 0; i < sd->alloc_custom_len; i++) {
+                       struct uci_alloc_custom *a = &sd->alloc_custom[i];
+                       a->om->free(a->section, a->om, a->ptr);
+               }
+               free(sd->alloc_custom);
        }
 
        free(sd->allocmap);
@@ -214,6 +214,81 @@ ucimap_handle_fixup(struct uci_map *map, struct uci_fixup *f)
        return true;
 }
 
+void
+ucimap_free_item(struct ucimap_section_data *sd, void *item)
+{
+       struct uci_alloc_custom *ac;
+       struct uci_alloc *a;
+       void *ptr = *((void **) item);
+       int i;
+
+       if (!ptr)
+               return;
+
+       *((void **)item) = NULL;
+       for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
+               if (a->ptr != ptr)
+                       continue;
+
+               if (i != sd->allocmap_len - 1)
+                       a->ptr = sd->allocmap[sd->allocmap_len - 1].ptr;
+
+               sd->allocmap_len--;
+               return;
+       }
+
+       for (i = 0, ac = sd->alloc_custom; i < sd->alloc_custom_len; i++, ac++) {
+               if (ac->ptr != ptr)
+                       continue;
+
+               if (i != sd->alloc_custom_len - 1)
+                       memcpy(ac, &sd->alloc_custom[sd->alloc_custom_len - 1],
+                               sizeof(struct uci_alloc_custom));
+
+               ac->om->free(ac->section, ac->om, ac->ptr);
+               sd->alloc_custom_len--;
+               return;
+       }
+}
+
+int
+ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items)
+{
+       struct ucimap_list *new;
+       struct uci_alloc *a;
+       int i, offset = 0;
+       int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
+
+       if (!*list) {
+               new = calloc(1, size);
+
+               ucimap_add_alloc(sd, new);
+               goto set;
+       }
+
+       for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
+               if (a->ptr != *list)
+                       continue;
+
+               goto realloc;
+       }
+       return -ENOENT;
+
+realloc:
+       if (items > (*list)->size)
+               offset = (items - (*list)->size) * sizeof(union ucimap_data);
+
+       a->ptr = realloc(a->ptr, size);
+       if (offset)
+               memset((char *) a->ptr + offset, 0, size - offset);
+       new = a->ptr;
+
+set:
+       new->size = items;
+       *list = new;
+       return 0;
+}
+
 static void
 ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
 {
@@ -237,6 +312,16 @@ ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct
 }
 
 static void
+ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
+{
+       struct uci_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
+
+       a->section = ucimap_section_ptr(sd);
+       a->om = om;
+       a->ptr = ptr;
+}
+
+static void
 ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
 {
        union ucimap_data tdata = *data;
@@ -245,8 +330,15 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s
        char *s;
        int val;
 
-       if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type))
+       if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
+               if (unlikely(data->list->size <= data->list->n_items)) {
+                       /* should not happen */
+                       DPRINTF("ERROR: overflow while filling a list\n");
+                       return;
+               }
+
                data = &data->list->item[data->list->n_items++];
+       }
 
        switch(om->type & UCIMAP_SUBTYPE) {
        case UCIMAP_STRING:
@@ -296,6 +388,10 @@ ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_s
        if (om->parse) {
                if (om->parse(ucimap_section_ptr(sd), om, &tdata, str) < 0)
                        return;
+               if (ucimap_is_custom(om->type) && om->free) {
+                       if (tdata.ptr != data->ptr)
+                               ucimap_add_custom_alloc(sd, om, data->ptr);
+               }
        }
        if (ucimap_is_custom(om->type))
                return;
@@ -411,6 +507,14 @@ ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
 {
        unsigned int type;
 
+       if (unlikely(sm->type_name != om->type_name) &&
+           unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
+               DPRINTF("Option '%s' of section type '%s' refereces unknown "
+                       "section type '%s', should be '%s'.\n",
+                       om->name, sm->type, om->type_name, sm->type_name);
+               return false;
+       }
+
        if (om->detected_type < 0)
                return true;
 
@@ -448,6 +552,15 @@ failed:
        return false;
 }
 
+static void
+ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
+{
+       if (ucimap_is_alloc(om->type))
+               (*n_alloc)++;
+       else if (ucimap_is_custom(om->type) && om->free)
+               (*n_custom)++;
+}
+
 int
 ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
 {
@@ -455,6 +568,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
        char *section_name;
        void *section;
        int n_alloc = 2;
+       int n_alloc_custom = 0;
        int err;
 
        INIT_LIST_HEAD(&sd->list);
@@ -469,6 +583,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
                        union ucimap_data *data;
                        struct uci_element *e;
                        int n_elements = 0;
+                       int n_elements_custom = 0;
                        int size;
 
                        data = ucimap_get_data(sd, om);
@@ -481,7 +596,7 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
 
                                if (o->type == UCI_TYPE_LIST) {
                                        uci_foreach_element(&o->v.list, tmp) {
-                                               n_elements++;
+                                               ucimap_count_alloc(om, &n_elements, &n_elements_custom);
                                        }
                                } else if ((o->type == UCI_TYPE_STRING) &&
                                           ucimap_is_list_auto(om->type)) {
@@ -494,43 +609,55 @@ ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucim
                                                        break;
 
                                                n_elements++;
+                                               ucimap_count_alloc(om, &n_elements, &n_elements_custom);
 
                                                while (*data && !isspace(*data))
                                                        data++;
                                        } while (*data);
 
                                        /* for the duplicated data string */
-                                       if (n_elements > 0)
+                                       if (n_elements)
                                                n_alloc++;
                                }
                                break;
                        }
                        /* add one more for the ucimap_list */
                        n_alloc += n_elements + 1;
+                       n_alloc_custom += n_elements_custom;
                        size = sizeof(struct ucimap_list) +
                                n_elements * sizeof(union ucimap_data);
+
                        data->list = malloc(size);
+                       if (!data->list)
+                               goto error_mem;
+
+                       data->list->size = n_elements;
                        memset(data->list, 0, size);
-               } else if (ucimap_is_alloc(om->type)) {
-                       n_alloc++;
+               } else {
+                       ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
                }
        }
 
-       sd->allocmap = malloc(n_alloc * sizeof(struct uci_alloc));
+       sd->allocmap = calloc(n_alloc, sizeof(struct uci_alloc));
        if (!sd->allocmap)
                goto error_mem;
 
+       if (n_alloc_custom > 0) {
+               sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct uci_alloc_custom));
+               if (!sd->alloc_custom)
+                       goto error_mem;
+       }
+
        section_name = strdup(s->e.name);
        if (!section_name)
                goto error_mem;
 
        sd->section_name = section_name;
 
-       sd->cmap = malloc(BITFIELD_SIZE(sm->n_options));
+       sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
        if (!sd->cmap)
                goto error_mem;
 
-       memset(sd->cmap, 0, BITFIELD_SIZE(sm->n_options));
        ucimap_add_alloc(sd, (void *)section_name);
        ucimap_add_alloc(sd, (void *)sd->cmap);
        ucimap_foreach_option(sm, om) {
@@ -665,6 +792,9 @@ ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_s
 
                        if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
                                continue;
+
+                       if (!str)
+                               str = "";
                }
                if (!str)
                        continue;