summaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2010-06-05 12:06:41 +0300
committerTimo Teräs <timo.teras@iki.fi>2010-06-05 12:33:54 +0300
commit7be853e63785276338a4c4d9e5be084f24114bed (patch)
tree3481293203a948897d7a10f9fb74f09a7c7d6286 /src/common.c
parent069c89898478b0273f7e6d0ea803d6151ee74ff7 (diff)
downloadapk-tools-7be853e63785276338a4c4d9e5be084f24114bed.tar.bz2
apk-tools-7be853e63785276338a4c4d9e5be084f24114bed.tar.xz
all: rework how arrays work
Instead of having a null pointer, use a dummy array which just says the array is empty. This helps in multiple places of the code which would otherwise need explicitly need to check first if the array exists. This has been cause of multiple seg.faults in the past as the array check is easily omitted. This also removes (or fixes) all existing checks accordingly.
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..3eee2fb
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,41 @@
+/* common.c - Alpine Package Keeper (APK)
+ *
+ * Copyright (C) 2010 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation. See http://www.gnu.org/ for details.
+ */
+
+#include <malloc.h>
+#include <string.h>
+#include "apk_defines.h"
+
+static int *dummy_array = 0;
+
+void *apk_array_resize(void *array, size_t new_size, size_t elem_size)
+{
+ int old_size, diff;
+ void *tmp;
+
+ if (new_size == 0) {
+ if (array != &dummy_array)
+ free(array);
+ return &dummy_array;
+ }
+
+ old_size = array ? *((int*) array) : 0;
+ diff = new_size - old_size;
+
+ if (array == &dummy_array)
+ array = NULL;
+
+ tmp = realloc(array, sizeof(int) + new_size * elem_size);
+ if (diff > 0)
+ memset(tmp + sizeof(int) + old_size * elem_size, 0,
+ diff * elem_size);
+ *((int*) tmp) = new_size;
+
+ return tmp;
+}