From 03420ef4ad9919d3eff410d4ce731e2a441818df Mon Sep 17 00:00:00 2001 From: David Given Date: Wed, 3 Aug 2022 21:13:12 +0200 Subject: [PATCH 1/3] Fix warnings caused by bad prototypes. --- modules/src/data/smap.c | 28 +++++++++++++++------------- modules/src/data/smap.h | 14 +++++++------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/modules/src/data/smap.c b/modules/src/data/smap.c index 35e594997..abc8d180e 100644 --- a/modules/src/data/smap.c +++ b/modules/src/data/smap.c @@ -26,7 +26,7 @@ static void append(void* mapp, const char* key, void* value) } -void smap_put(struct smap *mapp, const char* key, void* value) +void smap_put(void *mapp, const char* key, void* value) { struct smap* map = mapp; int i; @@ -44,34 +44,36 @@ void smap_put(struct smap *mapp, const char* key, void* value) append(map, key, value); } -void smap_init(struct smap *mapp) +void smap_init(void *mapp) { - mapp->count = 0; - mapp->item = NULL; - mapp->max = 0; + struct smap* map = mapp; + map->count = 0; + map->item = NULL; + map->max = 0; } -void smap_free(struct smap *mapp, int free_key, int free_value) +void smap_free(void *mapp, int free_key, int free_value) { + struct smap* map = mapp; int i; - for (i=0; icount; i++) + for (i=0; icount; i++) { - struct smap_node* node = &mapp->item[i]; + struct smap_node* node = &map->item[i]; if (free_key) { - free(node->left); + free((void*) node->left); } if (free_value) { free(node->right); } } - mapp->count = 0; - free(mapp->item); + map->count = 0; + free(map->item); } -void smap_add(struct smap *mapp, const char* key, void* value) +void smap_add(void *mapp, const char* key, void* value) { struct smap* map = mapp; int i; @@ -86,7 +88,7 @@ void smap_add(struct smap *mapp, const char* key, void* value) append(map, key, value); } -void* smap_get(struct smap *mapp, const char* left) +void* smap_get(void *mapp, const char* left) { struct smap* map = mapp; int i; diff --git a/modules/src/data/smap.h b/modules/src/data/smap.h index 184c2b176..eef2488c1 100644 --- a/modules/src/data/smap.h +++ b/modules/src/data/smap.h @@ -8,7 +8,7 @@ struct smap_node { - char* left; + const char* left; void* right; }; @@ -23,7 +23,7 @@ struct smap #define SMAPOF(RIGHT) \ struct { \ - struct { char* left; RIGHT* right; }* item; \ + struct { const char* left; RIGHT* right; }* item; \ int count; \ int max; \ } @@ -31,28 +31,28 @@ struct smap /** Initializes a string map and returns the initialized * handle in `mapp`. */ -extern void smap_init(struct smap *mapp); +extern void smap_init(void *mapp); /** Adds a new item with name `key` in the string map if * it does not already exist, otherwise replaces the * value `value` associated with the existing `key`. */ -extern void smap_put(struct smap *mapp, const char* key, void* value); +extern void smap_put(void *mapp, const char* key, void* value); /** Adds a new item in a string map only if `key` does * not already exist in the string map. */ -extern void smap_add(struct smap *mapp, const char* key, void* value); +extern void smap_add(void *mapp, const char* key, void* value); /** Returns the value associated with the specified `key`, returns * NULL if `key` is not present in the string map. * */ -extern void* smap_get(struct smap* mapp, const char* key); +extern void* smap_get(void* mapp, const char* key); /** Frees the data structure associated with the string map. * Also frees the memory associated with the key if * `free_key` is non-zero, and free the memory associated * with the value if `free_value` is non-zero. * */ -extern void smap_free(struct smap *mapp, int free_key, int free_value); +extern void smap_free(void *mapp, int free_key, int free_value); #endif From 42b8c481e30241f668e5db3ba64af91e7a94a90c Mon Sep 17 00:00:00 2001 From: dg Date: Sun, 8 Jan 2023 11:27:36 +0000 Subject: [PATCH 2/3] More warning fixes. --- modules/src/data/stringlist.c | 4 ++-- modules/src/data/stringlist.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/src/data/stringlist.c b/modules/src/data/stringlist.c index 39f125697..e947ea58f 100644 --- a/modules/src/data/stringlist.c +++ b/modules/src/data/stringlist.c @@ -39,7 +39,7 @@ void stringlist_free(struct stringlist* list, int freedata) struct stringfragment* next = f->next; if (freedata) { - free(f->data); + free((void*) f->data); f->data = NULL; } free(f); @@ -54,7 +54,7 @@ void stringlist_init(struct stringlist* list) } -char* stringlist_get(struct stringlist *list, int index) +const char* stringlist_get(struct stringlist *list, int index) { struct stringfragment* f = list->first; int i = 0; diff --git a/modules/src/data/stringlist.h b/modules/src/data/stringlist.h index b3c6ae8e6..b104314d8 100644 --- a/modules/src/data/stringlist.h +++ b/modules/src/data/stringlist.h @@ -3,7 +3,7 @@ struct stringfragment { - char* data; + const char* data; struct stringfragment* next; }; @@ -18,7 +18,7 @@ extern void stringlist_addall(struct stringlist* list, struct stringlist* src); extern void stringlist_free(struct stringlist* list, int freedata); extern void stringlist_init(struct stringlist* list); extern int stringlist_count(struct stringlist *list); -extern char* stringlist_get(struct stringlist *list, int index); +extern const char* stringlist_get(struct stringlist *list, int index); #endif From f21d256f3f3fae59dff8c0ae30368c408bbd2af1 Mon Sep 17 00:00:00 2001 From: dg Date: Sun, 8 Jan 2023 11:27:46 +0000 Subject: [PATCH 3/3] Update documentation. --- modules/src/data/pmap.h | 26 ++++++++++++++++++++++++++ modules/src/data/smap.h | 29 ++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/modules/src/data/pmap.h b/modules/src/data/pmap.h index a7980a3a3..dd17f1c24 100644 --- a/modules/src/data/pmap.h +++ b/modules/src/data/pmap.h @@ -1,6 +1,9 @@ #ifndef PMAP_H #define PMAP_H +/* A bidirectional multimap, storing (left, right) tuples. + */ + struct pmap_node { void* left; @@ -23,10 +26,33 @@ struct pmap int max; \ } +/** Adds a new relation `(left -> right)` to the map + * if `left` is not present. Otherwise replaces the + * `right` associated with the first `left`. + * + * Use this in normal map mode. + */ extern void pmap_put(void* map, void* left, void* right); + +/** Adds a new relation to the map if that relation does + * not already exist. + * + * Use this in multimap mode. + */ extern void pmap_add(void* map, void* left, void* right); + +/** Removes a relation from the map. + * + * Use this in multimap mode. + */ extern void pmap_remove(void* map, void* left, void* right); + +/** Given a `left`, find the first matching `right`. + */ extern void* pmap_findleft(void* map, void* left); + +/** Given a `right`, find the first matching `left`. + */ extern void* pmap_findright(void* map, void* right); #endif diff --git a/modules/src/data/smap.h b/modules/src/data/smap.h index eef2488c1..8e82639f7 100644 --- a/modules/src/data/smap.h +++ b/modules/src/data/smap.h @@ -6,6 +6,8 @@ #ifndef SMAP_H #define SMAP_H +/* A string multimap (each key can have multiple values). */ + struct smap_node { const char* left; @@ -28,24 +30,33 @@ struct smap int max; \ } -/** Initializes a string map and returns the initialized - * handle in `mapp`. +/** Initializes a string map. Optional; a zero-initialised map + * structure is a valid empty map. */ extern void smap_init(void *mapp); -/** Adds a new item with name `key` in the string map if - * it does not already exist, otherwise replaces the - * value `value` associated with the existing `key`. + +/** Adds a new item with name `key` in the string map if it does not already + * exist. Otherwise replaces the value `value` associated with the existing + * `key`. + * + * Use this for normal map mode. */ extern void smap_put(void *mapp, const char* key, void* value); -/** Adds a new item in a string map only if `key` does - * not already exist in the string map. + +/** Adds a new item with name `key` and value `value`. If that relation already + * exists, nothing happens. + * + * Use this for multimap mode. */ extern void smap_add(void *mapp, const char* key, void* value); -/** Returns the value associated with the specified `key`, returns - * NULL if `key` is not present in the string map. + +/** Returns the first value associated with the specified `key`. Returns NULL + * if `key` is not present in the string map. * + * Use this in normal map mode. */ extern void* smap_get(void* mapp, const char* key); + /** Frees the data structure associated with the string map. * Also frees the memory associated with the key if * `free_key` is non-zero, and free the memory associated