From 24480f7fa3bf3f40bd9fb7c865f9e3b329bf3ed8 Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Fri, 27 Feb 2009 13:35:33 -0500 Subject: Refactor creation of domain_map into confdb The NSS provider, the Data Provider backends and the InfoPipe all need access to the domain map provided by the confdb. Instead of reimplimenting it in multiple places, it is now provided in a pair of helper functions from the confdb. confdb_get_domains() returns a domain map by reference. Always returns the most up-to-date set of domains from the confdb. confdb_get_domains_list() returns an array of strings of all the domain names. Always returns the most up-to-date set of domains from the confdb. This patch also modifies the btreemap_get_keys() function to better handle memory and report allocation failures. --- server/util/btreemap.c | 46 ++++++++++++++++++++++++++++++++++++++-------- server/util/btreemap.h | 2 +- 2 files changed, 39 insertions(+), 9 deletions(-) (limited to 'server/util') diff --git a/server/util/btreemap.c b/server/util/btreemap.c index c9289f52..7bda0570 100644 --- a/server/util/btreemap.c +++ b/server/util/btreemap.c @@ -166,21 +166,51 @@ int btreemap_set_value(TALLOC_CTX *mem_ctx, return EOK; } -/* Return an array of keys in sort order - * count should be initialized to zero before calling this function. - */ -void btreemap_get_keys(TALLOC_CTX *mem_ctx, struct btreemap *map, const void ***array, int *count) +static int _btreemap_get_keys(TALLOC_CTX *mem_ctx, struct btreemap *map, const void ***array, int *count, int depth) { - if (map == NULL) return; + int ret; + const void **tmp_array; + + if (map == NULL) { + if (depth == 0) { + /* This is the top-level */ + count = 0; + *array = NULL; + } + return EOK; + } /* Left Node */ - btreemap_get_keys(mem_ctx, map->left, array, count); + ret = _btreemap_get_keys(mem_ctx, map->left, array, count, depth+1); + if (ret != EOK) { + return ret; + } /* Current Node */ (*count)++; - *array = talloc_realloc(mem_ctx, *array, const void *, *count); + tmp_array = talloc_realloc(mem_ctx, *array, const void *, *count); + if (tmp_array == NULL) { + /* Out of memory */ + *count = 0; + talloc_free(*array); + *array = NULL; + return ENOMEM; + } + *array = tmp_array; (*array)[(*count)-1] = map->key; /* Right Node */ - btreemap_get_keys(mem_ctx, map->right, array, count); + ret = _btreemap_get_keys(mem_ctx, map->right, array, count, depth+1); + if (ret != EOK) { + return ret; + } + + return EOK; +} +/* Return an array of keys in sort order + */ +int btreemap_get_keys(TALLOC_CTX *mem_ctx, struct btreemap *map, const void ***array, int *count) +{ + *array = NULL; + return _btreemap_get_keys(mem_ctx, map, array, count, 0); } diff --git a/server/util/btreemap.h b/server/util/btreemap.h index c5c41507..8d7fb159 100644 --- a/server/util/btreemap.h +++ b/server/util/btreemap.h @@ -35,7 +35,7 @@ void *btreemap_get_value(struct btreemap *map, const void *key); int btreemap_set_value(TALLOC_CTX *mem_ctx, struct btreemap **map, const void *key, void *value, btreemap_comparison_fn comparator); -void btreemap_get_keys(TALLOC_CTX *mem_ctx, struct btreemap *map, const void ***array, int *count); +int btreemap_get_keys(TALLOC_CTX *mem_ctx, struct btreemap *map, const void ***array, int *count); #endif /*BTREEMAP_H_*/ -- cgit