diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2017-07-14 10:21:23 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2017-07-14 10:21:23 (GMT) |
commit | 14639bc1b49ded61ce4fe674bf5dbb5a6345d734 (patch) | |
tree | 98ba2f4c045ff08c1ce9c46010fea06eb298a9a4 /src/common | |
parent | d7f78fe9a75d96b6f3d441335dcf50a5c026d8ea (diff) |
Fixed some atomic accesses and a few memory leaks in flat arrays.
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/array.c | 111 |
1 files changed, 66 insertions, 45 deletions
diff --git a/src/common/array.c b/src/common/array.c index 36c30a1..ea05e6a 100644 --- a/src/common/array.c +++ b/src/common/array.c @@ -28,9 +28,6 @@ #include <endian.h> #include <glib.h> #include <malloc.h> -#ifndef NDEBUG -# include <stdbool.h> -#endif #include <string.h> @@ -117,6 +114,11 @@ typedef struct _ext_flat_array_t +/* Verrouille l'accès à une nouvelle adresse d'un tableau. */ +static void relock_flat_array(flat_array_t **, void *); + + + /****************************************************************************** * * * Paramètres : array = tableau compressé à modifier. [OUT] * @@ -131,7 +133,29 @@ typedef struct _ext_flat_array_t void lock_flat_array(flat_array_t **array) { - g_bit_lock((gint *)array, FLAT_ARRAY_LOCK_BIT); + g_pointer_bit_lock(array, FLAT_ARRAY_LOCK_BIT); + +} + + +/****************************************************************************** +* * +* Paramètres : array = tableau compressé à modifier. [OUT] * +* new = nouvelle adresse à traiter atomiquement. * +* * +* Description : Verrouille l'accès à une nouvelle adresse d'un tableau. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void relock_flat_array(flat_array_t **array, void *new) +{ + g_pointer_bit_lock(&new, FLAT_ARRAY_LOCK_BIT); + + g_atomic_pointer_set(array, new); } @@ -150,7 +174,7 @@ void lock_flat_array(flat_array_t **array) void unlock_flat_array(flat_array_t **array) { - g_bit_unlock((gint *)array, FLAT_ARRAY_LOCK_BIT); + g_pointer_bit_unlock(array, FLAT_ARRAY_LOCK_BIT); } @@ -187,9 +211,7 @@ void reset_flat_array(flat_array_t **array) free(GET_LONELY_ITEM(*array)); - *array = NULL; - - lock_flat_array(array); + relock_flat_array(array, NULL); break; @@ -202,9 +224,7 @@ void reset_flat_array(flat_array_t **array) free(extended->items); free(extended); - *array = NULL; - - lock_flat_array(array); + relock_flat_array(array, NULL); break; @@ -231,8 +251,9 @@ void reset_flat_array(flat_array_t **array) void copy_flat_array_items(flat_array_t **src, flat_array_t **dest, size_t size, item_notify_cb notify) { void *item; /* Elément manipulé */ + void *new_single; /* Nouvelle copie crée */ ext_flat_array_t *extended; /* Version de tableau étendue */ - ext_flat_array_t *new; /* Nouvelle copie créée */ + ext_flat_array_t *new_ext; /* Nouvelle copie créée */ size_t i; /* Boucle de parcours */ assert(!FLAT_ARRAY_IS_LOCKED(*src)); @@ -253,10 +274,10 @@ void copy_flat_array_items(flat_array_t **src, flat_array_t **dest, size_t size, if (notify != NULL) notify(item); - *dest = malloc(size); - memcpy(*dest, item, size); + new_single = malloc(size); + memcpy(new_single, item, size); - lock_flat_array(dest); + relock_flat_array(dest, new_single); } @@ -264,25 +285,23 @@ void copy_flat_array_items(flat_array_t **src, flat_array_t **dest, size_t size, { extended = EXTENDED_ARRAY(*src); - new = (ext_flat_array_t *)malloc(sizeof(ext_flat_array_t)); + new_ext = (ext_flat_array_t *)malloc(sizeof(ext_flat_array_t)); - new->items = malloc(extended->count * size); - new->count = extended->count; + new_ext->items = malloc(extended->count * size); + new_ext->count = extended->count; - memcpy(new->items, extended->items, new->count * size); + memcpy(new_ext->items, extended->items, new_ext->count * size); if (notify != NULL) - for (i = 0; i < new->count; i++) + for (i = 0; i < new_ext->count; i++) { - item = (void *)(((char *)new->items) + i * size); + item = (void *)(((char *)new_ext->items) + i * size); notify(item); } - FLAT_ARRAY_SET_INDEX(new); - - *dest = (flat_array_t *)new; + FLAT_ARRAY_SET_INDEX(new_ext); - lock_flat_array(dest); + relock_flat_array(dest, new_ext); } @@ -350,16 +369,17 @@ size_t count_flat_array_items(const flat_array_t *array) void add_item_to_flat_array(flat_array_t **array, const void *item, size_t size) { + void *single; /* Copie à un seul élément */ ext_flat_array_t *extended; /* Version de tableau étendue */ assert(FLAT_ARRAY_IS_LOCKED(*array)); if (FLAT_ARRAY_IS_EMPTY(*array)) { - *array = malloc(size); - memcpy(*array, item, size); + single = malloc(size); + memcpy(single, item, size); - lock_flat_array(array); + relock_flat_array(array, single); } @@ -372,14 +392,16 @@ void add_item_to_flat_array(flat_array_t **array, const void *item, size_t size) extended->items = malloc(2 * size); extended->count = 2; - memcpy(extended->items, GET_LONELY_ITEM(*array), size); + single = GET_LONELY_ITEM(*array); + + memcpy(extended->items, single, size); memcpy(((char *)extended->items) + size, item, size); - FLAT_ARRAY_SET_INDEX(extended); + free(single); - *array = (flat_array_t *)extended; + FLAT_ARRAY_SET_INDEX(extended); - lock_flat_array(array); + relock_flat_array(array, extended); } @@ -416,16 +438,17 @@ void add_item_to_flat_array(flat_array_t **array, const void *item, size_t size) void insert_item_into_flat_array(flat_array_t **array, void *item, size_t size, __compar_fn_t compar) { + void *single; /* Copie à un seul élément */ ext_flat_array_t *extended; /* Version de tableau étendue */ assert(FLAT_ARRAY_IS_LOCKED(*array)); if (FLAT_ARRAY_IS_EMPTY(*array)) { - *array = malloc(size); - memcpy(*array, item, size); + single = malloc(size); + memcpy(single, item, size); - lock_flat_array(array); + relock_flat_array(array, single); } @@ -438,13 +461,15 @@ void insert_item_into_flat_array(flat_array_t **array, void *item, size_t size, extended->items = malloc(size); extended->count = 1; - memcpy(extended->items, GET_LONELY_ITEM(*array), size); + single = GET_LONELY_ITEM(*array); - FLAT_ARRAY_SET_INDEX(extended); + memcpy(extended->items, single, size); - *array = (flat_array_t *)extended; + free(single); - lock_flat_array(array); + FLAT_ARRAY_SET_INDEX(extended); + + relock_flat_array(array, extended); } @@ -538,9 +563,7 @@ void rem_item_from_flat_array(flat_array_t **array, size_t index, size_t size) free(GET_LONELY_ITEM(*array)); - *array = NULL; - - lock_flat_array(array); + relock_flat_array(array, NULL); break; @@ -560,9 +583,7 @@ void rem_item_from_flat_array(flat_array_t **array, size_t index, size_t size) free(extended); - *array = new; - - lock_flat_array(array); + relock_flat_array(array, new); break; |