1 /* mallocator - recycle objects to avoid malloc() / free() */
3 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/mallocator.h"
10 #include "xbt/asserts.h"
11 #include "xbt/sysdep.h"
12 #include "mallocator_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator, xbt, "Mallocators");
15 extern int _surf_do_model_check; /* kill mallocators when this is true */
19 * \param size size of the internal stack: number of objects the mallocator
20 * will be able to store
21 * \param new_f function to allocate a new object of your datatype, called
22 * in \a xbt_mallocator_get() when the mallocator is empty
23 * \param free_f function to free an object of your datatype, called
24 * in \a xbt_mallocator_release() when the stack is full, and when
25 * the mallocator is freed.
26 * \param reset_f function to reinitialise an object of your datatype, called
27 * when you extract an object from the mallocator
29 * Create and initialize a new mallocator for a given datatype.
31 * \return pointer to the created mallocator
32 * \see xbt_mallocator_free()
34 xbt_mallocator_t xbt_mallocator_new(int size,
36 void_f_pvoid_t free_f,
37 void_f_pvoid_t reset_f)
43 xbt_assert0(size > 0, "size must be positive");
44 xbt_assert0(new_f != NULL && free_f != NULL
45 && reset_f != NULL, "invalid parameter");
47 /* Let's force 0 size mallocator! (Dirty hack, blame Martin :) )*/
49 /* mallocators and memory mess introduced by model-checking do not mix well together:
50 * The mallocator will give standard memory when we are using raw memory (so these blocks are killed on restore)
51 * and the contrary (so these blocks will leak accross restores)
53 if (_surf_do_model_check)
56 m = xbt_new0(s_xbt_mallocator_t, 1);
57 VERB1("Create mallocator %p", m);
58 if (XBT_LOG_ISENABLED(xbt_mallocator, xbt_log_priority_verbose))
59 xbt_backtrace_display_current();
61 m->objects = xbt_new0(void *, _surf_do_model_check?1:size);
72 * \param m the mallocator you want to destroy
74 * Destroy the mallocator and all its data. The function
75 * free_f is called on each object in the mallocator.
77 * \see xbt_mallocator_new()
79 void xbt_mallocator_free(xbt_mallocator_t m)
83 xbt_assert0(m != NULL, "Invalid parameter");
85 VERB3("Frees mallocator %p (size:%d/%d)", m, m->current_size, m->max_size);
86 for (i = 0; i < m->current_size; i++) {
87 (*(m->free_f)) (m->objects[i]);
94 * \brief Extract an object from a mallocator
95 * \param m a mallocator
97 * Remove an object from the mallocator and return it.
98 * This function is designed to be used instead of malloc().
99 * If the mallocator is not empty, an object is
100 * extracted from the mallocator and no malloc is done.
102 * If the mallocator is empty, a new object is created,
103 * by calling the function new_f().
105 * In both cases, the function reset_f() is called on the object.
107 * \see xbt_mallocator_release()
109 void *xbt_mallocator_get(xbt_mallocator_t m)
112 xbt_assert0(m != NULL, "Invalid parameter");
114 if (m->current_size > 0) {
115 /* there is at least an available object */
116 DEBUG3("Reuse an old object for mallocator %p (size:%d/%d)", m,
117 m->current_size, m->max_size);
118 object = m->objects[--m->current_size];
120 /* otherwise we must allocate a new object */
121 DEBUG3("Create a new object for mallocator %p (size:%d/%d)", m,
122 m->current_size, m->max_size);
123 object = (*(m->new_f)) ();
125 (*(m->reset_f)) (object);
129 /** \brief Push an object into a mallocator
130 * \param m a mallocator
131 * \param object an object you don't need anymore
133 * Push into the mallocator an object you don't need anymore.
134 * This function is designed to be used instead of free().
135 * If the mallocator is not full, your object if stored into
136 * the mallocator and no free is done.
137 * If the mallocator is full, the object is freed by calling
138 * the function free_f().
140 * \see xbt_mallocator_get()
142 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
144 xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
146 if (m->current_size < m->max_size) {
147 /* there is enough place to push the object */
149 ("Store deleted object in mallocator %p for further use (size:%d/%d)",
150 m, m->current_size, m->max_size);
151 m->objects[m->current_size++] = object;
153 /* otherwise we don't have a choice, we must free the object */
154 DEBUG3("Free deleted object: mallocator %p is full (size:%d/%d)", m,
155 m->current_size, m->max_size);
156 (*(m->free_f)) (object);