aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-08-23 15:27:39 +0200
committerMattias Andrée <maandree@operamail.com>2013-08-23 15:27:39 +0200
commit701c303517c1d11b172c89cf2875cf3b0476b297 (patch)
treeaa18a722a60ad36c7fb60580dce0ec50b8bdadeb /src
parentc error fixes (diff)
downloadargparser-701c303517c1d11b172c89cf2875cf3b0476b297.tar.gz
argparser-701c303517c1d11b172c89cf2875cf3b0476b297.tar.bz2
argparser-701c303517c1d11b172c89cf2875cf3b0476b297.tar.xz
fix mem faults
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r--src/argparser.c73
1 files 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