Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: Change the reference speed to a command line option
[simgrid.git] / src / xbt / mallocator.c
1 /* mallocator - recycle objects to avoid malloc() / free()                  */
2
3 /* Copyright (c) 2006 Christophe Thiery. All rights reserved.               */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/mallocator.h"
9 #include "xbt/asserts.h"
10 #include "xbt/sysdep.h"
11 #include "mallocator_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator, xbt, "Mallocators");
14
15 /**
16  * \brief Constructor
17  * \param size size of the internal stack: number of objects the mallocator
18  * will be able to store
19  * \param new_f function to allocate a new object of your datatype, called
20  * in \a xbt_mallocator_get() when the mallocator is empty
21  * \param free_f function to free an object of your datatype, called
22  * in \a xbt_mallocator_release() when the stack is full, and when
23  * the mallocator is freed.
24  * \param reset_f function to reinitialise an object of your datatype, called
25  * when you extract an object from the mallocator
26  *
27  * Create and initialize a new mallocator for a given datatype.
28  *
29  * \return pointer to the created mallocator
30  * \see xbt_mallocator_free()
31  */
32 xbt_mallocator_t xbt_mallocator_new(int size,
33                                     pvoid_f_void_t new_f,
34                                     void_f_pvoid_t free_f,
35                                     void_f_pvoid_t reset_f)
36 {
37
38
39   xbt_mallocator_t m;
40
41   xbt_assert0(size > 0, "size must be positive");
42   xbt_assert0(new_f != NULL && free_f != NULL
43               && reset_f != NULL, "invalid parameter");
44
45   m = xbt_new0(s_xbt_mallocator_t, 1);
46   VERB1("Create mallocator %p", m);
47   if (XBT_LOG_ISENABLED(xbt_mallocator, xbt_log_priority_verbose))
48     xbt_backtrace_display_current();
49
50   m->objects = xbt_new0(void *, size);
51   m->max_size = size;
52   m->current_size = 0;
53   m->new_f = new_f;
54   m->free_f = free_f;
55   m->reset_f = reset_f;
56
57   return m;
58 }
59
60 /** \brief Destructor
61  * \param m the mallocator you want to destroy
62  *
63  * Destroy the mallocator and all its data. The function
64  * free_f is called on each object in the mallocator.
65  *
66  * \see xbt_mallocator_new()
67  */
68 void xbt_mallocator_free(xbt_mallocator_t m)
69 {
70
71   int i;
72   xbt_assert0(m != NULL, "Invalid parameter");
73
74   VERB3("Frees mallocator %p (size:%d/%d)", m, m->current_size, m->max_size);
75   for (i = 0; i < m->current_size; i++) {
76     (*(m->free_f)) (m->objects[i]);
77   }
78   xbt_free(m->objects);
79   xbt_free(m);
80 }
81
82 /**
83  * \brief Extract an object from a mallocator
84  * \param m a mallocator
85  *
86  * Remove an object from the mallocator and return it.
87  * This function is designed to be used instead of malloc().
88  * If the mallocator is not empty, an object is
89  * extracted from the mallocator and no malloc is done.
90  *
91  * If the mallocator is empty, a new object is created,
92  * by calling the function new_f().
93  *
94  * In both cases, the function reset_f() is called on the object.
95  *
96  * \see xbt_mallocator_release()
97  */
98 void *xbt_mallocator_get(xbt_mallocator_t m)
99 {
100   void *object;
101   xbt_assert0(m != NULL, "Invalid parameter");
102
103   if (m->current_size > 0) {
104     /* there is at least an available object */
105     DEBUG3("Reuse an old object for mallocator %p (size:%d/%d)", m,
106            m->current_size, m->max_size);
107     object = m->objects[--m->current_size];
108   } else {
109     /* otherwise we must allocate a new object */
110     DEBUG3("Create a new object for mallocator %p (size:%d/%d)", m,
111            m->current_size, m->max_size);
112     object = (*(m->new_f)) ();
113   }
114   (*(m->reset_f)) (object);
115   return object;
116 }
117
118 /** \brief Push an object into a mallocator
119  * \param m a mallocator
120  * \param object an object you don't need anymore
121  *
122  * Push into the mallocator an object you don't need anymore.
123  * This function is designed to be used instead of free().
124  * If the mallocator is not full, your object if stored into
125  * the mallocator and no free is done.
126  * If the mallocator is full, the object is freed by calling
127  * the function free_f().
128  *
129  * \see xbt_mallocator_get()
130  */
131 void xbt_mallocator_release(xbt_mallocator_t m, void *object)
132 {
133   xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
134
135   if (m->current_size < m->max_size) {
136     /* there is enough place to push the object */
137     DEBUG3
138       ("Store deleted object in mallocator %p for further use (size:%d/%d)",
139        m, m->current_size, m->max_size);
140     m->objects[m->current_size++] = object;
141   } else {
142     /* otherwise we don't have a choice, we must free the object */
143     DEBUG3("Free deleted object: mallocator %p is full (size:%d/%d)", m,
144            m->current_size, m->max_size);
145     (*(m->free_f)) (object);
146   }
147 }