1 /* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
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/>.
25 #include "eina_config.h"
26 #include "eina_private.h"
28 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
29 #include "eina_safety_checks.h"
30 #include "eina_lalloc.h"
32 /*============================================================================*
34 *============================================================================*/
46 Eina_Lalloc_Alloc alloc_cb;
47 Eina_Lalloc_Free free_cb;
54 /*============================================================================*
56 *============================================================================*/
58 /*============================================================================*
60 *============================================================================*/
63 * @addtogroup Eina_Lalloc_Group Lazy allocator
68 EAPI Eina_Lalloc *eina_lalloc_new(void *data,
69 Eina_Lalloc_Alloc alloc_cb,
70 Eina_Lalloc_Free free_cb,
75 EINA_SAFETY_ON_NULL_RETURN_VAL(alloc_cb, NULL);
76 EINA_SAFETY_ON_NULL_RETURN_VAL(free_cb, NULL);
78 a = calloc(1, sizeof(Eina_Lalloc));
80 a->alloc_cb = alloc_cb;
84 a->num_allocated = num_init;
85 a->alloc_cb(a->data, a->num_allocated);
91 EAPI void eina_lalloc_free(Eina_Lalloc *a)
93 EINA_SAFETY_ON_NULL_RETURN(a);
94 EINA_SAFETY_ON_NULL_RETURN(a->free_cb);
99 EAPI Eina_Bool eina_lalloc_element_add(Eina_Lalloc *a)
101 EINA_SAFETY_ON_NULL_RETURN_VAL(a, EINA_FALSE);
102 EINA_SAFETY_ON_NULL_RETURN_VAL(a->alloc_cb, EINA_FALSE);
104 if (a->num_elements == a->num_allocated)
106 if (a->alloc_cb(a->data, (1 << a->acc)) == EINA_TRUE)
108 a->num_allocated = (1 << a->acc);
120 EAPI Eina_Bool eina_lalloc_elements_add(Eina_Lalloc *a, int num)
124 EINA_SAFETY_ON_NULL_RETURN_VAL(a, EINA_FALSE);
125 EINA_SAFETY_ON_NULL_RETURN_VAL(a->alloc_cb, EINA_FALSE);
127 tmp = a->num_elements + num;
128 if (tmp > a->num_allocated)
133 allocated = a->num_allocated;
136 while (tmp > allocated)
138 allocated = (1 << acc);
142 if (a->alloc_cb(a->data, allocated) == EINA_TRUE)
144 a->num_allocated = allocated;
151 a->num_elements += num;