From c015b02b43fa0d7743eb555fdf50fc433dc67b98 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Wed, 3 Nov 1999 19:58:47 +0000 Subject: three types of array-creation / array-deletion functions: char* UNISTR2* SID* decided to create a higher-order function set, add_item_to_array() free_item_array(). higher-order support routines needed to add a new type: type* item_dup(const type*) void item_free(type*) of course, strdup() and free() are perfect, pre-existing examples of such functions, used in the implementation of add_chars_to_array() and free_char_array(). sid_dup() and free() work for the add_sids_to_array() and free_sid_array() implementations. use unistr2_dup() and created unistr2_free() because the functionality behind these may change into something horrible, like [horror] dynamic memory allocation of the UNISTR2 character array. argh!!!! jean-francois, this function set implements what we talked about over... a year ago, now :-) (This used to be commit a80ea2eb47d298095eb6e5b0455309daa3a631cb) --- source3/lib/util.c | 67 +++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/util.c b/source3/lib/util.c index 9e01f0f095..0ed4e6fe50 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -3228,7 +3228,8 @@ BOOL become_user_permanently(uid_t uid, gid_t gid) return(True); } -void free_char_array(uint32 num_entries, char **entries) +void free_void_array(uint32 num_entries, void **entries, + void(free_item)(void*)) { uint32 i; if (entries != NULL) @@ -3237,62 +3238,66 @@ void free_char_array(uint32 num_entries, char **entries) { if (entries[i] != NULL) { - free(entries[i]); + free_item(entries[i]); } } free(entries); } } -BOOL add_chars_to_array(uint32 *len, char ***array, const char *name) +BOOL add_item_to_array(uint32 *len, void ***array, const void *item, + void*(item_dup)(const void*)) { - if (len == NULL || array == NULL) + if (len == NULL || array == NULL || item_dup == NULL) { return False; } - (*array) = (char**)Realloc((*array), ((*len)+1) * sizeof((*array)[0])); + (*array) = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0])); if ((*array) != NULL) { - (*array)[(*len)] = strdup(name); + (*array)[(*len)] = item_dup(item); (*len)++; return True; } return True; } -BOOL add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid) +void free_char_array(uint32 num_entries, char **entries) { - if (len == NULL || array == NULL) - { - return False; - } + void(*fn)(void*) = (void(*)(void*))&free; + free_void_array(num_entries, (void**)entries, *fn); +} - (*array) = (char**)Realloc((*array), ((*len)+1) * sizeof((*array)[0])); +BOOL add_chars_to_array(uint32 *len, char ***array, const char *name) +{ + void*(*fn)(const void*) = (void*(*)(const void*))&strdup; + return add_item_to_array(len, (void***)array, (const void*)name, *fn); + +} - if ((*array) != NULL) - { - (*array)[(*len)] = sid_dup(sid); - (*len)++; - return True; - } - return True; +void free_unistr_array(uint32 num_entries, UNISTR2 **entries) +{ + void(*fn)(void*) = (void(*)(void*))&unistr2_free; + free_void_array(num_entries, (void**)entries, *fn); +} + +BOOL add_unistr_to_array(uint32 *len, UNISTR2 ***array, UNISTR2 *name) +{ + void*(*fn)(const void*) = (void*(*)(const void*))&unistr2_dup; + return add_item_to_array(len, (void***)array, (const void*)name, *fn); } void free_sid_array(uint32 num_entries, DOM_SID **entries) { - uint32 i; - if (entries != NULL) - { - for (i = 0; i < num_entries; i++) - { - if (entries[i] != NULL) - { - free(entries[i]); - } - } - free(entries); - } + void(*fn)(void*) = (void(*)(void*))&free; + free_void_array(num_entries, (void**)entries, *fn); +} + +BOOL add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid) +{ + void*(*fn)(const void*) = (void*(*)(const void*))&sid_dup; + return add_item_to_array(len, (void***)array, (const void*)sid, *fn); } -- cgit