X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/584cf83039502064235bfe4cd62730835ebfeccf..f3507930c130404d05f982cf9fe960fb95f7eb4b:/src/xbt/mallocator.c diff --git a/src/xbt/mallocator.c b/src/xbt/mallocator.c index 01f303f307..fcf4bc2d4e 100644 --- a/src/xbt/mallocator.c +++ b/src/xbt/mallocator.c @@ -31,10 +31,14 @@ xbt_mallocator_t xbt_mallocator_new(int size, pvoid_f_void_t new_f, void_f_pvoid_t free_f, void_f_pvoid_t reset_f) { + + + xbt_mallocator_t m; + xbt_assert0(size > 0, "size must be positive"); - xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL, - "invalid parameter"); - xbt_mallocator_t m = xbt_new0(s_xbt_mallocator_t, 1); + xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,"invalid parameter"); + + m = xbt_new0(s_xbt_mallocator_t, 1); m->objects = xbt_new0(void*, size); m->max_size = size; @@ -55,11 +59,13 @@ xbt_mallocator_t xbt_mallocator_new(int size, * \see xbt_mallocator_new() */ void xbt_mallocator_free(xbt_mallocator_t m) { - xbt_assert0(m != NULL, "Invalid parameter"); int i; + xbt_assert0(m != NULL, "Invalid parameter"); + + for (i = 0; i < m->current_size; i++) { - m->free_f(m->objects[i]); + (*(m->free_f))(m->objects[i]); } xbt_free(m->objects); xbt_free(m); @@ -82,18 +88,19 @@ void xbt_mallocator_free(xbt_mallocator_t m) { * \see xbt_mallocator_release() */ void *xbt_mallocator_get(xbt_mallocator_t m) { + void *object; xbt_assert0(m != NULL, "Invalid parameter"); - void *object; + if (m->current_size > 0) { /* there is at least an available object */ object = m->objects[--m->current_size]; } else { /* otherwise we must allocate a new object */ - object = m->new_f(); + object = (*(m->new_f))(); } - m->reset_f(object); + (*(m->reset_f))(object); return object; } @@ -119,6 +126,6 @@ void xbt_mallocator_release(xbt_mallocator_t m, void *object) { } else { /* otherwise we don't have a choice, we must free the object */ - m->free_f(object); + (*(m->free_f))(object); } }