Logo AND Algorithmique Numérique Distribuée

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