Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : get pointer address for bytes detected as different in heap compariso...
[simgrid.git] / src / xbt / mallocator.c
1 /* mallocator - recycle objects to avoid malloc() / free()                  */
2
3 /* Copyright (c) 2006-2011. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
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"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator, xbt, "Mallocators");
16
17
18 /* Change to 0 to completely disable mallocators. */
19 #define MALLOCATOR_IS_WANTED 1
20
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).
25  */
26 #define MALLOCATOR_IS_ENABLED (MALLOCATOR_IS_WANTED && !MC_IS_ENABLED)
27
28 /**
29  * \brief Constructor
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)
39  *
40  * Create and initialize a new mallocator for a given datatype.
41  *
42  * \return pointer to the created mallocator
43  * \see xbt_mallocator_free()
44  */
45 xbt_mallocator_t xbt_mallocator_new(int size,
46                                     pvoid_f_void_t new_f,
47                                     void_f_pvoid_t free_f,
48                                     void_f_pvoid_t reset_f)
49 {
50   xbt_mallocator_t m;
51
52   xbt_assert(size > 0, "size must be positive");
53   xbt_assert(new_f != NULL && free_f != NULL, "invalid parameter");
54
55   m = xbt_new0(s_xbt_mallocator_t, 1);
56   XBT_VERB("Create mallocator %p", m);
57   m->current_size = 0;
58   m->new_f = new_f;
59   m->free_f = free_f;
60   m->reset_f = reset_f;
61
62   if (MALLOCATOR_IS_ENABLED) {
63     m->objects = xbt_new0(void *, size);
64     m->max_size = size;
65   } else {
66     if (!MALLOCATOR_IS_WANTED) /* Warn to avoid to commit debugging settings */
67       XBT_WARN("Mallocator is disabled!");
68     m->objects = NULL;
69     m->max_size = 0;
70   }
71   m->mutex = xbt_os_mutex_init();
72   return m;
73 }
74
75 /** \brief Destructor
76  * \param m the mallocator you want to destroy
77  *
78  * Destroy the mallocator and all its data. The function
79  * free_f is called on each object in the mallocator.
80  *
81  * \see xbt_mallocator_new()
82  */
83 void xbt_mallocator_free(xbt_mallocator_t m)
84 {
85
86   int i;
87   xbt_assert(m != NULL, "Invalid parameter");
88
89   XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size,
90         m->max_size);
91   for (i = 0; i < m->current_size; i++) {
92     m->free_f(m->objects[i]);
93   }
94   xbt_free(m->objects);
95   xbt_os_mutex_destroy(m->mutex);
96   xbt_free(m);
97 }
98
99 /**
100  * \brief Extract an object from a mallocator
101  * \param m a mallocator
102  *
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.
107  *
108  * If the mallocator is empty, a new object is created,
109  * by calling the function new_f().
110  *
111  * In both cases, the function reset_f() (if defined) is called on the object.
112  *
113  * \see xbt_mallocator_release()
114  */
115 void *xbt_mallocator_get(xbt_mallocator_t m)
116 {
117   void *object;
118
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) */
124
125       /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
126       /*           m, m->current_size, m->max_size); */
127       int i;
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;
132     }
133
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);
139   } else {
140     object = m->new_f();
141   }
142
143   if (m->reset_f)
144     m->reset_f(object);
145   return object;
146 }
147
148 /** \brief Push an object into a mallocator
149  * \param m a mallocator
150  * \param object an object you don't need anymore
151  *
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().
158  *
159  * \see xbt_mallocator_get()
160  */
161 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
162 {
163   xbt_os_mutex_acquire(m->mutex);
164   if (m->current_size < m->max_size) {
165     /* there is enough place to push the object */
166     /* XBT_DEBUG
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;
170   } else {
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); */
174     m->free_f(object);
175   }
176   xbt_os_mutex_release(m->mutex);
177 }