Logo AND Algorithmique Numérique Distribuée

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