From 7be853e63785276338a4c4d9e5be084f24114bed Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Sat, 5 Jun 2010 12:06:41 +0300 Subject: 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. --- src/apk_defines.h | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/apk_defines.h') 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 - * Copyright (C) 2008 Timo Teräs + * Copyright (C) 2008-2010 Timo Teräs * 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 #include #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]; \ } -- cgit v1.2.3-60-g2f50