X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/421598739fa7167a9c4fac25b1038630f8b7fcf3..456ae098e6f673b73c4757ff1a7bfd459e166c3c:/src/xbt/mallocator.c diff --git a/src/xbt/mallocator.c b/src/xbt/mallocator.c index ff9557f0c2..b6784661e1 100644 --- a/src/xbt/mallocator.c +++ b/src/xbt/mallocator.c @@ -1,6 +1,6 @@ -/* dict - a generic dictionnary, variation over the B-tree concept */ +/* mallocator - recycle objects to avoid malloc() / free() */ -/* Copyright (c) 2006 Christophe Thiery. All rights reserved. */ +/* Copyright (c) 2006 Christophe Thiery. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -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,9 +59,11 @@ 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]); } @@ -82,9 +88,10 @@ 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];