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);
66 if (!MALLOCATOR_IS_WANTED) /* Warn to avoid to commit debugging settings */
67 XBT_WARN("Mallocator is disabled!");
71 m->mutex = xbt_os_mutex_init();
76 * \param m the mallocator you want to destroy
78 * Destroy the mallocator and all its data. The function
79 * free_f is called on each object in the mallocator.
81 * \see xbt_mallocator_new()
83 void xbt_mallocator_free(xbt_mallocator_t m)
87 xbt_assert(m != NULL, "Invalid parameter");
89 XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size,
91 for (i = 0; i < m->current_size; i++) {
92 m->free_f(m->objects[i]);
95 xbt_os_mutex_destroy(m->mutex);
100 * \brief Extract an object from a mallocator
101 * \param m a mallocator
103 * Remove an object from the mallocator and return it.
104 * This function is designed to be used instead of malloc().
105 * If the mallocator is not empty, an object is
106 * extracted from the mallocator and no malloc is done.
108 * If the mallocator is empty, a new object is created,
109 * by calling the function new_f().
111 * In both cases, the function reset_f() (if defined) is called on the object.
113 * \see xbt_mallocator_release()
115 void *xbt_mallocator_get(xbt_mallocator_t m)
119 if (MALLOCATOR_IS_ENABLED) {
120 xbt_os_mutex_acquire(m->mutex);
121 if (m->current_size <= 0) {
122 /* No object is ready yet. Create a bunch of them to try to group the
123 * mallocs on the same memory pages (to help the cache lines) */
125 /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
126 /* m, m->current_size, m->max_size); */
128 int amount = MIN(m->max_size / 2, 1000);
129 for (i = 0; i < amount; i++)
130 m->objects[i] = m->new_f();
131 m->current_size = amount;
134 /* there is at least an available object, now */
135 /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
136 /* m, m->current_size, m->max_size); */
137 object = m->objects[--m->current_size];
138 xbt_os_mutex_release(m->mutex);
148 /** \brief Push an object into a mallocator
149 * \param m a mallocator
150 * \param object an object you don't need anymore
152 * Push into the mallocator an object you don't need anymore.
153 * This function is designed to be used instead of free().
154 * If the mallocator is not full, your object if stored into
155 * the mallocator and no free is done.
156 * If the mallocator is full, the object is freed by calling
157 * the function free_f().
159 * \see xbt_mallocator_get()
161 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
163 xbt_os_mutex_acquire(m->mutex);
164 if (m->current_size < m->max_size) {
165 /* there is enough place to push the object */
167 ("Store deleted object in mallocator %p for further use (size:%d/%d)",
168 m, m->current_size, m->max_size); */
169 m->objects[m->current_size++] = object;
171 /* otherwise we don't have a choice, we must free the object */
172 /* XBT_DEBUG("Free deleted object: mallocator %p is full (size:%d/%d)", m,
173 m->current_size, m->max_size); */
176 xbt_os_mutex_release(m->mutex);