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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator,xbt,"Mallocators");
17 * \param size size of the internal stack: number of objects the mallocator
18 * will be able to store
19 * \param new_f function to allocate a new object of your datatype, called
20 * in \a xbt_mallocator_get() when the mallocator is empty
21 * \param free_f function to free an object of your datatype, called
22 * in \a xbt_mallocator_release() when the stack is full, and when
23 * the mallocator is freed.
24 * \param reset_f function to reinitialise an object of your datatype, called
25 * when you extract an object from the mallocator
27 * Create and initialize a new mallocator for a given datatype.
29 * \return pointer to the created mallocator
30 * \see xbt_mallocator_free()
32 xbt_mallocator_t xbt_mallocator_new(int size,
34 void_f_pvoid_t free_f,
35 void_f_pvoid_t reset_f) {
40 xbt_assert0(size > 0, "size must be positive");
41 xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,"invalid parameter");
43 m = xbt_new0(s_xbt_mallocator_t, 1);
44 VERB1("Create mallocator %p",m);
45 if (XBT_LOG_ISENABLED(xbt_mallocator,xbt_log_priority_verbose))
46 xbt_backtrace_display_current();
48 m->objects = xbt_new0(void*, size);
59 * \param m the mallocator you want to destroy
61 * Destroy the mallocator and all its data. The function
62 * free_f is called on each object in the mallocator.
64 * \see xbt_mallocator_new()
66 void xbt_mallocator_free(xbt_mallocator_t m) {
69 xbt_assert0(m != NULL, "Invalid parameter");
71 VERB3("Frees mallocator %p (size:%d/%d)",m,m->current_size,m->max_size);
72 for (i = 0; i < m->current_size; i++) {
73 (*(m->free_f))(m->objects[i]);
80 * \brief Extract an object from a mallocator
81 * \param m a mallocator
83 * Remove an object from the mallocator and return it.
84 * This function is designed to be used instead of malloc().
85 * If the mallocator is not empty, an object is
86 * extracted from the mallocator and no malloc is done.
88 * If the mallocator is empty, a new object is created,
89 * by calling the function new_f().
91 * In both cases, the function reset_f() is called on the object.
93 * \see xbt_mallocator_release()
95 void *xbt_mallocator_get(xbt_mallocator_t m) {
97 xbt_assert0(m != NULL, "Invalid parameter");
99 if (m->current_size > 0) {
100 /* there is at least an available object */
101 DEBUG3("Reuse an old object for mallocator %p (size:%d/%d)",m,m->current_size,m->max_size);
102 object = m->objects[--m->current_size];
105 /* otherwise we must allocate a new object */
106 DEBUG3("Create a new object for mallocator %p (size:%d/%d)",m,m->current_size,m->max_size);
107 object = (*(m->new_f))();
109 (*(m->reset_f))(object);
113 /** \brief Push an object into a mallocator
114 * \param m a mallocator
115 * \param object an object you don't need anymore
117 * Push into the mallocator an object you don't need anymore.
118 * This function is designed to be used instead of free().
119 * If the mallocator is not full, your object if stored into
120 * the mallocator and no free is done.
121 * If the mallocator is full, the object is freed by calling
122 * the function free_f().
124 * \see xbt_mallocator_get()
126 void xbt_mallocator_release(xbt_mallocator_t m, void *object) {
127 xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
129 if (m->current_size < m->max_size) {
130 /* there is enough place to push the object */
131 DEBUG3("Store deleted object in mallocator %p for further use (size:%d/%d)",m,m->current_size,m->max_size);
132 m->objects[m->current_size++] = object;
135 /* otherwise we don't have a choice, we must free the object */
136 DEBUG3("Free deleted object: mallocator %p is full (size:%d/%d)",m,m->current_size,m->max_size);
137 (*(m->free_f))(object);