1 #include "xbt/mallocator.h"
2 #include "xbt/asserts.h"
3 #include "xbt/sysdep.h"
4 #include "mallocator_private.h"
6 xbt_mallocator_t xbt_mallocator_new(int size,
9 void_f_pvoid_t reset_f) {
10 xbt_assert0(size > 0, "size must be positive");
11 xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,
13 xbt_mallocator_t m = xbt_new0(s_xbt_mallocator_t, 1);
15 m->objects = xbt_new0(void*, size);
25 /* Destroy the mallocator and all its data */
26 void xbt_mallocator_free(xbt_mallocator_t m) {
27 xbt_assert0(m != NULL, "Invalid parameter");
30 for (i = 0; i < m->current_size; i++) {
31 m->free_f(m->objects[i]);
37 /* Return an object (use this function instead of malloc) */
38 void *xbt_mallocator_get(xbt_mallocator_t m) {
39 xbt_assert0(m != NULL, "Invalid parameter");
42 if (m->current_size > 0) {
43 /* there is at least an available object */
44 object = m->objects[--m->current_size];
47 /* otherwise we must allocate a new object */
54 /* Release an object (use this function instead of free) */
55 void xbt_mallocator_release(xbt_mallocator_t m, void *object) {
56 xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
58 if (m->current_size < m->max_size) {
59 /* there is enough place to push the object */
60 m->objects[m->current_size++] = object;
63 /* otherwise we don't have a choice, we must free the object */