17 #include <core/dependencies.h>
18 #include <core/allocator.h>
27 #ifdef POK_NEEDS_ALLOCATOR
29 #ifndef POK_CONFIG_ALLOCATOR_MEMORY_SIZE
30 #define POK_CONFIG_ALLOCATOR_MEMORY_SIZE 16384
33 #ifndef POK_CONFIG_ALLOCATOR_NB_SPACES
34 #define POK_CONFIG_ALLOCATOR_NB_SPACES 100
37 #define POK_ALLOCATOR_CHECK_INIT \
38 if (pok_allocator_initialized == 0)\
41 for (toto = 0 ; toto < POK_CONFIG_ALLOCATOR_NB_SPACES ; toto++) \
43 pok_allocator_spaces[toto].start = 0; \
44 pok_allocator_spaces[toto].size = 0; \
45 pok_allocator_spaces[toto].allocated = 0; \
47 pok_allocator_used_spaces = 1; \
48 pok_allocator_spaces[0].start = 0; \
49 pok_allocator_spaces[0].allocated = 0; \
50 pok_allocator_spaces[0].size = POK_CONFIG_ALLOCATOR_MEMORY_SIZE;\
51 pok_allocator_initialized = 1; \
68 uint8_t pok_allocator_memspace[POK_CONFIG_ALLOCATOR_MEMORY_SIZE];
70 uint32_t pok_allocator_used_spaces = 0;
71 bool_t pok_allocator_initialized = 0;
73 #ifdef POK_NEEDS_DEBUG
74 void pok_allocator_print_spaces ()
77 printf (
"[LIBPOK] [ALLOCATOR] Used spaces = %d\n", pok_allocator_used_spaces);
78 for (space = 0 ; space < pok_allocator_used_spaces ; space++)
80 printf (
"[LIBPOK] [ALLOCATOR] Space %d start=%d size=%d allocated=%d\n", space, pok_allocator_spaces[space].start, pok_allocator_spaces[space].size, pok_allocator_spaces[space].allocated);
85 void* pok_allocator_allocate (
size_t needed_size)
87 POK_ALLOCATOR_CHECK_INIT
91 if (pok_allocator_used_spaces >= (POK_CONFIG_ALLOCATOR_NB_SPACES - 1))
93 #ifdef POK_NEEDS_DEBUG
94 printf (
"[LIBPOK] [ALLOCATOR] Not enough space\n");
99 #ifdef POK_NEEDS_DEBUG
100 printf(
"Try to take a new memory chunk, required space=%d\n", needed_size);
103 for (space = 0 ; space < pok_allocator_used_spaces ; space++)
105 #ifdef POK_NEEDS_DEBUG
106 printf (
"[LIBPOK] [ALLOCATOR] Look space %d, size %d, allocated=%d\n", space, pok_allocator_spaces[space].size, pok_allocator_spaces[space].allocated);
108 if ((pok_allocator_spaces[space].allocated == 0) && (pok_allocator_spaces[space].size >= needed_size))
110 if (pok_allocator_spaces[space].size == needed_size)
115 pok_allocator_spaces[space].allocated = 1;
117 #ifdef POK_NEEDS_DEBUG
118 printf (
"[LIBPOK] [ALLOCATOR] Allocate directly space %d\n", space);
119 pok_allocator_print_spaces ();
121 return (&pok_allocator_memspace[pok_allocator_spaces[space].start]);
128 new_space = pok_allocator_used_spaces;
129 pok_allocator_used_spaces = pok_allocator_used_spaces + 1;
131 pok_allocator_spaces[space].allocated = 1;
132 pok_allocator_spaces[new_space].allocated = 0;
134 pok_allocator_spaces[new_space].size = pok_allocator_spaces[space].size - needed_size;
135 pok_allocator_spaces[space].size = needed_size;
137 pok_allocator_spaces[new_space].start = pok_allocator_spaces[space].start + needed_size;
139 #ifdef POK_NEEDS_DEBUG
140 printf(
"[LIBPOK] [ALLOCATOR] Allocate space %d, CREATE NEW SPACE %d (size=%d)\n", space, new_space, pok_allocator_spaces[new_space].size);
141 pok_allocator_print_spaces ();
144 return (&pok_allocator_memspace[pok_allocator_spaces[space].start]);
149 #ifdef POK_NEEDS_DEBUG
150 printf (
"[LIBPOK] [ALLOCATOR] Didn't find any space for that amount of memory (%d)\n", needed_size);
158 void pok_allocator_delete_space (uint32_t space)
162 #ifdef POK_NEEDS_DEBUG
163 printf(
"[LIBPOK] [ALLOCATOR] Delete space %d\n", space);
166 for (tmp = space ; tmp < POK_CONFIG_ALLOCATOR_NB_SPACES ; tmp++)
168 pok_allocator_spaces[tmp].allocated = pok_allocator_spaces[tmp+1].allocated;
169 pok_allocator_spaces[tmp].size = pok_allocator_spaces[tmp+1].size;
170 pok_allocator_spaces[tmp].start = pok_allocator_spaces[tmp+1].start;
173 pok_allocator_used_spaces = pok_allocator_used_spaces - 1;
176 void pok_allocator_merge_space (uint32_t space)
180 if (pok_allocator_used_spaces == 1)
185 for (space2 = 0 ; space2 < pok_allocator_used_spaces ; space2++)
187 if ((space2 == space) || (pok_allocator_spaces[space2].allocated == 1))
195 if (pok_allocator_spaces[space].start == (pok_allocator_spaces[space2].start + pok_allocator_spaces[space2].size))
197 #ifdef POK_NEEDS_DEBUG
200 pok_allocator_spaces[space2].size = pok_allocator_spaces[space2].size + pok_allocator_spaces[space].size;
201 pok_allocator_delete_space (space);
202 pok_allocator_merge_space (space2);
209 if (pok_allocator_spaces[space2].start == (pok_allocator_spaces[space].start + pok_allocator_spaces[space].size))
211 #ifdef POK_NEEDS_DEBUG
214 pok_allocator_spaces[space].size = pok_allocator_spaces[space].size + pok_allocator_spaces[space2].size;
215 pok_allocator_delete_space (space2);
216 pok_allocator_merge_space (space2);
223 void pok_allocator_free (
void* ptr)
225 POK_ALLOCATOR_CHECK_INIT
229 for (space = 0 ; space < pok_allocator_used_spaces ; space++)
231 if (ptr == &pok_allocator_memspace[pok_allocator_spaces[space].start])
234 pok_allocator_spaces[space].allocated = 0;
235 pok_allocator_merge_space (space);
236 #ifdef POK_NEEDS_DEBUG
237 pok_allocator_print_spaces ();
243 #ifdef POK_NEEDS_DEBUG
244 printf(
"[LIBPOK] [ALLOCATOR] free() didn't find the space to free\n");
245 pok_allocator_print_spaces ();