Logo AND Algorithmique Numérique Distribuée

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