Logo AND Algorithmique Numérique Distribuée

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