Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
469c43596a0cb55fcb93a887b6e1c4d0465d9a39
[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 /** Implementation note on the mallocators:
18  *
19  * Mallocators and memory mess introduced by model-checking do not mix well
20  * together: the mallocator will give standard memory when we are using raw
21  * memory (so these blocks are killed on restore) and the contrary (so these
22  * blocks will leak across restores).
23  *
24  * In addition, model-checking is activated when the command-line arguments
25  * are parsed, at the beginning of main, while most of the mallocators are
26  * created during the constructor functions launched from xbt_preinit, before
27  * the beginning of the main function.
28  *
29  * We want the code as fast as possible when they are active while we can deal
30  * with a little slow-down when they are inactive. So we start the mallocators
31  * as inactive. When they are so, they check at each use whether they should
32  * switch to the fast active mode or should stay in inactive mode.
33  * Finally, we give external elements a way to switch them
34  * all to the active mode (through xbt_mallocator_initialization_is_done).
35  *
36  * This design avoids to store all mallocators somewhere for later conversion,
37  * which would be hard to achieve provided that all our data structures use
38  * some mallocators internally...
39  */
40
41 /* Value != 0 when the framework configuration is done.  Value > 1 if the
42  * mallocators should be protected from concurrent accesses.  */
43 static int initialization_done = 0;
44
45 static XBT_INLINE void lock_create(xbt_mallocator_t m)
46 {
47   m->mutex = initialization_done > 1 ? xbt_os_mutex_init() : NULL;
48 }
49
50 static XBT_INLINE void lock_destroy(xbt_mallocator_t m)
51 {
52   if (m->mutex)
53     xbt_os_mutex_destroy(m->mutex);
54 }
55
56 static XBT_INLINE void lock_acquire(xbt_mallocator_t m)
57 {
58   if (m->mutex)
59     xbt_os_mutex_acquire(m->mutex);
60 }
61
62 static XBT_INLINE void lock_release(xbt_mallocator_t m)
63 {
64   if (m->mutex)
65     xbt_os_mutex_release(m->mutex);
66 }
67
68 /**
69  * This function must be called once the framework configuration is done. If not,
70  * mallocators will never get used. Check the implementation notes in
71  * src/xbt/mallocator.c for the justification of this.
72  *
73  * For example, surf_config uses this function to tell to the mallocators that
74  * the simgrid
75  * configuration is now finished and that it can create them if not done yet */
76 void xbt_mallocator_initialization_is_done(int protect)
77 {
78   initialization_done = protect ? 2 : 1;
79 }
80
81 /** used by the module to know if it's time to activate the mallocators yet */
82 static XBT_INLINE int xbt_mallocator_is_active(void) {
83 #if MALLOCATOR_COMPILED_IN
84   return initialization_done && !MC_is_active();
85 #else
86   return 0;
87 #endif
88 }
89
90 /**
91  * \brief Constructor
92  * \param size size of the internal stack: number of objects the mallocator
93  * will be able to store
94  * \param new_f function to allocate a new object of your datatype, called
95  * in \a xbt_mallocator_get() when the mallocator is empty
96  * \param free_f function to free an object of your datatype, called
97  * in \a xbt_mallocator_release() when the stack is full, and when
98  * the mallocator is freed.
99  * \param reset_f function to reinitialise an object of your datatype, called
100  * when you extract an object from the mallocator (can be NULL)
101  *
102  * Create and initialize a new mallocator for a given datatype.
103  *
104  * \return pointer to the created mallocator
105  * \see xbt_mallocator_free()
106  */
107 xbt_mallocator_t xbt_mallocator_new(int size,
108                                     pvoid_f_void_t new_f,
109                                     void_f_pvoid_t free_f,
110                                     void_f_pvoid_t reset_f)
111 {
112   xbt_mallocator_t m;
113
114   xbt_assert(size > 0, "size must be positive");
115   xbt_assert(new_f != NULL && free_f != NULL, "invalid parameter");
116
117   m = xbt_new0(s_xbt_mallocator_t, 1);
118   XBT_VERB("Create mallocator %p (%s)",
119            m, xbt_mallocator_is_active() ? "enabled" : "disabled");
120   m->current_size = 0;
121   m->new_f = new_f;
122   m->free_f = free_f;
123   m->reset_f = reset_f;
124   m->max_size = size;
125
126   return m;
127 }
128
129 /** \brief Destructor
130  * \param m the mallocator you want to destroy
131  *
132  * Destroy the mallocator and all its data. The function
133  * free_f is called on each object in the mallocator.
134  *
135  * \see xbt_mallocator_new()
136  */
137 void xbt_mallocator_free(xbt_mallocator_t m)
138 {
139
140   int i;
141   xbt_assert(m != NULL, "Invalid parameter");
142
143   XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size,
144         m->max_size);
145   for (i = 0; i < m->current_size; i++) {
146     m->free_f(m->objects[i]);
147   }
148   xbt_free(m->objects);
149   lock_destroy(m);
150   xbt_free(m);
151 }
152
153 /**
154  * \brief Extract an object from a mallocator
155  * \param m a mallocator
156  *
157  * Remove an object from the mallocator and return it.
158  * This function is designed to be used instead of malloc().
159  * If the mallocator is not empty, an object is
160  * extracted from the mallocator and no malloc is done.
161  *
162  * If the mallocator is empty, a new object is created,
163  * by calling the function new_f().
164  *
165  * In both cases, the function reset_f() (if defined) is called on the object.
166  *
167  * \see xbt_mallocator_release()
168  */
169 void *xbt_mallocator_get(xbt_mallocator_t m)
170 {
171   void *object;
172
173   if (m->objects != NULL) { // this mallocator is active, stop thinking and go for it!
174     lock_acquire(m);
175     if (m->current_size <= 0) {
176       /* No object is ready yet. Create a bunch of them to try to group the
177        * mallocs on the same memory pages (to help the cache lines) */
178
179       /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
180       /*           m, m->current_size, m->max_size); */
181       int i;
182       int amount = MIN(m->max_size / 2, 1000);
183       for (i = 0; i < amount; i++)
184         m->objects[i] = m->new_f();
185       m->current_size = amount;
186     }
187
188     /* there is at least an available object, now */
189     /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
190     /*           m, m->current_size, m->max_size); */
191     object = m->objects[--m->current_size];
192     lock_release(m);
193   } else {
194     if (xbt_mallocator_is_active()) {
195       // We have to switch this mallocator from inactive to active (and then get an object)
196       m->objects = xbt_new0(void *, m->max_size);
197       lock_create(m);
198       return xbt_mallocator_get(m);
199     } else {
200       object = m->new_f();
201     }
202   }
203
204   if (m->reset_f)
205     m->reset_f(object);
206   return object;
207 }
208
209 /** \brief Push an object into a mallocator
210  * \param m a mallocator
211  * \param object an object you don't need anymore
212  *
213  * Push into the mallocator an object you don't need anymore.
214  * This function is designed to be used instead of free().
215  * If the mallocator is not full, your object if stored into
216  * the mallocator and no free is done.
217  * If the mallocator is full, the object is freed by calling
218  * the function free_f().
219  *
220  * \see xbt_mallocator_get()
221  */
222 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
223 {
224   if (m->objects != NULL) { // Go for it
225     lock_acquire(m);
226     if (m->current_size < m->max_size) {
227       /* there is enough place to push the object */
228       /* XBT_DEBUG
229          ("Store deleted object in mallocator %p for further use (size:%d/%d)",
230          m, m->current_size, m->max_size); */
231       m->objects[m->current_size++] = object;
232       lock_release(m);
233     } else {
234       lock_release(m);
235       /* otherwise we don't have a choice, we must free the object */
236       /* XBT_DEBUG("Free deleted object: mallocator %p is full (size:%d/%d)", m,
237          m->current_size, m->max_size); */
238       m->free_f(object);
239     }
240   } else {
241     if (xbt_mallocator_is_active()) {
242       // We have to switch this mallocator from inactive to active (and then store that object)
243       m->objects = xbt_new0(void *, m->max_size);
244       lock_create(m);
245       xbt_mallocator_release(m,object);
246     } else {
247       m->free_f(object);
248     }
249   }
250 }