Logo AND Algorithmique Numérique Distribuée

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