Logo AND Algorithmique Numérique Distribuée

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