1 #include "goomsl_heap.h"
8 int size_of_each_array;
9 int consumed_in_last_array;
12 /* Constructors / Destructor */
16 return goom_heap_new_with_granularity (4096);
20 goom_heap_new_with_granularity (int granularity)
24 _this = (GoomHeap *) malloc (sizeof (GoomHeap));
25 _this->number_of_arrays = 0;
26 _this->size_of_each_array = granularity;
27 _this->consumed_in_last_array = 0;
28 _this->arrays = (void **) malloc (sizeof (void *));
33 goom_heap_delete (GoomHeap * _this)
37 for (i = 0; i < _this->number_of_arrays; ++i) {
38 free (_this->arrays[i]);
45 align_it (GoomHeap * _this, int alignment)
47 if ((alignment > 1) && (_this->number_of_arrays > 0)) {
48 void *last_array = _this->arrays[_this->number_of_arrays - 1];
49 int last_address = (int) last_array + _this->consumed_in_last_array;
50 int decal = (last_address % alignment);
53 _this->consumed_in_last_array += alignment - decal;
59 goom_heap_malloc_with_alignment_prefixed (GoomHeap * _this, int nb_bytes,
60 int alignment, int prefix_bytes)
64 /* d'abord on gere les problemes d'alignement */
65 _this->consumed_in_last_array += prefix_bytes;
66 align_it (_this, alignment);
68 /* ensuite on verifie que la quantite de memoire demandee tient dans le buffer */
69 if ((_this->consumed_in_last_array + nb_bytes >= _this->size_of_each_array)
70 || (_this->number_of_arrays == 0)) {
72 if (prefix_bytes + nb_bytes + alignment >= _this->size_of_each_array) {
74 /* Si la zone demandee est plus grosse que la granularitee */
75 /* On alloue un buffer plus gros que les autres */
77 (void **) realloc (_this->arrays,
78 sizeof (void *) * (_this->number_of_arrays + 2));
80 _this->number_of_arrays += 1;
81 _this->consumed_in_last_array = prefix_bytes;
83 _this->arrays[_this->number_of_arrays - 1] =
84 malloc (prefix_bytes + nb_bytes + alignment);
85 align_it (_this, alignment);
87 (void *) ((char *) _this->arrays[_this->number_of_arrays - 1] +
88 _this->consumed_in_last_array);
90 /* puis on repart sur un nouveau buffer vide */
91 _this->number_of_arrays += 1;
92 _this->consumed_in_last_array = 0;
93 _this->arrays[_this->number_of_arrays - 1] =
94 malloc (_this->size_of_each_array);
97 _this->number_of_arrays += 1;
98 _this->consumed_in_last_array = prefix_bytes;
100 (void **) realloc (_this->arrays,
101 sizeof (void *) * _this->number_of_arrays);
103 _this->arrays[_this->number_of_arrays - 1] =
104 malloc (_this->size_of_each_array);
105 align_it (_this, alignment);
109 (void *) ((char *) _this->arrays[_this->number_of_arrays - 1] +
110 _this->consumed_in_last_array);
111 _this->consumed_in_last_array += nb_bytes;
116 goom_heap_malloc_with_alignment (GoomHeap * _this, int nb_bytes, int alignment)
118 return goom_heap_malloc_with_alignment_prefixed (_this, nb_bytes, alignment,
123 goom_heap_malloc (GoomHeap * _this, int nb_bytes)
125 return goom_heap_malloc_with_alignment (_this, nb_bytes, 1);