summaryrefslogtreecommitdiffstats
path: root/src/apk_defines.h
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/apk_defines.h
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/apk_defines.h')
-rw-r--r--src/apk_defines.h36
1 files changed, 18 insertions, 18 deletions
diff --git a/src/apk_defines.h b/src/apk_defines.h
index 7414159..4a07414 100644
--- a/src/apk_defines.h
+++ b/src/apk_defines.h
@@ -1,7 +1,7 @@
/* apk_defines.c - Alpine Package Keeper (APK)
*
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
- * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
+ * Copyright (C) 2008-2010 Timo Teräs <timo.teras@iki.fi>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
@@ -12,7 +12,6 @@
#ifndef APK_DEFINES_H
#define APK_DEFINES_H
-#include <malloc.h>
#include <string.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -92,32 +91,33 @@ typedef void (*apk_progress_cb)(void *cb_ctx, size_t);
#define APK_PROGRESS_SCALE 0x100
+void *apk_array_resize(void *array, size_t new_size, size_t elem_size);
+
#define APK_ARRAY(array_type_name, elem_type_name) \
struct array_type_name { \
int num; \
elem_type_name item[]; \
}; \
- static inline struct array_type_name * \
- array_type_name##_resize(struct array_type_name *a, int size) \
+ static inline void \
+ array_type_name##_init(struct array_type_name **a) \
+ { \
+ *a = apk_array_resize(NULL, 0, 0); \
+ } \
+ static inline void \
+ array_type_name##_free(struct array_type_name **a) \
+ { \
+ *a = apk_array_resize(*a, 0, 0); \
+ } \
+ static inline void \
+ array_type_name##_resize(struct array_type_name **a, size_t size)\
{ \
- struct array_type_name *tmp; \
- int oldnum = a ? a->num : 0; \
- int diff = size - oldnum; \
- tmp = (struct array_type_name *) \
- realloc(a, sizeof(struct array_type_name) + \
- size * sizeof(elem_type_name)); \
- if (diff > 0) \
- memset(&tmp->item[oldnum], 0, \
- diff * sizeof(a->item[0])); \
- tmp->num = size; \
- return tmp; \
+ *a = apk_array_resize(*a, size, sizeof(elem_type_name));\
} \
static inline elem_type_name * \
array_type_name##_add(struct array_type_name **a) \
{ \
- int size = 1; \
- if (*a != NULL) size += (*a)->num; \
- *a = array_type_name##_resize(*a, size); \
+ int size = 1 + (*a)->num; \
+ *a = apk_array_resize(*a, size, sizeof(elem_type_name));\
return &(*a)->item[size-1]; \
}