DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / util / memory.c
1 /********************************************************************
3 memory.c - memory handling routines
5 Niksa Orlic, 2005-03-22
7 ********************************************************************/
9 #include <stdlib.h>
10 #include <string.h>
12 #include "memory.h"
13 #include "error.h"
14 //#include "message.h"
16 #define SLOWLIST 1
18 static memory_object* memory_object_list_begin = NULL;
19 static memory_object* memory_object_list_end = NULL;
22 /*
23 Initialize the list of memory objects
24 */
25 void mem_init()
26 {
27 memory_object_list_begin = (memory_object*)malloc(sizeof(memory_object));
28 memory_object_list_begin->next = NULL;
29 memory_object_list_begin->prev = NULL;
30 memory_object_list_begin->data = NULL;
31 memory_object_list_end = memory_object_list_begin;
32 }
34 /*
35 Clean up the list of memory objects
36 */
37 void mem_close()
38 {
39 memory_object *it = memory_object_list_begin;
40 memory_object *old_object;
42 while (it != NULL)
43 {
44 old_object = it;
45 it = it->next;
46 #if SLOWLIST
47 if (old_object->data != NULL)
48 free(old_object->data);
49 free(old_object);
50 #else
51 mem_free(old_object);
52 #endif
53 }
54 }
56 /*
57 Allocate the new block of memory and append it into the list
58 */
59 void* mem_alloc(size_t s)
60 {
61 #if SLOWLIST
62 memory_object *wrapper = (memory_object*)malloc(sizeof(memory_object));
63 #else
64 memory_object *wrapper = (memory_object*)malloc(sizeof(memory_object) + s);
65 #endif
66 if (wrapper == NULL)
67 return NULL;
69 #if SLOWLIST
70 wrapper->data = malloc(s);
71 if (wrapper->data == NULL)
72 return NULL;
73 #else
74 wrapper->data = (char*)wrapper + sizeof(memory_object);
75 #endif
77 wrapper->next = NULL;
78 wrapper->prev = memory_object_list_end;
79 memory_object_list_end->next = wrapper;
80 memory_object_list_end = wrapper;
82 if (memory_object_list_end == NULL)
83 die(26);
85 return wrapper->data;
86 }
89 /*
90 Reallocate the new block of memory and append it into the list
91 */
92 void* mem_realloc(char* old_block, int s)
93 {
94 memory_object *wrapper;
95 memory_object *prev, *next;
97 void *new_block;
99 prev = next = NULL;
101 #if SLOWLIST
102 /* find the block */
104 memory_object *it = memory_object_list_begin;
106 while (it != NULL)
108 if (it->data == old_block)
109 {
110 break;
113 it = it->next;
116 it->data = realloc(it->data, s);
118 return it->data;
120 #else
121 prev = ((memory_object*)(old_block - sizeof(memory_object)))->prev;
122 next = ((memory_object*)(old_block - sizeof(memory_object)))->next;
124 new_block = realloc(old_block - sizeof(memory_object), sizeof(memory_object) + s);
126 if (new_block == NULL)
128 mem_free(old_block);
129 return NULL;
132 if ((old_block - sizeof(memory_object)) == new_block)
133 return old_block;
135 wrapper = (memory_object*) new_block;
137 if (prev != NULL)
138 prev->next = wrapper;
139 wrapper->prev = prev;
141 if (next != NULL)
142 next->prev = wrapper;
143 wrapper->next = next;
145 mem_free(old_block);
147 return wrapper->data;
148 #endif
151 /*
152 Free the block of memory
153 */
154 void mem_free(char* old_block)
155 {
156 memory_object *prev, *next;
158 prev = next = NULL;
160 if (old_block == NULL)
161 return;
163 #if SLOWLIST
164 /* find the block */
166 memory_object *it = memory_object_list_begin;
168 while (it != NULL)
170 if (it->data == old_block)
172 prev = it->prev;
173 next = it->next;
175 if (prev == NULL)
177 int a = 1;
180 free(old_block);
181 free(it);
183 break;
186 it = it->next;
189 if (it == NULL)
190 return;
192 #else
193 prev = ((memory_object*)(old_block - sizeof(memory_object)))->prev;
194 next = ((memory_object*)(old_block - sizeof(memory_object)))->next;
196 free(old_block - sizeof(memory_object));
197 #endif
199 if (prev != NULL)
200 prev->next = next;
202 if (next != NULL)
203 next->prev = prev;
205 if (next == NULL)
206 memory_object_list_end = prev;
208 if (memory_object_list_end == NULL)
210 int a = 1;