Logo AND Algorithmique Numérique Distribuée

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