From d80dae3035f31ef06d2e37997ee8af82ff59d561 Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Fri, 20 Apr 2012 08:30:59 +0000 Subject: [PATCH] eina: fix Eina_Inarray API to at least match Eina_Array API. NOTE: some of this function should be moved as inline, but that's to late for a change I think. So we will fix that if needed. Second point, I am not happy with is eina_inarray_insert and eina_inarray_insert_at. The naming is really poor. SVN revision: 70352 --- legacy/eina/src/examples/eina_inarray_01.c | 16 +++---- legacy/eina/src/examples/eina_inarray_02.c | 2 +- legacy/eina/src/include/eina_inarray.h | 48 +++++++++++-------- legacy/eina/src/lib/eina_inarray.c | 33 +++++++++---- legacy/eina/src/lib/eina_value.c | 2 +- legacy/eina/src/tests/eina_test_inarray.c | 8 ++-- legacy/eina/src/tests/eina_test_value.c | 6 +-- .../src/modules/engines/buffer/evas_engine.c | 4 +- 8 files changed, 70 insertions(+), 49 deletions(-) diff --git a/legacy/eina/src/examples/eina_inarray_01.c b/legacy/eina/src/examples/eina_inarray_01.c index b9ab4e7a51..2809778993 100644 --- a/legacy/eina/src/examples/eina_inarray_01.c +++ b/legacy/eina/src/examples/eina_inarray_01.c @@ -19,27 +19,27 @@ int main(int argc, char **argv) iarr = eina_inarray_new(sizeof(char), 0); ch = 'a'; - eina_inarray_append(iarr, &ch); + eina_inarray_push(iarr, &ch); ch = 'b'; - eina_inarray_append(iarr, &ch); + eina_inarray_push(iarr, &ch); ch = 'c'; - eina_inarray_append(iarr, &ch); + eina_inarray_push(iarr, &ch); ch = 'd'; - eina_inarray_append(iarr, &ch); + eina_inarray_push(iarr, &ch); printf("Inline array of chars:\n"); EINA_INARRAY_FOREACH(iarr, ch2) printf("char: %c(pointer: %p)\n", *ch2, ch2); eina_inarray_flush(iarr); - eina_inarray_setup(iarr, sizeof(int), 4); + eina_inarray_step_set(iarr, sizeof(Eina_Inarray), sizeof(int), 4); a = 97; - eina_inarray_append(iarr, &a); + eina_inarray_push(iarr, &a); a = 98; - eina_inarray_append(iarr, &a); + eina_inarray_push(iarr, &a); a = 100; - eina_inarray_append(iarr, &a); + eina_inarray_push(iarr, &a); a = 99; eina_inarray_insert_sorted(iarr, &a, cmp); diff --git a/legacy/eina/src/examples/eina_inarray_02.c b/legacy/eina/src/examples/eina_inarray_02.c index 3653b36f9a..17c0667408 100644 --- a/legacy/eina/src/examples/eina_inarray_02.c +++ b/legacy/eina/src/examples/eina_inarray_02.c @@ -21,7 +21,7 @@ main(int argc, char **argv) for (i = 0; i < 20; i++){ str = &strings[i]; - eina_inarray_append(iarr, str); + eina_inarray_push(iarr, str); } printf("Inline array of strings:\n"); diff --git a/legacy/eina/src/include/eina_inarray.h b/legacy/eina/src/include/eina_inarray.h index 7d0902ca1a..cfa1621d76 100644 --- a/legacy/eina/src/include/eina_inarray.h +++ b/legacy/eina/src/include/eina_inarray.h @@ -50,14 +50,14 @@ * insertion function expect a memory address we have to put the value we want * to store in a variable(this should be no problem since in real world usage * that's usually where the value will be anyways): - * @until append + * @until push * @note Because the inline array copies the value given to it we can later * change @c ch, which we do, without affecting the contents of the array. * * So let's add some more elements: - * @until append - * @until append - * @until append + * @until push + * @until push + * @until push * * We will then iterate over our array and print every position of it. The thing * to note here is not so much the values which will be the expected 'a', 'b', @@ -71,26 +71,26 @@ * @until _flush * * And then to be able to store a different type on the same array we use the - * eina_array_setup() function, which is just like the eina_inarray_new() + * eina_inarray_step_set() function, which is just like the eina_inarray_new() * function except it receives already allocated memory. This time we're going * to ask eina to use a step of size 4 because that's how many elements we'll be * putting on the array: - * @until _setup + * @until _step_set * @note Strictly speaking the reason to call eina_inarray_setup() is not * because we're storing different type, but rather because our types have * different sizes. Eina inline arrays don't actually know anything about types, * they only deal in blocks of memory of a given size. - * @note Since eina_array_setup() receives already allocated memory you can(and + * @note Since eina_inarray_step_set() receives already allocated memory you can(and * it is in fact good practice) use inline arrays not declared as pointers: * @code * Eina_Inarray arr; - * eina_inarray_setup(&arr, sizeof(int), 4); + * eina_inarray_step_set(&arr, sizeof(arr), sizeof(int), 4); * @endcode * * And now to add our integer values to the array: - * @until append - * @until append - * @until append + * @until push + * @until push + * @until push * * Just to change things up a bit we've left out the 99 value, but will still * add it in such a way to keep the array ordered. There are many ways to do @@ -193,13 +193,16 @@ typedef struct _Eina_Inarray Eina_Inarray; /** * Inline array structure, use #Eina_Inarray typedef instead. * - * Do not modify these fields directly, use eina_inarray_setup() or + * Do not modify these fields directly, use eina_inarray_step_set() or * eina_inarray_new() instead. * * @since 1.2 */ struct _Eina_Inarray { +#define EINA_ARRAY_VERSION 1 + int version; /**< Should match EINA_ARRAY_VERSION used when compiled your apps, provided for ABI compatibility */ + unsigned int member_size; /**< byte size of each entry in members */ unsigned int len; /**< number of elements used in members */ unsigned int max; /**< number of elements allocated in members */ @@ -258,9 +261,10 @@ EAPI void eina_inarray_free(Eina_Inarray *array) EINA_ARG_NONNULL(1); * * @since 1.2 */ -EAPI void eina_inarray_setup(Eina_Inarray *array, - unsigned int member_size, - unsigned int step) EINA_ARG_NONNULL(1); +EAPI void eina_inarray_step_set(Eina_Inarray *array, + unsigned int sizeof_eina_inarray, + unsigned int member_size, + unsigned int step) EINA_ARG_NONNULL(1); /** * @brief Remove every member from array. @@ -284,8 +288,8 @@ EAPI void eina_inarray_flush(Eina_Inarray *array) EINA_ARG_NONNULL(1); * * @since 1.2 */ -EAPI int eina_inarray_append(Eina_Inarray *array, - const void *data) EINA_ARG_NONNULL(1, 2); +EAPI int eina_inarray_push(Eina_Inarray *array, + const void *data) EINA_ARG_NONNULL(1, 2); /** * @brief Copy the data to array at position found by comparison function @@ -304,7 +308,7 @@ EAPI int eina_inarray_append(Eina_Inarray *array, * * @see eina_inarray_insert_sorted() * @see eina_inarray_insert_at() - * @see eina_inarray_append() + * @see eina_inarray_push() * * @since 1.2 */ @@ -359,11 +363,13 @@ EAPI int eina_inarray_remove(Eina_Inarray *array, /** * @brief Removes the last member of the array * @param array array object - * @return the index of the removed member or -1 on errors. + * @return the data poped out of the array. + * + * Note: The data could be considered valid only until any other operation touch the Inarray. * * @since 1.2 */ -EAPI int eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1); +EAPI void *eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1); /** * @brief Get the member at given position @@ -373,7 +379,7 @@ EAPI int eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1); * * Gets the member given its position in the array. It is a pointer to * its current memory, then it can be invalidated with functions that - * changes the array such as eina_inarray_append(), + * changes the array such as eina_inarray_push(), * eina_inarray_insert_at() or eina_inarray_remove_at() or variants. * * See also eina_inarray_lookup() and eina_inarray_lookup_sorted(). diff --git a/legacy/eina/src/lib/eina_inarray.c b/legacy/eina/src/lib/eina_inarray.c index dc95bc600f..73ac47d936 100644 --- a/legacy/eina/src/lib/eina_inarray.c +++ b/legacy/eina/src/lib/eina_inarray.c @@ -114,6 +114,7 @@ static void _eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step) { EINA_MAGIC_SET(array, EINA_MAGIC_INARRAY); + array->version = EINA_ARRAY_VERSION; array->member_size = member_size; array->len = 0; array->max = 0; @@ -127,7 +128,7 @@ _eina_inarray_resize(Eina_Inarray *array, unsigned int new_size) unsigned int new_max; void *tmp; - if (new_size < array->max) + if (new_size < array->max) /* don't change this behaviour as eina_inarray_pop rely on it */ return EINA_TRUE; if (new_size % array->step == 0) @@ -360,10 +361,24 @@ eina_inarray_free(Eina_Inarray *array) } EAPI void -eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step) +eina_inarray_step_set(Eina_Inarray *array, + unsigned int sizeof_eina_inarray, + unsigned int member_size, + unsigned int step) { EINA_SAFETY_ON_NULL_RETURN(array); EINA_SAFETY_ON_TRUE_RETURN(member_size == 0); + + if (sizeof (Eina_Inarray) != sizeof_eina_inarray) + { + ERR("Unknow Eina_Inarray size ! Got %i, expected %i\n", + sizeof_eina_inarray, + (int) sizeof (Eina_Inarray)); + /* Force memory to zero to provide a small layer of security */ + memset(array, 0, sizeof_eina_inarray); + return ; + } + _eina_inarray_setup(array, member_size, step); } @@ -378,7 +393,7 @@ eina_inarray_flush(Eina_Inarray *array) } EAPI int -eina_inarray_append(Eina_Inarray *array, const void *data) +eina_inarray_push(Eina_Inarray *array, const void *data) { void *p; @@ -421,7 +436,7 @@ eina_inarray_insert(Eina_Inarray *array, const void *data, Eina_Compare_Cb compa return -1; return position; } - return eina_inarray_append(array, data); + return eina_inarray_push(array, data); } EAPI int @@ -481,15 +496,15 @@ found: return position; } -EAPI int +EAPI void * eina_inarray_pop(Eina_Inarray *array) { - EINA_MAGIC_CHECK_INARRAY(array, -1); - EINA_SAFETY_ON_TRUE_RETURN_VAL(array->len == 0, -1); + EINA_MAGIC_CHECK_INARRAY(array, NULL); + EINA_SAFETY_ON_TRUE_RETURN_VAL(array->len == 0, NULL); if (!_eina_inarray_resize(array, array->len - 1)) - return -1; + return NULL; array->len--; - return array->len + 1; + return _eina_inarray_get(array, array->len + 1); } EAPI void * diff --git a/legacy/eina/src/lib/eina_value.c b/legacy/eina/src/lib/eina_value.c index a436b3ca33..8f5be6cce8 100644 --- a/legacy/eina/src/lib/eina_value.c +++ b/legacy/eina/src/lib/eina_value.c @@ -2666,7 +2666,7 @@ _eina_value_type_array_pset(const Eina_Value_Type *type, void *mem, const void * if (tmem->array) { _eina_value_type_array_flush_elements(tmem); - eina_inarray_setup(tmem->array, desc->subtype->value_size, desc->step); + eina_inarray_step_set(tmem->array, sizeof (Eina_Inarray), desc->subtype->value_size, desc->step); } else { diff --git a/legacy/eina/src/tests/eina_test_inarray.c b/legacy/eina/src/tests/eina_test_inarray.c index 22ba763770..5f33f4710c 100644 --- a/legacy/eina/src/tests/eina_test_inarray.c +++ b/legacy/eina/src/tests/eina_test_inarray.c @@ -46,7 +46,7 @@ START_TEST(eina_inarray_test_simple) for (i = 0; i < test_members; i++) { - pos = eina_inarray_append(array, &i); + pos = eina_inarray_push(array, &i); fail_unless(pos == i); } fail_unless(eina_inarray_count(array) == (unsigned)test_members); @@ -251,7 +251,7 @@ START_TEST(eina_inarray_test_sort) for (i = 0; i < numbers_count; i++) { short val = rand_numbers[i]; - eina_inarray_append(array, &val); + eina_inarray_push(array, &val); } eina_inarray_sort(array, short_cmp); fail_unless(check_short_sorted(array)); @@ -273,7 +273,7 @@ START_TEST(eina_inarray_test_reverse) for (i = 0; i < numbers_count; i++) { short val = i; - eina_inarray_append(array, &val); + eina_inarray_push(array, &val); } eina_inarray_reverse(array); @@ -326,7 +326,7 @@ START_TEST(eina_inarray_test_itr) for (i = 0; i < numbers_count; i++) { short val = i; - eina_inarray_append(array, &val); + eina_inarray_push(array, &val); } i = 0; EINA_INARRAY_FOREACH(array, member) diff --git a/legacy/eina/src/tests/eina_test_value.c b/legacy/eina/src/tests/eina_test_value.c index 8074aca75f..d3692081ad 100644 --- a/legacy/eina/src/tests/eina_test_value.c +++ b/legacy/eina/src/tests/eina_test_value.c @@ -1133,11 +1133,11 @@ START_TEST(eina_value_test_array) inarray = eina_inarray_new(sizeof(char), 0); fail_unless(inarray != NULL); c = 11; - fail_unless(eina_inarray_append(inarray, &c) >= 0); + fail_unless(eina_inarray_push(inarray, &c) >= 0); c = 21; - fail_unless(eina_inarray_append(inarray, &c) >= 0); + fail_unless(eina_inarray_push(inarray, &c) >= 0); c = 31; - fail_unless(eina_inarray_append(inarray, &c) >= 0); + fail_unless(eina_inarray_push(inarray, &c) >= 0); desc.subtype = EINA_VALUE_TYPE_CHAR; desc.step = 0; desc.array = inarray; diff --git a/legacy/evas/src/modules/engines/buffer/evas_engine.c b/legacy/evas/src/modules/engines/buffer/evas_engine.c index 758ef10a7b..371c78361e 100644 --- a/legacy/evas/src/modules/engines/buffer/evas_engine.c +++ b/legacy/evas/src/modules/engines/buffer/evas_engine.c @@ -119,7 +119,7 @@ _output_setup(int w, } re->tb = evas_common_tilebuf_new(w, h); evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE); - eina_inarray_setup(&re->previous_rects, sizeof (Eina_Rectangle), 8); + eina_inarray_step_set(&re->previous_rects, sizeof (Eina_Inarray), sizeof (Eina_Rectangle), 8); return re; } @@ -312,7 +312,7 @@ eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, i Eina_Rectangle local; EINA_RECTANGLE_SET(&local, rect->x, rect->y, rect->w, rect->h); - eina_inarray_append(&re->previous_rects, &local); + eina_inarray_push(&re->previous_rects, &local); } /* and regenerate the damage list by tacking into account the damage over two frames */