Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
declare portable snprintf and vsnprintf functions for Visual C++ 7
[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 /**
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
35
36   xbt_mallocator_t m;
37
38   xbt_assert0(size > 0, "size must be positive");
39   xbt_assert0(new_f != NULL && free_f != NULL && reset_f != NULL,"invalid parameter");
40
41   m = xbt_new0(s_xbt_mallocator_t, 1);
42
43   m->objects = xbt_new0(void*, size);
44   m->max_size = size;
45   m->current_size = 0;
46   m->new_f = new_f;
47   m->free_f = free_f;
48   m->reset_f = reset_f;
49
50   return m;
51 }
52
53 /** \brief Destructor
54  * \param m the mallocator you want to destroy
55  *
56  * Destroy the mallocator and all its data. The function
57  * free_f is called on each object in the mallocator.
58  *
59  * \see xbt_mallocator_new()
60  */
61 void xbt_mallocator_free(xbt_mallocator_t m) {
62
63   int i;
64   xbt_assert0(m != NULL, "Invalid parameter");
65
66
67   for (i = 0; i < m->current_size; i++) {
68     m->free_f(m->objects[i]);
69   }
70   xbt_free(m->objects);
71   xbt_free(m);
72 }
73
74 /**
75  * \brief Extract an object from a mallocator
76  * \param m a mallocator
77  *
78  * Remove an object from the mallocator and return it.
79  * This function is designed to be used instead of malloc().
80  * If the mallocator is not empty, an object is
81  * extracted from the mallocator and no malloc is done.
82  * 
83  * If the mallocator is empty, a new object is created,
84  * by calling the function new_f().
85  * 
86  * In both cases, the function reset_f() is called on the object.
87  *
88  * \see xbt_mallocator_release()
89  */
90 void *xbt_mallocator_get(xbt_mallocator_t m) {
91   void *object;
92   xbt_assert0(m != NULL, "Invalid parameter");
93
94
95   if (m->current_size > 0) {
96     /* there is at least an available object */
97     object = m->objects[--m->current_size];
98   }
99   else {
100     /* otherwise we must allocate a new object */
101     object = m->new_f();
102   }
103   m->reset_f(object);
104   return object;
105 }
106
107 /** \brief Push an object into a mallocator
108  * \param m a mallocator
109  * \param object an object you don't need anymore
110  *
111  * Push into the mallocator an object you don't need anymore.
112  * This function is designed to be used instead of free().
113  * If the mallocator is not full, your object if stored into
114  * the mallocator and no free is done.
115  * If the mallocator is full, the object is freed by calling
116  * the function free_f().
117  *
118  * \see xbt_mallocator_get()
119  */
120 void xbt_mallocator_release(xbt_mallocator_t m, void *object) {
121   xbt_assert0(m != NULL && object != NULL, "Invalid parameter");
122
123   if (m->current_size < m->max_size) {
124     /* there is enough place to push the object */
125     m->objects[m->current_size++] = object;
126   }
127   else {
128     /* otherwise we don't have a choice, we must free the object */
129     m->free_f(object);
130   }
131 }