From 701c303517c1d11b172c89cf2875cf3b0476b297 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 23 Aug 2013 15:27:39 +0200 Subject: fix mem faults MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/argparser.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/src/argparser.c b/src/argparser.c index f09d6c8..4c980a9 100644 --- a/src/argparser.c +++ b/src/argparser.c @@ -32,6 +32,8 @@ /* Prototype for static functions */ static void _sort(char** list, long count, char** temp); static void sort(char** list, long count); +static void _sort_ptr(void** list, long count, void** temp); +static void sort_ptr(void** list, long count); static long cmp(char* a, char* b); static void map_init(args_Map* map); static void* map_get(args_Map* map, char* key); @@ -194,14 +196,27 @@ void args_dispose() { void** freethis = map_free(&args_opts); - long i = 0; - while (*(freethis + i)) + long i = 0, count = 0, last = 0, new, size = 128; + void** values = (void**)malloc(size * sizeof(void*)); + for (; *(freethis + i); i++) { - args_Array* value = *(freethis + i++); - if (value->values != null) - free(value->values); + args_Array* value = *(freethis + i); + if (count == size) + values = (void**)realloc(values, (size <<= 1) * sizeof(void*)); + *(values + count++) = value->values; free(value); } + sort_ptr(values, count); + for (i = 0; i < count; i++) + { + new = (long)(void*)*(values + i); + if (new != last) + { + last = new; + free(*(values + i)); + } + } + free(values); free(freethis); } } @@ -1515,6 +1530,54 @@ static void sort(char** list, long count) free(temp); } +/** + * Naïve merge sort is best merge sort in C + * + * @param list The list to sort from the point that needs sorting + * @param count The number of elements to sort + * @param temp Auxiliary memory + */ +static void _sort_ptr(void** list, long count, void** temp) +{ + if (count > 1) + { + long i = 0, a = count >> 1; + long j = a, b = count - a; + _sort_ptr(list + 0, a, temp + 0); + _sort_ptr(list + a, b, temp + a); + b += a; + while ((i < a) && (j < b)) + { + if (*(temp + i) <= *(temp + j)) + *list++ = *(temp + i++); + else + *list++ = *(temp + j++); + } + while (i < a) + *list++ = *(temp + i++); + while (j < b) + *list++ = *(temp + j++); + list -= count; + for (i = 0; i < count; i++) + *(temp + i) = *(list + i); + } + else if (count == 1) + *temp = *list; +} + +/** + * Naïve merge sort is best merge sort in C + * + * @param list The list to sort + * @param count The number of elements to sort + */ +static void sort_ptr(void** list, long count) +{ + void** temp = (void**)malloc(count * sizeof(void*)); + _sort_ptr(list, count, temp); + free(temp); +} + /** * Initialises a map -- cgit v1.2.3-70-g09d2