Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simply say fun_ptr(...) instead of (*fun_ptr)(...).
[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
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
54               && reset_f != NULL, "invalid parameter");
55
56   m = xbt_new0(s_xbt_mallocator_t, 1);
57   XBT_VERB("Create mallocator %p", m);
58   m->current_size = 0;
59   m->new_f = new_f;
60   m->free_f = free_f;
61   m->reset_f = reset_f;
62
63   if (MALLOCATOR_IS_ENABLED) {
64     m->objects = xbt_new0(void *, size);
65     m->max_size = size;
66   } else {
67     if (!MALLOCATOR_IS_WANTED) /* Warn to avoid to commit debugging settings */
68       XBT_WARN("Mallocator is disabled!");
69     m->objects = NULL;
70     m->max_size = 0;
71   }
72
73   return m;
74 }
75
76 /** \brief Destructor
77  * \param m the mallocator you want to destroy
78  *
79  * Destroy the mallocator and all its data. The function
80  * free_f is called on each object in the mallocator.
81  *
82  * \see xbt_mallocator_new()
83  */
84 void xbt_mallocator_free(xbt_mallocator_t m)
85 {
86
87   int i;
88   xbt_assert(m != NULL, "Invalid parameter");
89
90   XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size,
91         m->max_size);
92   for (i = 0; i < m->current_size; i++) {
93     m->free_f(m->objects[i]);
94   }
95   xbt_free(m->objects);
96   xbt_free(m);
97 }
98
99 /**
100  * \brief Extract an object from a mallocator
101  * \param m a mallocator
102  *
103  * Remove an object from the mallocator and return it.
104  * This function is designed to be used instead of malloc().
105  * If the mallocator is not empty, an object is
106  * extracted from the mallocator and no malloc is done.
107  *
108  * If the mallocator is empty, a new object is created,
109  * by calling the function new_f().
110  *
111  * In both cases, the function reset_f() is called on the object.
112  *
113  * \see xbt_mallocator_release()
114  */
115 void *xbt_mallocator_get(xbt_mallocator_t m)
116 {
117   void *object;
118
119   if (MALLOCATOR_IS_ENABLED) {
120     if (m->current_size <= 0) {
121       /* No object is ready yet. Create a bunch of them to try to group the
122        * mallocs on the same memory pages (to help the cache lines) */
123
124       /* XBT_DEBUG("Create a new object for mallocator %p (size:%d/%d)", */
125       /*           m, m->current_size, m->max_size); */
126       int i;
127       int amount = MIN(m->max_size / 2, 1000);
128       for (i = 0; i < amount; i++)
129         m->objects[i] = m->new_f();
130       m->current_size = amount;
131     }
132
133     /* there is at least an available object, now */
134     /* XBT_DEBUG("Reuse an old object for mallocator %p (size:%d/%d)", */
135     /*           m, m->current_size, m->max_size); */
136     object = m->objects[--m->current_size];
137   } else {
138     object = m->new_f();
139   }
140
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 }