Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into MC_LTL
[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
18 /* Change to 0 to completely disable mallocators. */
19 #define MALLOCATOR_IS_WANTED 1
20
21 /* Mallocators and memory mess introduced by model-checking do not mix well
22  * together: the mallocator will give standard memory when we are using raw
23  * memory (so these blocks are killed on restore) and the contrary (so these
24  * blocks will leak accross restores).
25  */
26 #define MALLOCATOR_IS_ENABLED (MALLOCATOR_IS_WANTED && !MC_IS_ENABLED)
27
28 /**
29  * \brief Constructor
30  * \param size size of the internal stack: number of objects the mallocator
31  * will be able to store
32  * \param new_f function to allocate a new object of your datatype, called
33  * in \a xbt_mallocator_get() when the mallocator is empty
34  * \param free_f function to free an object of your datatype, called
35  * in \a xbt_mallocator_release() when the stack is full, and when
36  * the mallocator is freed.
37  * \param reset_f function to reinitialise an object of your datatype, called
38  * when you extract an object from the mallocator (can be NULL)
39  *
40  * Create and initialize a new mallocator for a given datatype.
41  *
42  * \return pointer to the created mallocator
43  * \see xbt_mallocator_free()
44  */
45 xbt_mallocator_t xbt_mallocator_new(int size,
46                                     pvoid_f_void_t new_f,
47                                     void_f_pvoid_t free_f,
48                                     void_f_pvoid_t reset_f)
49 {
50   xbt_mallocator_t m;
51
52   xbt_assert(size > 0, "size must be positive");
53   xbt_assert(new_f != NULL && free_f != NULL, "invalid parameter");
54
55   m = xbt_new0(s_xbt_mallocator_t, 1);
56   XBT_VERB("Create mallocator %p", m);
57   m->current_size = 0;
58   m->new_f = new_f;
59   m->free_f = free_f;
60   m->reset_f = reset_f;
61
62   if (MALLOCATOR_IS_ENABLED) {
63     m->objects = xbt_new0(void *, size);
64     m->max_size = size;
65   } else {
66     if (!MALLOCATOR_IS_WANTED) /* Warn to avoid to commit debugging settings */
67       XBT_WARN("Mallocator is disabled!");
68     m->objects = NULL;
69     m->max_size = 0;
70   }
71
72   return m;
73 }
74
75 /** \brief Destructor
76  * \param m the mallocator you want to destroy
77  *
78  * Destroy the mallocator and all its data. The function
79  * free_f is called on each object in the mallocator.
80  *
81  * \see xbt_mallocator_new()
82  */
83 void xbt_mallocator_free(xbt_mallocator_t m)
84 {
85
86   int i;
87   xbt_assert(m != NULL, "Invalid parameter");
88
89   XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size,
90         m->max_size);
91   for (i = 0; i < m->current_size; i++) {
92     m->free_f(m->objects[i]);
93   }
94   xbt_free(m->objects);
95   xbt_free(m);
96 }
97
98 /**
99  * \brief Extract an object from a mallocator
100  * \param m a mallocator
101  *
102  * Remove an object from the mallocator and return it.
103  * This function is designed to be used instead of malloc().
104  * If the mallocator is not empty, an object is
105  * extracted from the mallocator and no malloc is done.
106  *
107  * If the mallocator is empty, a new object is created,
108  * by calling the function new_f().
109  *
110  * In both cases, the function reset_f() (if defined) is called on the object.
111  *
112  * \see xbt_mallocator_release()
113  */
114 void *xbt_mallocator_get(xbt_mallocator_t m)
115 {
116   void *object;
117
118   if (MALLOCATOR_IS_ENABLED) {
119     if (m->current_size <= 0) {
120       /* No object is ready yet. Create a bunch of them to try to group the
121        * mallocs on the same memory pages (to help the cache lines) */
122
123       /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
124       /*           m, m->current_size, m->max_size); */
125       int i;
126       int amount = MIN(m->max_size / 2, 1000);
127       for (i = 0; i < amount; i++)
128         m->objects[i] = m->new_f();
129       m->current_size = amount;
130     }
131
132     /* there is at least an available object, now */
133     /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
134     /*           m, m->current_size, m->max_size); */
135     object = m->objects[--m->current_size];
136   } else {
137     object = m->new_f();
138   }
139
140   if (m->reset_f)
141     m->reset_f(object);
142   return object;
143 }
144
145 /** \brief Push an object into a mallocator
146  * \param m a mallocator
147  * \param object an object you don't need anymore
148  *
149  * Push into the mallocator an object you don't need anymore.
150  * This function is designed to be used instead of free().
151  * If the mallocator is not full, your object if stored into
152  * the mallocator and no free is done.
153  * If the mallocator is full, the object is freed by calling
154  * the function free_f().
155  *
156  * \see xbt_mallocator_get()
157  */
158 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
159 {
160   if (m->current_size < m->max_size) {
161     /* there is enough place to push the object */
162     /* XBT_DEBUG
163         ("Store deleted object in mallocator %p for further use (size:%d/%d)",
164          m, m->current_size, m->max_size); */
165     m->objects[m->current_size++] = object;
166   } else {
167     /* otherwise we don't have a choice, we must free the object */
168     /* XBT_DEBUG("Free deleted object: mallocator %p is full (size:%d/%d)", m,
169            m->current_size, m->max_size); */
170     m->free_f(object);
171   }
172 }