From cfad5e411a897bd30bf9f6014e58a25f1ecb69da Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 9 Nov 2008 20:06:20 +0000 Subject: Return errors from dictionary constructor/destructor svn path=/trunk/libparserutils/; revision=5673 --- include/parserutils/utils/dict.h | 5 +++-- src/utils/dict.c | 33 +++++++++++++++++++++------------ test/dict.c | 4 ++-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/include/parserutils/utils/dict.h b/include/parserutils/utils/dict.h index 7c24a2f..ac6050c 100644 --- a/include/parserutils/utils/dict.h +++ b/include/parserutils/utils/dict.h @@ -24,8 +24,9 @@ typedef struct parserutils_dict_entry parserutils_dict_entry; struct parserutils_dict; typedef struct parserutils_dict parserutils_dict; -parserutils_dict *parserutils_dict_create(parserutils_alloc alloc, void *pw); -void parserutils_dict_destroy(parserutils_dict *dict); +parserutils_error parserutils_dict_create(parserutils_alloc alloc, void *pw, + parserutils_dict **dict); +parserutils_error parserutils_dict_destroy(parserutils_dict *dict); parserutils_error parserutils_dict_insert(parserutils_dict *dict, const uint8_t *data, size_t len, diff --git a/src/utils/dict.c b/src/utils/dict.c index 8b519ba..303389c 100644 --- a/src/utils/dict.c +++ b/src/utils/dict.c @@ -36,37 +36,46 @@ static void dict_del(void *key, void *value, void *pw); * \param pw Pointer to client-specific private data * \return Pointer to dictionary instance, or NULL on memory exhaustion */ -parserutils_dict *parserutils_dict_create(parserutils_alloc alloc, void *pw) +parserutils_error parserutils_dict_create(parserutils_alloc alloc, void *pw, + parserutils_dict **dict) { - parserutils_dict *dict; + parserutils_dict *d; - if (alloc == NULL) - return NULL; + if (alloc == NULL || dict == NULL) + return PARSERUTILS_BADPARM; - dict = alloc(NULL, sizeof(parserutils_dict), pw); - if (dict == NULL) - return NULL; + d = alloc(NULL, sizeof(parserutils_dict), pw); + if (d == NULL) + return PARSERUTILS_NOMEM; - memset(dict->table, 0, TABLE_SIZE * sizeof(parserutils_rbtree *)); + memset(d->table, 0, TABLE_SIZE * sizeof(parserutils_rbtree *)); - dict->alloc = alloc; - dict->pw = pw; + d->alloc = alloc; + d->pw = pw; - return dict; + *dict = d; + + return PARSERUTILS_OK; } /** * Destroy a dictionary * * \param dict The dictionary instance to destroy + * \return CSS_OK on success, appropriate error otherwise */ -void parserutils_dict_destroy(parserutils_dict *dict) +parserutils_error parserutils_dict_destroy(parserutils_dict *dict) { + if (dict == NULL) + return PARSERUTILS_BADPARM; + for (int i = 0; i < TABLE_SIZE; i++) { parserutils_rbtree_destroy(dict->table[i], dict_del, dict); } dict->alloc(dict, 0, dict->pw); + + return PARSERUTILS_OK; } /** diff --git a/test/dict.c b/test/dict.c index ee8c597..cb25a8e 100644 --- a/test/dict.c +++ b/test/dict.c @@ -29,8 +29,8 @@ int main(int argc, char **argv) } buf[sizeof(buf) - 1] = '\0'; - dict = parserutils_dict_create(myrealloc, NULL); - assert(dict != NULL); + assert(parserutils_dict_create(myrealloc, NULL, &dict) == + PARSERUTILS_OK); for (int i = 0; i < (int) sizeof(buf); i++) { uint8_t *s = buf; -- cgit v1.2.3