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) {
38 xbt_assert0(size > 0, "size must be positive");
39 xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,"invalid parameter");
41 m = xbt_new0(s_xbt_mallocator_t, 1);
43 m->objects = xbt_new0(void*, size);
54 * \param m the mallocator you want to destroy
56 * Destroy the mallocator and all its data. The function
57 * free_f is called on each object in the mallocator.
59 * \see xbt_mallocator_new()
61 void xbt_mallocator_free(xbt_mallocator_t m) {
64 xbt_assert0(m != NULL, "Invalid parameter");
67 for (i = 0; i < m->current_size; i++) {
68 m->free_f(m->objects[i]);
75 * \brief Extract an object from a mallocator
76 * \param m a mallocator
78 * Remove an object from the mallocator and return it.
79 * This function is designed to be used instead of malloc().
80 * If the mallocator is not empty, an object is
81 * extracted from the mallocator and no malloc is done.
83 * If the mallocator is empty, a new object is created,
84 * by calling the function new_f().
86 * In both cases, the function reset_f() is called on the object.
88 * \see xbt_mallocator_release()
90 void *xbt_mallocator_get(xbt_mallocator_t m) {
92 xbt_assert0(m != NULL, "Invalid parameter");
95 if (m->current_size > 0) {
96 /* there is at least an available object */
97 object = m->objects[--m->current_size];
100 /* otherwise we must allocate a new object */
107 /** \brief Push an object into a mallocator
108 * \param m a mallocator
109 * \param object an object you don't need anymore
111 * Push into the mallocator an object you don't need anymore.
112 * This function is designed to be used instead of free().
113 * If the mallocator is not full, your object if stored into
114 * the mallocator and no free is done.
115 * If the mallocator is full, the object is freed by calling
116 * the function free_f().
118 * \see xbt_mallocator_get()
120 void xbt_mallocator_release(xbt_mallocator_t m, void *object) {
121 xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
123 if (m->current_size < m->max_size) {
124 /* there is enough place to push the object */
125 m->objects[m->current_size++] = object;
128 /* otherwise we don't have a choice, we must free the object */