-/* 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. */
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;
* \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]);
}
* \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];