From f85866471b686888d01aecb7c54e99abf74a5937 Mon Sep 17 00:00:00 2001 From: cedric Date: Thu, 19 Aug 2010 12:02:28 +0000 Subject: [PATCH] * eina: fix structure content to be more futur proof and reduce risk of ABI breakage. git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@51390 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/include/eina_accessor.h | 3 +++ src/include/eina_array.h | 4 ++++ src/include/eina_iterator.h | 3 +++ src/lib/eina_array.c | 22 ++++++++++++++++++++-- src/lib/eina_file.c | 6 ++++-- src/lib/eina_hash.c | 8 +++++--- src/lib/eina_inlist.c | 4 +++- src/lib/eina_list.c | 5 ++++- src/lib/eina_matrixsparse.c | 2 ++ src/lib/eina_rbtree.c | 3 ++- src/lib/eina_tiler.c | 4 ++++ src/tests/eina_test_array.c | 2 +- 12 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/include/eina_accessor.h b/src/include/eina_accessor.h index ae81978..4d2f92e 100644 --- a/src/include/eina_accessor.h +++ b/src/include/eina_accessor.h @@ -51,6 +51,9 @@ typedef Eina_Bool (*Eina_Accessor_Lock_Callback)(Eina_Accessor *it); struct _Eina_Accessor { +#define EINA_ACCESSOR_VERSION 1 + int version; + Eina_Accessor_Get_At_Callback get_at EINA_ARG_NONNULL(1, 3) EINA_WARN_UNUSED_RESULT; Eina_Accessor_Get_Container_Callback get_container EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; Eina_Accessor_Free_Callback free EINA_ARG_NONNULL(1); diff --git a/src/include/eina_array.h b/src/include/eina_array.h index c5dc6ab..7c7fc82 100644 --- a/src/include/eina_array.h +++ b/src/include/eina_array.h @@ -65,6 +65,9 @@ typedef void **Eina_Array_Iterator; */ struct _Eina_Array { +#define EINA_ARRAY_VERSION 1 + int version; /**< Should match EINA_ARRAY_VERSION used when compiled your apps, provided for ABI compatibility */ + void **data; /**< Pointer to a vector of pointer to payload */ unsigned int total; /**< Total number of slots in the vector */ unsigned int count; /**< Number of active slots in the vector */ @@ -76,6 +79,7 @@ EAPI Eina_Array * eina_array_new(unsigned int step) EINA_WARN_UNUS EAPI Eina_Array * eina_array_threadsafe_new(unsigned int step) EINA_WARN_UNUSED_RESULT EINA_MALLOC EINA_WARN_UNUSED_RESULT; EAPI void eina_array_free(Eina_Array *array) EINA_ARG_NONNULL(1); EAPI void eina_array_step_set(Eina_Array *array, + unsigned int sizeof_eina_array, unsigned int step) EINA_ARG_NONNULL(1); EAPI void eina_array_clean(Eina_Array *array) EINA_ARG_NONNULL(1); EAPI void eina_array_flush(Eina_Array *array) EINA_ARG_NONNULL(1); diff --git a/src/include/eina_iterator.h b/src/include/eina_iterator.h index 7c68095..15188be 100644 --- a/src/include/eina_iterator.h +++ b/src/include/eina_iterator.h @@ -49,6 +49,9 @@ typedef Eina_Bool (*Eina_Iterator_Lock_Callback)(Eina_Iterator *it); struct _Eina_Iterator { +#define EINA_ITERATOR_VERSION 1 + int version; + Eina_Iterator_Next_Callback next EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; Eina_Iterator_Get_Container_Callback get_container EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; Eina_Iterator_Free_Callback free EINA_ARG_NONNULL(1); diff --git a/src/lib/eina_array.c b/src/lib/eina_array.c index 1c1004d..5eff8b1 100644 --- a/src/lib/eina_array.c +++ b/src/lib/eina_array.c @@ -415,6 +415,7 @@ eina_array_new(unsigned int step) EINA_MAGIC_SET(array, EINA_MAGIC_ARRAY); + array->version = EINA_ARRAY_VERSION; array->data = NULL; array->total = 0; array->count = 0; @@ -448,6 +449,7 @@ eina_array_free(Eina_Array *array) * @brief Set the step of an array. * * @param array The array. + * @param sizeof_array Should be the value returned by sizeof (Eina_Array). * @param step The count of pointers to add when increasing the array size. * * This function sets the step of @p array to @p step. For performance @@ -456,9 +458,23 @@ eina_array_free(Eina_Array *array) * the array is not initialized. */ EAPI void -eina_array_step_set(Eina_Array *array, unsigned int step) +eina_array_step_set(Eina_Array *array, + unsigned int sizeof_eina_array, + unsigned int step) { EINA_SAFETY_ON_NULL_RETURN(array); + + if (sizeof (Eina_Array) != sizeof_eina_array) + { + ERR("Unknow Eina_Array size ! Got %i, expected %i !\n", + sizeof_eina_array, + (int) sizeof (Eina_Array)); + /* Force memory to zero to provide a small layer of security */ + memset(array, 0, sizeof_eina_array); + return ; + } + + array->version = EINA_ARRAY_VERSION; array->data = NULL; array->total = 0; array->count = 0; @@ -654,6 +670,7 @@ eina_array_iterator_new(const Eina_Array *array) it->array = array; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(eina_array_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( eina_array_iterator_get_container); @@ -682,7 +699,7 @@ eina_array_accessor_new(const Eina_Array *array) EINA_SAFETY_ON_NULL_RETURN_VAL(array, NULL); EINA_MAGIC_CHECK_ARRAY(array); - eina_error_set(0); + eina_error_set(0); it = calloc(1, sizeof (Eina_Accessor_Array)); if (!it) { @@ -695,6 +712,7 @@ eina_array_accessor_new(const Eina_Array *array) it->array = array; + it->accessor.version = EINA_ACCESSOR_VERSION; it->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_array_accessor_get_at); it->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER( eina_array_accessor_get_container); diff --git a/src/lib/eina_file.c b/src/lib/eina_file.c index e4a701e..0e3a3df 100644 --- a/src/lib/eina_file.c +++ b/src/lib/eina_file.c @@ -431,7 +431,7 @@ eina_file_ls(const char *dir) if (length < 1) return NULL; - it = malloc(sizeof (Eina_File_Iterator) + length); + it = calloc(1, sizeof (Eina_File_Iterator) + length); if (!it) return NULL; @@ -450,6 +450,7 @@ eina_file_ls(const char *dir) else it->length = length - 1; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_file_ls_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_file_ls_iterator_container); @@ -498,7 +499,7 @@ eina_file_direct_ls(const char *dir) if (length + NAME_MAX + 2 >= PATH_MAX) return NULL; - it = malloc(sizeof(Eina_File_Direct_Iterator) + length); + it = calloc(1, sizeof(Eina_File_Direct_Iterator) + length); if (!it) return NULL; @@ -523,6 +524,7 @@ eina_file_direct_ls(const char *dir) it->info.name_start = length + 1; } + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_file_direct_ls_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_file_direct_ls_iterator_container); diff --git a/src/lib/eina_hash.c b/src/lib/eina_hash.c index 19e156a..076a407 100644 --- a/src/lib/eina_hash.c +++ b/src/lib/eina_hash.c @@ -1621,7 +1621,7 @@ eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL); EINA_MAGIC_CHECK_HASH(hash); - eina_error_set(0); + eina_error_set(0); it = calloc(1, sizeof (Eina_Iterator_Hash)); if (!it) { @@ -1632,6 +1632,7 @@ eina_hash_iterator_data_new(const Eina_Hash *hash) it->hash = hash; it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_data_get_content); + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_hash_iterator_get_container); @@ -1670,7 +1671,7 @@ eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL); EINA_MAGIC_CHECK_HASH(hash); - eina_error_set(0); + eina_error_set(0); it = calloc(1, sizeof (Eina_Iterator_Hash)); if (!it) { @@ -1682,6 +1683,7 @@ eina_hash_iterator_key_new(const Eina_Hash *hash) it->get_content = FUNC_ITERATOR_GET_CONTENT( _eina_hash_iterator_key_get_content); + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_hash_iterator_get_container); @@ -1723,7 +1725,6 @@ eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL); EINA_MAGIC_CHECK_HASH(hash); - eina_error_set(0); it = calloc(1, sizeof (Eina_Iterator_Hash)); if (!it) @@ -1736,6 +1737,7 @@ eina_hash_iterator_tuple_new(const Eina_Hash *hash) it->get_content = FUNC_ITERATOR_GET_CONTENT( _eina_hash_iterator_tuple_get_content); + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_hash_iterator_get_container); diff --git a/src/lib/eina_inlist.c b/src/lib/eina_inlist.c index a02799b..82873b7 100644 --- a/src/lib/eina_inlist.c +++ b/src/lib/eina_inlist.c @@ -626,7 +626,7 @@ eina_inlist_iterator_new(const Eina_Inlist *list) { Eina_Iterator_Inlist *it; - eina_error_set(0); + eina_error_set(0); it = calloc(1, sizeof (Eina_Iterator_Inlist)); if (!it) { @@ -637,6 +637,7 @@ eina_inlist_iterator_new(const Eina_Inlist *list) it->head = list; it->current = list; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(eina_inlist_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( eina_inlist_iterator_get_container); @@ -676,6 +677,7 @@ eina_inlist_accessor_new(const Eina_Inlist *list) it->current = list; it->index = 0; + it->accessor.version = EINA_ACCESSOR_VERSION; it->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_inlist_accessor_get_at); it->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER( eina_inlist_accessor_get_container); diff --git a/src/lib/eina_list.c b/src/lib/eina_list.c index 5a6c2aa..2b140d2 100644 --- a/src/lib/eina_list.c +++ b/src/lib/eina_list.c @@ -2064,7 +2064,7 @@ eina_list_iterator_new(const Eina_List *list) { Eina_Iterator_List *it; - eina_error_set(0); + eina_error_set(0); it = calloc(1, sizeof (Eina_Iterator_List)); if (!it) { @@ -2078,6 +2078,7 @@ eina_list_iterator_new(const Eina_List *list) it->head = list; it->current = list; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(eina_list_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( eina_list_iterator_get_container); @@ -2127,6 +2128,7 @@ eina_list_iterator_reversed_new(const Eina_List *list) it->head = eina_list_last(list); it->current = it->head; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(eina_list_iterator_prev); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( eina_list_iterator_get_container); @@ -2167,6 +2169,7 @@ eina_list_accessor_new(const Eina_List *list) it->current = list; it->index = 0; + it->accessor.version = EINA_ACCESSOR_VERSION; it->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_list_accessor_get_at); it->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER( eina_list_accessor_get_container); diff --git a/src/lib/eina_matrixsparse.c b/src/lib/eina_matrixsparse.c index 735f77c..4857a53 100644 --- a/src/lib/eina_matrixsparse.c +++ b/src/lib/eina_matrixsparse.c @@ -1608,6 +1608,7 @@ eina_matrixsparse_iterator_new(const Eina_Matrixsparse *m) it->ref.row = m->rows; it->ref.col = m->rows ? m->rows->cols : NULL; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_matrixsparse_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_matrixsparse_iterator_get_container); @@ -1670,6 +1671,7 @@ eina_matrixsparse_iterator_complete_new(const Eina_Matrixsparse *m) it->dummy.col.parent = &it->dummy.row; EINA_MAGIC_SET(&it->dummy.col, EINA_MAGIC_MATRIXSPARSE_CELL); + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT( _eina_matrixsparse_iterator_complete_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( diff --git a/src/lib/eina_rbtree.c b/src/lib/eina_rbtree.c index 0f25a23..6d4130b 100644 --- a/src/lib/eina_rbtree.c +++ b/src/lib/eina_rbtree.c @@ -184,7 +184,7 @@ _eina_rbtree_iterator_build(const Eina_Rbtree *root, unsigned char mask) Eina_Iterator_Rbtree_List *first; Eina_Iterator_Rbtree *it; - eina_error_set(0); + eina_error_set(0); it = calloc(1, sizeof (Eina_Iterator_Rbtree)); if (!it) { @@ -204,6 +204,7 @@ _eina_rbtree_iterator_build(const Eina_Rbtree *root, unsigned char mask) it->mask = mask; + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_eina_rbtree_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _eina_rbtree_iterator_get_content); diff --git a/src/lib/eina_tiler.c b/src/lib/eina_tiler.c index 13251c8..9d3f4cb 100644 --- a/src/lib/eina_tiler.c +++ b/src/lib/eina_tiler.c @@ -1190,6 +1190,7 @@ EAPI Eina_Iterator *eina_tiler_iterator_new(const Eina_Tiler *t) Eina_Iterator_Tiler *it; EINA_MAGIC_CHECK_TILER(t, NULL); + it = calloc(1, sizeof (Eina_Iterator_Tiler)); if (!it) return NULL; @@ -1209,6 +1210,8 @@ EAPI Eina_Iterator *eina_tiler_iterator_new(const Eina_Tiler *t) } it->curr = it->tiler->splitter.rects.head; + + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(_iterator_next); it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( _iterator_get_container); @@ -1282,6 +1285,7 @@ eina_tile_grid_slicer_iterator_new(int x, EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR); + it->iterator.version = EINA_ITERATOR_VERSION; it->iterator.next = FUNC_ITERATOR_NEXT(eina_tile_grid_slicer_iterator_next); it->iterator.free = FUNC_ITERATOR_FREE(eina_tile_grid_slicer_iterator_free); diff --git a/src/tests/eina_test_array.c b/src/tests/eina_test_array.c index dec8981..0b054ea 100644 --- a/src/tests/eina_test_array.c +++ b/src/tests/eina_test_array.c @@ -78,7 +78,7 @@ START_TEST(eina_array_static) eina_init(); - eina_array_step_set(&sea, 10); + eina_array_step_set(&sea, sizeof(sea), 10); for (i = 0; i < 200; ++i) { -- 2.7.4