summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_array.c
blob: 2a7446e6475b614b30f7469862592c1e047bff5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* EINA - EFL data type library
 * Copyright (C) 2008 Cedric Bail
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "eina_config.h"
#include "eina_private.h"
#include "eina_log.h"

/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
#include "eina_safety_checks.h"
#include "eina_array.h"

/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/

/**
 * @cond LOCAL
 */

static const char EINA_MAGIC_ARRAY_STR[] = "Eina Array";
static const char EINA_MAGIC_ARRAY_ITERATOR_STR[] = "Eina Array Iterator";
static const char EINA_MAGIC_ARRAY_ACCESSOR_STR[] = "Eina Array Accessor";

#define EINA_MAGIC_CHECK_ARRAY(d, ...) \
   do { \
        if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ARRAY)) \
          { \
             EINA_MAGIC_FAIL(d, EINA_MAGIC_ARRAY); \
             return __VA_ARGS__; \
          } \
     } while (0)

#define EINA_MAGIC_CHECK_ARRAY_ITERATOR(d, ...)                 \
   do {                                                          \
        if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ARRAY_ITERATOR))       \
          {                                                        \
             EINA_MAGIC_FAIL(d, EINA_MAGIC_ARRAY_ITERATOR);        \
             return __VA_ARGS__;                                   \
          }                                                        \
     } while (0)

#define EINA_MAGIC_CHECK_ARRAY_ACCESSOR(d, ...)                 \
   do {                                                          \
        if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ARRAY_ACCESSOR))       \
          {                                                        \
             EINA_MAGIC_FAIL(d, EINA_MAGIC_ACCESSOR);              \
             return __VA_ARGS__;                                   \
          }                                                        \
     } while (0)


typedef struct _Eina_Iterator_Array Eina_Iterator_Array;
struct _Eina_Iterator_Array
{
   Eina_Iterator iterator;

   const Eina_Array *array;
   unsigned int index;

   EINA_MAGIC
};

typedef struct _Eina_Accessor_Array Eina_Accessor_Array;
struct _Eina_Accessor_Array
{
   Eina_Accessor accessor;
   const Eina_Array *array;
   EINA_MAGIC
};

static int _eina_array_log_dom = -1;

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_eina_array_log_dom, __VA_ARGS__)

#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_eina_array_log_dom, __VA_ARGS__)

static void        eina_array_iterator_free(Eina_Iterator_Array *it) EINA_ARG_NONNULL(1);
static Eina_Array *eina_array_iterator_get_container(Eina_Iterator_Array *it) EINA_ARG_NONNULL(1);
static Eina_Bool   eina_array_iterator_next(Eina_Iterator_Array *it,
                                            void **data) EINA_ARG_NONNULL(1);

static Eina_Bool   eina_array_accessor_get_at(Eina_Accessor_Array *it,
                                              unsigned int idx,
                                              void **data) EINA_ARG_NONNULL(1);
static Eina_Array *eina_array_accessor_get_container(Eina_Accessor_Array *it) EINA_ARG_NONNULL(1);
static void        eina_array_accessor_free(Eina_Accessor_Array *it) EINA_ARG_NONNULL(1);

static Eina_Bool
eina_array_iterator_next(Eina_Iterator_Array *it, void **data)
{
   EINA_MAGIC_CHECK_ARRAY_ITERATOR(it, EINA_FALSE);

   if (!(it->index < eina_array_count(it->array)))
      return EINA_FALSE;

   if (data)
      *data = eina_array_data_get(it->array, it->index);

   it->index++;
   return EINA_TRUE;
}

static Eina_Array *
eina_array_iterator_get_container(Eina_Iterator_Array *it)
{
   EINA_MAGIC_CHECK_ARRAY_ITERATOR(it, NULL);
   return (Eina_Array *)it->array;
}

static void
eina_array_iterator_free(Eina_Iterator_Array *it)
{
   EINA_MAGIC_CHECK_ARRAY_ITERATOR(it);
   MAGIC_FREE(it);
}

static Eina_Bool
eina_array_accessor_get_at(Eina_Accessor_Array *it,
                           unsigned int idx,
                           void **data)
{
   EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it, EINA_FALSE);

   if (!(idx < eina_array_count(it->array)))
      return EINA_FALSE;

   if (data)
      *data = eina_array_data_get(it->array, idx);

   return EINA_TRUE;
}

static Eina_Array *
eina_array_accessor_get_container(Eina_Accessor_Array *it)
{
   EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it, NULL);
   return (Eina_Array *)it->array;
}

static void
eina_array_accessor_free(Eina_Accessor_Array *it)
{
   EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it);
   MAGIC_FREE(it);
}

static Eina_Accessor *
eina_array_accessor_clone(const Eina_Accessor_Array *it)
{
   Eina_Accessor_Array *ac;

   EINA_MAGIC_CHECK_ARRAY_ACCESSOR(it, NULL);

   ac = calloc(1, sizeof (Eina_Accessor_Array));
   if (!ac) return NULL;

   memcpy(ac, it, sizeof(Eina_Accessor_Array));

   return &ac->accessor;
}

/* used from eina_inline_array.x, thus a needed symbol */
EAPI Eina_Bool
eina_array_grow(Eina_Array *array)
{
   void **tmp;
   unsigned int total;

   EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
   EINA_MAGIC_CHECK_ARRAY(array, EINA_FALSE);

   total = array->total + array->step;
   tmp = realloc(array->data, sizeof (void *) * total);
   if (EINA_UNLIKELY(!tmp)) return 0;

   array->total = total;
   array->data = tmp;

   return 1;
}

/**
 * @endcond
 */


/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/

/**
 * @internal
 * @brief Initialize the array module.
 *
 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
 *
 * This function sets up the error and magic modules or Eina. It is
 * called by eina_init().
 *
 * @see eina_init()
 */
Eina_Bool
eina_array_init(void)
{
   _eina_array_log_dom = eina_log_domain_register("eina_array",
                                                  EINA_LOG_COLOR_DEFAULT);
   if (_eina_array_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: eina_array");
        return EINA_FALSE;
     }

#define EMS(n) eina_magic_string_static_set(n, n ## _STR)
   EMS(EINA_MAGIC_ARRAY);
   EMS(EINA_MAGIC_ARRAY_ITERATOR);
   EMS(EINA_MAGIC_ARRAY_ACCESSOR);
#undef EMS
   return EINA_TRUE;
}

/**
 * @internal
 * @brief Shut down the array module.
 *
 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
 *
 * This function shuts down the array module set up by
 * eina_array_init(). It is called by eina_shutdown().
 *
 * @see eina_shutdown()
 */
Eina_Bool
eina_array_shutdown(void)
{
   eina_log_domain_unregister(_eina_array_log_dom);
   _eina_array_log_dom = -1;
   return EINA_TRUE;
}

/*============================================================================*
 *                                   API                                      *
 *============================================================================*/

EAPI Eina_Array *
eina_array_new(unsigned int step)
{
   Eina_Array *array;

   array = malloc(sizeof (Eina_Array));
   if (!array) return NULL;

   EINA_MAGIC_SET(array, EINA_MAGIC_ARRAY);

   array->version = EINA_ARRAY_VERSION;
   array->data = NULL;
   array->total = 0;
   array->count = 0;
   array->step = step;

   return array;
}

EAPI void
eina_array_free(Eina_Array *array)
{
   if (!array) return;
   eina_array_flush(array);

   EINA_MAGIC_CHECK_ARRAY(array);
   MAGIC_FREE(array);
}

EAPI void
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;
   array->step = step;
   EINA_MAGIC_SET(array, EINA_MAGIC_ARRAY);
}

EAPI void
eina_array_flush(Eina_Array *array)
{
   EINA_SAFETY_ON_NULL_RETURN(array);
   EINA_MAGIC_CHECK_ARRAY(array);

   array->count = 0;
   array->total = 0;

   if (!array->data)
      return;

   free(array->data);
   array->data = NULL;
}

EAPI Eina_Bool
eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data,
                                                       void *gdata),
                  void *gdata)
{
   unsigned int i, j, size, count;
   void *data, **tmp;

   EINA_SAFETY_ON_NULL_RETURN_VAL(array, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(keep,  EINA_FALSE);
   EINA_MAGIC_CHECK_ARRAY(array, EINA_FALSE);

   if (array->total == 0) return EINA_TRUE;
   // 1. walk through all items and shuffle down any items on top of
   // previously removed item slots
   for (count = array->count, tmp = array->data, j = 0, i = 0; i < count; i++)
     {
        data = tmp[i];
        if (keep(data, gdata) == EINA_TRUE)
          {
             // we keep it - store it (again) (and j will be <= i ALWAYS)
             tmp[j] = data;
             j++;
          }
     }
   array->count = j;
   // 2. if we reduced size by more than N (block size) then realloc back down
   if ((array->total - array->count) >= array->step)
     {
        if (array->count == 0)
          {
             free(array->data);
             array->total = 0;
             array->data = NULL;
          }
        else
          {
             // realloc back down - rounding up to the nearest step size
             size = ((array->count / array->step) + (array->count % array->step ? 1 : 0)) * array->step;
             tmp = realloc(array->data, sizeof(void *) * size);
             if (!tmp) return EINA_FALSE;
             array->total = size;
             array->data = tmp;
          }
     }
   return EINA_TRUE;
}

EAPI Eina_Iterator *
eina_array_iterator_new(const Eina_Array *array)
{
   Eina_Iterator_Array *it;

   EINA_SAFETY_ON_NULL_RETURN_VAL(array, NULL);
   EINA_MAGIC_CHECK_ARRAY(array, NULL);

   it = calloc(1, sizeof (Eina_Iterator_Array));
   if (!it) return NULL;

   EINA_MAGIC_SET(it,            EINA_MAGIC_ARRAY_ITERATOR);
   EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);

   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);
   it->iterator.free = FUNC_ITERATOR_FREE(eina_array_iterator_free);

   return &it->iterator;
}

EAPI Eina_Accessor *
eina_array_accessor_new(const Eina_Array *array)
{
   Eina_Accessor_Array *ac;

   EINA_SAFETY_ON_NULL_RETURN_VAL(array, NULL);
   EINA_MAGIC_CHECK_ARRAY(array, NULL);

   ac = calloc(1, sizeof (Eina_Accessor_Array));
   if (!ac) return NULL;

   EINA_MAGIC_SET(ac,            EINA_MAGIC_ARRAY_ACCESSOR);
   EINA_MAGIC_SET(&ac->accessor, EINA_MAGIC_ACCESSOR);

   ac->array = array;

   ac->accessor.version = EINA_ACCESSOR_VERSION;
   ac->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_array_accessor_get_at);
   ac->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER(
         eina_array_accessor_get_container);
   ac->accessor.free = FUNC_ACCESSOR_FREE(eina_array_accessor_free);
   ac->accessor.clone = FUNC_ACCESSOR_CLONE(eina_array_accessor_clone);

   return &ac->accessor;
}