Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add Doxygen documentation for mallocators
[simgrid.git] / src / xbt / mallocator.c
1 /* dict - a generic dictionnary, variation over the B-tree concept          */
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 /**
14  * \brief Constructor
15  * \param size size of the internal stack: number of objects the mallocator
16  * will be able to store
17  * \param new_f function to allocate a new object of your datatype, called
18  * in \a xbt_mallocator_get() when the mallocator is empty
19  * \param free_f function to free an object of your datatype, called
20  * in \a xbt_mallocator_release() when the stack is full, and when
21  * the mallocator is freed.
22  * \param reset_f function to reinitialise an object of your datatype, called
23  * when you extract an object from the mallocator 
24  *
25  * Create and initialize a new mallocator for a given datatype.
26  *
27  * \return pointer to the created mallocator
28  * \see xbt_mallocator_free()
29  */
30 xbt_mallocator_t xbt_mallocator_new(int size,
31                                     pvoid_f_void_t new_f,
32                                     void_f_pvoid_t free_f,
33                                     void_f_pvoid_t reset_f) {
34   xbt_assert0(size > 0, "size must be positive");
35   xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,
36               "invalid parameter");
37   xbt_mallocator_t m = xbt_new0(s_xbt_mallocator_t, 1);
38
39   m->objects = xbt_new0(void*, size);
40   m->max_size = size;
41   m->current_size = 0;
42   m->new_f = new_f;
43   m->free_f = free_f;
44   m->reset_f = reset_f;
45
46   return m;
47 }
48
49 /** \brief Destructor
50  * \param m the mallocator you want to destroy
51  *
52  * Destroy the mallocator and all its data. The function
53  * free_f is called on each object in the mallocator.
54  *
55  * \see xbt_mallocator_new()
56  */
57 void xbt_mallocator_free(xbt_mallocator_t m) {
58   xbt_assert0(m != NULL, "Invalid parameter");
59
60   int i;
61   for (i = 0; i < m->current_size; i++) {
62     m->free_f(m->objects[i]);
63   }
64   xbt_free(m->objects);
65   xbt_free(m);
66 }
67
68 /**
69  * \brief Extract an object from a mallocator
70  * \param m a mallocator
71  *
72  * Remove an object from the mallocator and return it.
73  * This function is designed to be used instead of malloc().
74  * If the mallocator is not empty, an object is
75  * extracted from the mallocator and no malloc is done.
76  * 
77  * If the mallocator is empty, a new object is created,
78  * by calling the function new_f().
79  * 
80  * In both cases, the function reset_f() is called on the object.
81  *
82  * \see xbt_mallocator_release()
83  */
84 void *xbt_mallocator_get(xbt_mallocator_t m) {
85   xbt_assert0(m != NULL, "Invalid parameter");
86
87   void *object;
88   if (m->current_size > 0) {
89     /* there is at least an available object */
90     object = m->objects[--m->current_size];
91   }
92   else {
93     /* otherwise we must allocate a new object */
94     object = m->new_f();
95   }
96   m->reset_f(object);
97   return object;
98 }
99
100 /** \brief Push an object into a mallocator
101  * \param m a mallocator
102  * \param object an object you don't need anymore
103  *
104  * Push into the mallocator an object you don't need anymore.
105  * This function is designed to be used instead of free().
106  * If the mallocator is not full, your object if stored into
107  * the mallocator and no free is done.
108  * If the mallocator is full, the object is freed by calling
109  * the function free_f().
110  *
111  * \see xbt_mallocator_get()
112  */
113 void xbt_mallocator_release(xbt_mallocator_t m, void *object) {
114   xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
115
116   if (m->current_size < m->max_size) {
117     /* there is enough place to push the object */
118     m->objects[m->current_size++] = object;
119   }
120   else {
121     /* otherwise we don't have a choice, we must free the object */
122     m->free_f(object);
123   }
124 }