configure.ac: Add checks for Flex/Yacc/Bison and other furry animals, for the new...
[platform/upstream/gst-plugins-good.git] / gst / goom / goomsl_heap.c
1 #include "goomsl_heap.h"
2 #include <stdlib.h>
3
4 struct _GOOM_HEAP
5 {
6   void **arrays;
7   int number_of_arrays;
8   int size_of_each_array;
9   int consumed_in_last_array;
10 };
11
12 /* Constructors / Destructor */
13 GoomHeap *
14 goom_heap_new (void)
15 {
16   return goom_heap_new_with_granularity (4096);
17 }
18
19 GoomHeap *
20 goom_heap_new_with_granularity (int granularity)
21 {
22   GoomHeap *_this;
23
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 *));
29   return _this;
30 }
31
32 void
33 goom_heap_delete (GoomHeap * _this)
34 {
35   int i;
36
37   for (i = 0; i < _this->number_of_arrays; ++i) {
38     free (_this->arrays[i]);
39   }
40   free (_this->arrays);
41   free (_this);
42 }
43
44 static void
45 align_it (GoomHeap * _this, int alignment)
46 {
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);
51
52     if (decal != 0) {
53       _this->consumed_in_last_array += alignment - decal;
54     }
55   }
56 }
57
58 void *
59 goom_heap_malloc_with_alignment_prefixed (GoomHeap * _this, int nb_bytes,
60     int alignment, int prefix_bytes)
61 {
62   void *retval = NULL;
63
64   /* d'abord on gere les problemes d'alignement */
65   _this->consumed_in_last_array += prefix_bytes;
66   align_it (_this, alignment);
67
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)) {
71
72     if (prefix_bytes + nb_bytes + alignment >= _this->size_of_each_array) {
73
74       /* Si la zone demandee est plus grosse que la granularitee */
75       /* On alloue un buffer plus gros que les autres */
76       _this->arrays =
77           (void **) realloc (_this->arrays,
78           sizeof (void *) * (_this->number_of_arrays + 2));
79
80       _this->number_of_arrays += 1;
81       _this->consumed_in_last_array = prefix_bytes;
82
83       _this->arrays[_this->number_of_arrays - 1] =
84           malloc (prefix_bytes + nb_bytes + alignment);
85       align_it (_this, alignment);
86       retval =
87           (void *) ((char *) _this->arrays[_this->number_of_arrays - 1] +
88           _this->consumed_in_last_array);
89
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);
95       return retval;
96     } else {
97       _this->number_of_arrays += 1;
98       _this->consumed_in_last_array = prefix_bytes;
99       _this->arrays =
100           (void **) realloc (_this->arrays,
101           sizeof (void *) * _this->number_of_arrays);
102
103       _this->arrays[_this->number_of_arrays - 1] =
104           malloc (_this->size_of_each_array);
105       align_it (_this, alignment);
106     }
107   }
108   retval =
109       (void *) ((char *) _this->arrays[_this->number_of_arrays - 1] +
110       _this->consumed_in_last_array);
111   _this->consumed_in_last_array += nb_bytes;
112   return retval;
113 }
114
115 void *
116 goom_heap_malloc_with_alignment (GoomHeap * _this, int nb_bytes, int alignment)
117 {
118   return goom_heap_malloc_with_alignment_prefixed (_this, nb_bytes, alignment,
119       0);
120 }
121
122 void *
123 goom_heap_malloc (GoomHeap * _this, int nb_bytes)
124 {
125   return goom_heap_malloc_with_alignment (_this, nb_bytes, 1);
126 }