1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
29 #include "eina_config.h"
30 #include "eina_private.h"
31 #include "eina_error.h"
34 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
35 #include "eina_safety_checks.h"
36 #include "eina_array.h"
38 /*============================================================================*
40 *============================================================================*/
46 static const char EINA_MAGIC_ARRAY_STR[] = "Eina Array";
47 static const char EINA_MAGIC_ARRAY_ITERATOR_STR[] = "Eina Array Iterator";
48 static const char EINA_MAGIC_ARRAY_ACCESSOR_STR[] = "Eina Array Accessor";
50 #define EINA_MAGIC_CHECK_ARRAY(d) \
52 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ARRAY)) { \
53 EINA_MAGIC_FAIL(d, EINA_MAGIC_ARRAY); } \
56 #define EINA_MAGIC_CHECK_ARRAY_ITERATOR(d, ...) \
58 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ARRAY_ITERATOR)) \
60 EINA_MAGIC_FAIL(d, EINA_MAGIC_ARRAY_ITERATOR); \
65 #define EINA_MAGIC_CHECK_ARRAY_ACCESSOR(d, ...) \
67 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ARRAY_ACCESSOR)) \
69 EINA_MAGIC_FAIL(d, EINA_MAGIC_ACCESSOR); \
75 typedef struct _Eina_Iterator_Array Eina_Iterator_Array;
76 struct _Eina_Iterator_Array
78 Eina_Iterator iterator;
80 const Eina_Array *array;
86 typedef struct _Eina_Accessor_Array Eina_Accessor_Array;
87 struct _Eina_Accessor_Array
89 Eina_Accessor accessor;
90 const Eina_Array *array;
94 static int _eina_array_log_dom = -1;
99 #define ERR(...) EINA_LOG_DOM_ERR(_eina_array_log_dom, __VA_ARGS__)
104 #define DBG(...) EINA_LOG_DOM_DBG(_eina_array_log_dom, __VA_ARGS__)
106 static void eina_array_iterator_free(Eina_Iterator_Array *it) EINA_ARG_NONNULL(1);
107 static Eina_Array *eina_array_iterator_get_container(Eina_Iterator_Array *it) EINA_ARG_NONNULL(1);
108 static Eina_Bool eina_array_iterator_next(Eina_Iterator_Array *it,
109 void **data) EINA_ARG_NONNULL(1);
111 static Eina_Bool eina_array_accessor_get_at(Eina_Accessor_Array *it,
113 void **data) EINA_ARG_NONNULL(1);
114 static Eina_Array *eina_array_accessor_get_container(Eina_Accessor_Array *it) EINA_ARG_NONNULL(1);
115 static void eina_array_accessor_free(Eina_Accessor_Array *it) EINA_ARG_NONNULL(1);
118 eina_array_iterator_next(Eina_Iterator_Array *it, void **data)
120 EINA_MAGIC_CHECK_ARRAY_ITERATOR(it, EINA_FALSE);
122 if (!(it->index < eina_array_count(it->array)))
126 *data = eina_array_data_get(it->array, it->index);
133 eina_array_iterator_get_container(Eina_Iterator_Array *it)
135 EINA_MAGIC_CHECK_ARRAY_ITERATOR(it, NULL);
136 return (Eina_Array *)it->array;
140 eina_array_iterator_free(Eina_Iterator_Array *it)
142 EINA_MAGIC_CHECK_ARRAY_ITERATOR(it);
147 eina_array_accessor_get_at(Eina_Accessor_Array *it,
151 EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it, EINA_FALSE);
153 if (!(idx < eina_array_count(it->array)))
157 *data = eina_array_data_get(it->array, idx);
163 eina_array_accessor_get_container(Eina_Accessor_Array *it)
165 EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it, NULL);
166 return (Eina_Array *)it->array;
170 eina_array_accessor_free(Eina_Accessor_Array *it)
172 EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it);
176 /* used from eina_inline_array.x, thus a needed symbol */
178 eina_array_grow(Eina_Array *array)
183 EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
185 EINA_MAGIC_CHECK_ARRAY(array);
187 total = array->total + array->step;
189 tmp = realloc(array->data, sizeof (void *) * total);
190 if (EINA_UNLIKELY(!tmp))
192 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
196 array->total = total;
207 /*============================================================================*
209 *============================================================================*/
213 * @brief Initialize the array module.
215 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
217 * This function sets up the error and magic modules or Eina. It is
218 * called by eina_init().
223 eina_array_init(void)
225 _eina_array_log_dom = eina_log_domain_register("eina_array",
226 EINA_LOG_COLOR_DEFAULT);
227 if (_eina_array_log_dom < 0)
229 EINA_LOG_ERR("Could not register log domain: eina_array");
233 #define EMS(n) eina_magic_string_static_set(n, n ## _STR)
234 EMS(EINA_MAGIC_ARRAY);
235 EMS(EINA_MAGIC_ARRAY_ITERATOR);
236 EMS(EINA_MAGIC_ARRAY_ACCESSOR);
243 * @brief Shut down the array module.
245 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
247 * This function shuts down the array module set up by
248 * eina_array_init(). It is called by eina_shutdown().
250 * @see eina_shutdown()
253 eina_array_shutdown(void)
255 eina_log_domain_unregister(_eina_array_log_dom);
256 _eina_array_log_dom = -1;
260 /*============================================================================*
262 *============================================================================*/
265 eina_array_new(unsigned int step)
270 array = malloc(sizeof (Eina_Array));
273 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
277 EINA_MAGIC_SET(array, EINA_MAGIC_ARRAY);
279 array->version = EINA_ARRAY_VERSION;
289 eina_array_free(Eina_Array *array)
291 eina_array_flush(array);
293 EINA_SAFETY_ON_NULL_RETURN(array);
294 EINA_MAGIC_CHECK_ARRAY(array);
299 eina_array_step_set(Eina_Array *array,
300 unsigned int sizeof_eina_array,
303 EINA_SAFETY_ON_NULL_RETURN(array);
305 if (sizeof (Eina_Array) != sizeof_eina_array)
307 ERR("Unknow Eina_Array size ! Got %i, expected %i !\n",
309 (int) sizeof (Eina_Array));
310 /* Force memory to zero to provide a small layer of security */
311 memset(array, 0, sizeof_eina_array);
315 array->version = EINA_ARRAY_VERSION;
320 EINA_MAGIC_SET(array, EINA_MAGIC_ARRAY);
324 eina_array_flush(Eina_Array *array)
326 EINA_SAFETY_ON_NULL_RETURN(array);
327 EINA_MAGIC_CHECK_ARRAY(array);
340 eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data,
346 The algorithm does exit before using unitialized data. So compiler is
347 giving you a false positiv here too.
350 unsigned int total = 0;
354 EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
355 EINA_SAFETY_ON_NULL_RETURN_VAL(keep, EINA_FALSE);
356 EINA_MAGIC_CHECK_ARRAY(array);
358 if (array->total == 0)
361 for (i = 0; i < array->count; ++i)
363 data = eina_array_data_get(array, i);
365 if (keep(data, gdata) == EINA_FALSE)
369 if (i < array->count)
372 for (; i < array->count; ++i)
374 data = eina_array_data_get(array, i);
376 if (keep(data, gdata) == EINA_TRUE)
379 /* Special case all objects that need to stay are at the beginning of the array. */
380 if (i == array->count)
382 array->count = limit;
383 if (array->count == 0)
394 tmp = malloc(sizeof (void *) * array->total);
397 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
401 memcpy(tmp, array->data, limit * sizeof(void *));
404 if (i < array->count)
411 for (; i < array->count; ++i)
413 data = eina_array_data_get(array, i);
415 if (keep(data, gdata))
424 /* If we do not keep any object in the array, we should have exited
425 earlier in test (i == array->count). */
429 array->count = total;
434 eina_array_iterator_new(const Eina_Array *array)
436 Eina_Iterator_Array *it;
438 EINA_SAFETY_ON_NULL_RETURN_VAL(array, NULL);
439 EINA_MAGIC_CHECK_ARRAY(array);
442 it = calloc(1, sizeof (Eina_Iterator_Array));
445 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
449 EINA_MAGIC_SET(it, EINA_MAGIC_ARRAY_ITERATOR);
450 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
454 it->iterator.version = EINA_ITERATOR_VERSION;
455 it->iterator.next = FUNC_ITERATOR_NEXT(eina_array_iterator_next);
456 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
457 eina_array_iterator_get_container);
458 it->iterator.free = FUNC_ITERATOR_FREE(eina_array_iterator_free);
460 return &it->iterator;
464 eina_array_accessor_new(const Eina_Array *array)
466 Eina_Accessor_Array *ac;
468 EINA_SAFETY_ON_NULL_RETURN_VAL(array, NULL);
469 EINA_MAGIC_CHECK_ARRAY(array);
472 ac = calloc(1, sizeof (Eina_Accessor_Array));
475 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
479 EINA_MAGIC_SET(ac, EINA_MAGIC_ARRAY_ACCESSOR);
480 EINA_MAGIC_SET(&ac->accessor, EINA_MAGIC_ACCESSOR);
484 ac->accessor.version = EINA_ACCESSOR_VERSION;
485 ac->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_array_accessor_get_at);
486 ac->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER(
487 eina_array_accessor_get_container);
488 ac->accessor.free = FUNC_ACCESSOR_FREE(eina_array_accessor_free);
490 return &ac->accessor;