1 /* mallocator - recycle objects to avoid malloc() / free() */
3 /* Copyright (c) 2006-2011. 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 "mc/mc.h" /* kill mallocators when model-checking is enabled */
13 #include "mallocator_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator, xbt, "Mallocators");
18 /* Change to 0 to completely disable mallocators. */
19 #define MALLOCATOR_IS_WANTED 1
21 /* Mallocators and memory mess introduced by model-checking do not mix well
22 * together: the mallocator will give standard memory when we are using raw
23 * memory (so these blocks are killed on restore) and the contrary (so these
24 * blocks will leak accross restores).
26 #define MALLOCATOR_IS_ENABLED (MALLOCATOR_IS_WANTED && !MC_IS_ENABLED)
30 * \param size size of the internal stack: number of objects the mallocator
31 * will be able to store
32 * \param new_f function to allocate a new object of your datatype, called
33 * in \a xbt_mallocator_get() when the mallocator is empty
34 * \param free_f function to free an object of your datatype, called
35 * in \a xbt_mallocator_release() when the stack is full, and when
36 * the mallocator is freed.
37 * \param reset_f function to reinitialise an object of your datatype, called
38 * when you extract an object from the mallocator (can be NULL)
40 * Create and initialize a new mallocator for a given datatype.
42 * \return pointer to the created mallocator
43 * \see xbt_mallocator_free()
45 xbt_mallocator_t xbt_mallocator_new(int size,
47 void_f_pvoid_t free_f,
48 void_f_pvoid_t reset_f)
52 xbt_assert(size > 0, "size must be positive");
53 xbt_assert(new_f != NULL && free_f != NULL, "invalid parameter");
55 m = xbt_new0(s_xbt_mallocator_t, 1);
56 XBT_VERB("Create mallocator %p", m);
62 if (MALLOCATOR_IS_ENABLED) {
63 m->objects = xbt_new0(void *, size);
65 m->mutex = xbt_os_mutex_init();
67 if (!MALLOCATOR_IS_WANTED) /* Warn to avoid to commit debugging settings */
68 XBT_WARN("Mallocator is disabled!");
77 * \param m the mallocator you want to destroy
79 * Destroy the mallocator and all its data. The function
80 * free_f is called on each object in the mallocator.
82 * \see xbt_mallocator_new()
84 void xbt_mallocator_free(xbt_mallocator_t m)
88 xbt_assert(m != NULL, "Invalid parameter");
90 XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size,
92 for (i = 0; i < m->current_size; i++) {
93 m->free_f(m->objects[i]);
96 xbt_os_mutex_destroy(m->mutex);
101 * \brief Extract an object from a mallocator
102 * \param m a mallocator
104 * Remove an object from the mallocator and return it.
105 * This function is designed to be used instead of malloc().
106 * If the mallocator is not empty, an object is
107 * extracted from the mallocator and no malloc is done.
109 * If the mallocator is empty, a new object is created,
110 * by calling the function new_f().
112 * In both cases, the function reset_f() (if defined) is called on the object.
114 * \see xbt_mallocator_release()
116 void *xbt_mallocator_get(xbt_mallocator_t m)
120 if (MALLOCATOR_IS_ENABLED) {
121 xbt_os_mutex_acquire(m->mutex);
122 if (m->current_size <= 0) {
123 /* No object is ready yet. Create a bunch of them to try to group the
124 * mallocs on the same memory pages (to help the cache lines) */
126 /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
127 /* m, m->current_size, m->max_size); */
129 int amount = MIN(m->max_size / 2, 1000);
130 for (i = 0; i < amount; i++)
131 m->objects[i] = m->new_f();
132 m->current_size = amount;
135 /* there is at least an available object, now */
136 /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
137 /* m, m->current_size, m->max_size); */
138 object = m->objects[--m->current_size];
139 xbt_os_mutex_release(m->mutex);
149 /** \brief Push an object into a mallocator
150 * \param m a mallocator
151 * \param object an object you don't need anymore
153 * Push into the mallocator an object you don't need anymore.
154 * This function is designed to be used instead of free().
155 * If the mallocator is not full, your object if stored into
156 * the mallocator and no free is done.
157 * If the mallocator is full, the object is freed by calling
158 * the function free_f().
160 * \see xbt_mallocator_get()
162 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
164 if (MALLOCATOR_IS_ENABLED) {
165 xbt_os_mutex_acquire(m->mutex);
166 if (m->current_size < m->max_size) {
167 /* there is enough place to push the object */
169 ("Store deleted object in mallocator %p for further use (size:%d/%d)",
170 m, m->current_size, m->max_size); */
171 m->objects[m->current_size++] = object;
172 xbt_os_mutex_release(m->mutex);
174 xbt_os_mutex_release(m->mutex);
175 /* otherwise we don't have a choice, we must free the object */
176 /* XBT_DEBUG("Free deleted object: mallocator %p is full (size:%d/%d)", m,
177 m->current_size, m->max_size); */