aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/lib/eina_lalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/lib/eina_lalloc.c')
-rw-r--r--libraries/eina/src/lib/eina_lalloc.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/libraries/eina/src/lib/eina_lalloc.c b/libraries/eina/src/lib/eina_lalloc.c
new file mode 100644
index 0000000..b1e62b7
--- /dev/null
+++ b/libraries/eina/src/lib/eina_lalloc.c
@@ -0,0 +1,158 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
3 *
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.
8 *
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.
13 *
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/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <stdlib.h>
24
25#include "eina_config.h"
26#include "eina_private.h"
27
28/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
29#include "eina_safety_checks.h"
30#include "eina_lalloc.h"
31
32/*============================================================================*
33* Local *
34*============================================================================*/
35
36/**
37 * @cond LOCAL
38 */
39
40struct _Eina_Lalloc
41{
42 void *data;
43 int num_allocated;
44 int num_elements;
45 int acc;
46 Eina_Lalloc_Alloc alloc_cb;
47 Eina_Lalloc_Free free_cb;
48};
49
50/**
51 * @endcond
52 */
53
54/*============================================================================*
55* Global *
56*============================================================================*/
57
58/*============================================================================*
59* API *
60*============================================================================*/
61
62/**
63 * @addtogroup Eina_Lalloc_Group Lazy allocator
64 *
65 * @{
66 */
67
68EAPI Eina_Lalloc *eina_lalloc_new(void *data,
69 Eina_Lalloc_Alloc alloc_cb,
70 Eina_Lalloc_Free free_cb,
71 int num_init)
72{
73 Eina_Lalloc *a;
74
75 EINA_SAFETY_ON_NULL_RETURN_VAL(alloc_cb, NULL);
76 EINA_SAFETY_ON_NULL_RETURN_VAL(free_cb, NULL);
77
78 a = calloc(1, sizeof(Eina_Lalloc));
79 a->data = data;
80 a->alloc_cb = alloc_cb;
81 a->free_cb = free_cb;
82 if (num_init > 0)
83 {
84 a->num_allocated = num_init;
85 a->alloc_cb(a->data, a->num_allocated);
86 }
87
88 return a;
89}
90
91EAPI void eina_lalloc_free(Eina_Lalloc *a)
92{
93 EINA_SAFETY_ON_NULL_RETURN(a);
94 EINA_SAFETY_ON_NULL_RETURN(a->free_cb);
95 a->free_cb(a->data);
96 free(a);
97}
98
99EAPI Eina_Bool eina_lalloc_element_add(Eina_Lalloc *a)
100{
101 EINA_SAFETY_ON_NULL_RETURN_VAL(a, EINA_FALSE);
102 EINA_SAFETY_ON_NULL_RETURN_VAL(a->alloc_cb, EINA_FALSE);
103
104 if (a->num_elements == a->num_allocated)
105 {
106 if (a->alloc_cb(a->data, (1 << a->acc)) == EINA_TRUE)
107 {
108 a->num_allocated = (1 << a->acc);
109 a->acc++;
110 }
111 else
112 return EINA_FALSE;
113 }
114
115 a->num_elements++;
116
117 return EINA_TRUE;
118}
119
120EAPI Eina_Bool eina_lalloc_elements_add(Eina_Lalloc *a, int num)
121{
122 int tmp;
123
124 EINA_SAFETY_ON_NULL_RETURN_VAL(a, EINA_FALSE);
125 EINA_SAFETY_ON_NULL_RETURN_VAL(a->alloc_cb, EINA_FALSE);
126
127 tmp = a->num_elements + num;
128 if (tmp > a->num_allocated)
129 {
130 int allocated;
131 int acc;
132
133 allocated = a->num_allocated;
134 acc = a->acc;
135
136 while (tmp > allocated)
137 {
138 allocated = (1 << acc);
139 acc++;
140 }
141
142 if (a->alloc_cb(a->data, allocated) == EINA_TRUE)
143 {
144 a->num_allocated = allocated;
145 a->acc = acc;
146 }
147 else
148 return EINA_FALSE;
149 }
150
151 a->num_elements += num;
152
153 return EINA_TRUE;
154}
155
156/**
157 * @}
158 */