1 /* mallocator - recycle objects to avoid malloc() / free() */
3 /* Copyright (c) 2006 Christophe Thiery. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/mallocator.h"
9 #include "xbt/asserts.h"
10 #include "xbt/sysdep.h"
11 #include "mallocator_private.h"
15 * \param size size of the internal stack: number of objects the mallocator
16 * will be able to store
17 * \param new_f function to allocate a new object of your datatype, called
18 * in \a xbt_mallocator_get() when the mallocator is empty
19 * \param free_f function to free an object of your datatype, called
20 * in \a xbt_mallocator_release() when the stack is full, and when
21 * the mallocator is freed.
22 * \param reset_f function to reinitialise an object of your datatype, called
23 * when you extract an object from the mallocator
25 * Create and initialize a new mallocator for a given datatype.
27 * \return pointer to the created mallocator
28 * \see xbt_mallocator_free()
30 xbt_mallocator_t xbt_mallocator_new(int size,
32 void_f_pvoid_t free_f,
33 void_f_pvoid_t reset_f) {
34 xbt_assert0(size > 0, "size must be positive");
35 xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,
37 xbt_mallocator_t m = xbt_new0(s_xbt_mallocator_t, 1);
39 m->objects = xbt_new0(void*, size);
50 * \param m the mallocator you want to destroy
52 * Destroy the mallocator and all its data. The function
53 * free_f is called on each object in the mallocator.
55 * \see xbt_mallocator_new()
57 void xbt_mallocator_free(xbt_mallocator_t m) {
58 xbt_assert0(m != NULL, "Invalid parameter");
61 for (i = 0; i < m->current_size; i++) {
62 m->free_f(m->objects[i]);
69 * \brief Extract an object from a mallocator
70 * \param m a mallocator
72 * Remove an object from the mallocator and return it.
73 * This function is designed to be used instead of malloc().
74 * If the mallocator is not empty, an object is
75 * extracted from the mallocator and no malloc is done.
77 * If the mallocator is empty, a new object is created,
78 * by calling the function new_f().
80 * In both cases, the function reset_f() is called on the object.
82 * \see xbt_mallocator_release()
84 void *xbt_mallocator_get(xbt_mallocator_t m) {
85 xbt_assert0(m != NULL, "Invalid parameter");
88 if (m->current_size > 0) {
89 /* there is at least an available object */
90 object = m->objects[--m->current_size];
93 /* otherwise we must allocate a new object */
100 /** \brief Push an object into a mallocator
101 * \param m a mallocator
102 * \param object an object you don't need anymore
104 * Push into the mallocator an object you don't need anymore.
105 * This function is designed to be used instead of free().
106 * If the mallocator is not full, your object if stored into
107 * the mallocator and no free is done.
108 * If the mallocator is full, the object is freed by calling
109 * the function free_f().
111 * \see xbt_mallocator_get()
113 void xbt_mallocator_release(xbt_mallocator_t m, void *object) {
114 xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
116 if (m->current_size < m->max_size) {
117 /* there is enough place to push the object */
118 m->objects[m->current_size++] = object;
121 /* otherwise we don't have a choice, we must free the object */