Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / dbestfit-3.3 / Malloc.c
blob25e30706fee40714137f4a2110020edf3aef49a6
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
6 /* Storleken på allokeringen bestäms genom att först slumpas en position i
7 "size_table" ut, sedan slumpas en storlek mellan den postionen och nästa värde
8 i tabellen. Genom att ha tabellen koncentrerad med låga värden, så skapas
9 flest såna. Rutinen håller på tills minnet en allokeringen nekas. Den kommer
10 aldrig att ha mer än MAXIMAL_MEMORY_TO_ALLOCATE allokerat samtidigt. Maximalt
11 har den MAX_ALLOCATIONS allokeringar samtidigt.
13 Statistiskt sätt så kommer efter ett tag MAX_ALLOCATIONS/2 allokeringar finnas
14 samtidigt, med varje allokering i median med värdet av halva "size_table".
16 När minnet är slut (malloc()=NULL), frågas användaren om han ska fortsätta.
18 Med jämna mellanrum skrivs statisktik ut på skärmen. (DISPLAY_WHEN)
20 För att stressa systemet med fler små allokeringar, så kan man öka
21 MAX_ALLOCATIONS. AMOUNT_OF_MEMORY bör få den att slå i taket fortare om man
22 minskar det.
24 Ingen initiering görs av slumptalen, så allt är upprepbart (men plocka bort
25 kommentaren på srand() och det löser sig.
29 /*#undef BMALLOC*/
31 #ifdef BMALLOC
32 #include "dmalloc.h"
34 #include "bmalloc.h"
35 #endif
37 #define MAX_ALLOCATIONS 100000
38 #define AMOUNT_OF_MEMORY 180000 /* bytes */
39 #define MAXIMAL_MEMORY_TO_ALLOCATE 100000 /* Sätt den här högre än
40 AMOUNT_OF_MEMORY, och malloc() bör
41 returnera NULL förr eller senare */
43 #define DISPLAY_WHEN (123456) /* When to display statistic */
45 #define min(a, b) (((a) < (b)) ? (a) : (b))
46 #define BOOL char
47 #define TRUE 1
48 #define FALSE 0
50 typedef struct {
51 char *memory;
52 long size;
53 char filled_with;
54 long table_position;
55 } MallocStruct;
58 Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras
59 eller reallokeras i.
62 MallocStruct my_mallocs[MAX_ALLOCATIONS];
64 long size_table[]={5,8,10,11,12,14,16,18,20,26,33,50,70,90,120,150,200,400,800,1000,2000,4000,8000,10000,11000,12000,13000,14000,15000,16000,17000,18000};
65 #define TABLESIZE ((sizeof(size_table)-1)/sizeof(long))
66 long size_allocs[TABLESIZE];
68 int main(void)
70 int i;
71 long count=-1;
72 long count_free=0, count_malloc=0, count_realloc=0;
73 long total_memory=0;
74 BOOL out_of_memory=FALSE;
75 unsigned int seed = time( NULL );
77 #ifdef BMALLOC
78 void *thisisourheap;
79 thisisourheap = (malloc)(AMOUNT_OF_MEMORY);
80 if(!thisisourheap)
81 return -1; /* can't get memory */
82 add_pool(thisisourheap, AMOUNT_OF_MEMORY);
83 #endif
85 seed = 1109323906;
87 srand( seed ); /* Initialize randomize */
89 printf("seed: %d\n", seed);
91 while (!out_of_memory) {
92 long number=rand()%MAX_ALLOCATIONS;
93 long size;
94 long table_position=rand()%TABLESIZE;
95 char fill_with=rand()&255;
97 count++;
99 size=rand()%(size_table[table_position+1]-size_table[table_position])+size_table[table_position];
101 /* fprintf(stderr, "number %d size %d\n", number, size); */
103 if (my_mallocs[number].size) { /* Om allokering redan finns på den här
104 positionen, så reallokerar vi eller
105 friar. */
106 long old_size=my_mallocs[number].size;
107 if (my_mallocs[number].size && fill_with<40) {
108 free(my_mallocs[number].memory);
109 total_memory -= my_mallocs[number].size;
110 count_free++;
111 size_allocs[my_mallocs[number].table_position]--;
112 size=0;
113 my_mallocs[number].size = 0;
114 my_mallocs[number].memory = NULL;
115 } else {
117 * realloc() part
120 char *temp;
121 #if 0
122 if(my_mallocs[number].size > size) {
123 printf("*** %d is realloc()ed to %d\n",
124 my_mallocs[number].size, size);
126 #endif
127 if (total_memory-old_size+size>MAXIMAL_MEMORY_TO_ALLOCATE)
128 goto output; /* for-loop */
129 temp = (char *)realloc(my_mallocs[number].memory, size);
130 if (!temp)
131 out_of_memory=TRUE;
132 else {
133 my_mallocs[number].memory = temp;
135 my_mallocs[number].size=size;
136 size_allocs[my_mallocs[number].table_position]--;
137 size_allocs[table_position]++;
138 total_memory -= old_size;
139 total_memory += size;
140 old_size=min(old_size, size);
141 while (--old_size>0) {
142 if (my_mallocs[number].memory[old_size]!=my_mallocs[number].filled_with)
143 fprintf(stderr, "Wrong filling!\n");
145 count_realloc++;
148 } else {
149 if (total_memory+size>MAXIMAL_MEMORY_TO_ALLOCATE) {
150 goto output; /* for-loop */
152 my_mallocs[number].memory=(char *)malloc(size); /* Allokera! */
153 if (!my_mallocs[number].memory)
154 out_of_memory=TRUE;
155 else {
156 size_allocs[table_position]++;
157 count_malloc++;
158 total_memory += size;
162 if(!out_of_memory) {
163 my_mallocs[number].table_position=table_position;
164 my_mallocs[number].size=size;
165 my_mallocs[number].filled_with=fill_with;
166 memset(my_mallocs[number].memory, fill_with, size);
168 output:
169 if (out_of_memory || !(count%DISPLAY_WHEN)) {
170 printf("(%d) malloc %d, realloc %d, free %d, total size %d\n", count, count_malloc, count_realloc, count_free, total_memory);
172 int count;
173 printf("[size bytes]=[number of allocations]\n");
174 for (count=0; count<TABLESIZE; count++) {
175 printf("%ld=%ld, ", size_table[count], size_allocs[count]);
177 printf("\n\n");
180 if (out_of_memory) {
181 fprintf(stderr, "Memory is out! Continue (y/n)");
182 switch (getchar()) {
183 case 'y':
184 case 'Y':
185 out_of_memory=FALSE;
186 break;
188 fprintf(stderr, "\n");
191 for(i = 0;i < MAX_ALLOCATIONS;i++) {
192 if((my_mallocs[i].memory))
193 free(my_mallocs[i].memory);
196 print_lists();
198 printf("\n");
199 return 0;
202 #include <stdio.h>
203 #include <stdlib.h>
204 #include <string.h>
205 #include <time.h>
207 /* Storleken på allokeringen bestäms genom att först slumpas en position i
208 "size_table" ut, sedan slumpas en storlek mellan den postionen och nästa värde
209 i tabellen. Genom att ha tabellen koncentrerad med låga värden, så skapas
210 flest såna. Rutinen håller på tills minnet en allokeringen nekas. Den kommer
211 aldrig att ha mer än MAXIMAL_MEMORY_TO_ALLOCATE allokerat samtidigt. Maximalt
212 har den MAX_ALLOCATIONS allokeringar samtidigt.
214 Statistiskt sätt så kommer efter ett tag MAX_ALLOCATIONS/2 allokeringar finnas
215 samtidigt, med varje allokering i median med värdet av halva "size_table".
217 När minnet är slut (malloc()=NULL), frågas användaren om han ska fortsätta.
219 Med jämna mellanrum skrivs statisktik ut på skärmen. (DISPLAY_WHEN)
221 För att stressa systemet med fler små allokeringar, så kan man öka
222 MAX_ALLOCATIONS. AMOUNT_OF_MEMORY bör få den att slå i taket fortare om man
223 minskar det.
225 Ingen initiering görs av slumptalen, så allt är upprepbart (men plocka bort
226 kommentaren på srand() och det löser sig.
230 /*#undef BMALLOC*/
232 #ifdef BMALLOC
233 #include "dmalloc.h"
235 #include "bmalloc.h"
236 #endif
238 #define MAX_ALLOCATIONS 100000
239 #define AMOUNT_OF_MEMORY 180000 /* bytes */
240 #define MAXIMAL_MEMORY_TO_ALLOCATE 100000 /* Sätt den här högre än
241 AMOUNT_OF_MEMORY, och malloc() bör
242 returnera NULL förr eller senare */
244 #define DISPLAY_WHEN (123456) /* When to display statistic */
246 #define min(a, b) (((a) < (b)) ? (a) : (b))
247 #define BOOL char
248 #define TRUE 1
249 #define FALSE 0
251 typedef struct {
252 char *memory;
253 long size;
254 char filled_with;
255 long table_position;
256 } MallocStruct;
259 Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras
260 eller reallokeras i.
263 MallocStruct my_mallocs[MAX_ALLOCATIONS];
265 long size_table[]={5,8,10,11,12,14,16,18,20,26,33,50,70,90,120,150,200,400,800,1000,2000,4000,8000,10000,11000,12000,13000,14000,15000,16000,17000,18000};
266 #define TABLESIZE ((sizeof(size_table)-1)/sizeof(long))
267 long size_allocs[TABLESIZE];
269 int main(void)
271 int i;
272 long count=-1;
273 long count_free=0, count_malloc=0, count_realloc=0;
274 long total_memory=0;
275 BOOL out_of_memory=FALSE;
276 unsigned int seed = time( NULL );
278 #ifdef BMALLOC
279 void *thisisourheap;
280 thisisourheap = (malloc)(AMOUNT_OF_MEMORY);
281 if(!thisisourheap)
282 return -1; /* can't get memory */
283 add_pool(thisisourheap, AMOUNT_OF_MEMORY);
284 #endif
286 seed = 1109323906;
288 srand( seed ); /* Initialize randomize */
290 printf("seed: %d\n", seed);
292 while (!out_of_memory) {
293 long number=rand()%MAX_ALLOCATIONS;
294 long size;
295 long table_position=rand()%TABLESIZE;
296 char fill_with=rand()&255;
298 count++;
300 size=rand()%(size_table[table_position+1]-size_table[table_position])+size_table[table_position];
302 /* fprintf(stderr, "number %d size %d\n", number, size); */
304 if (my_mallocs[number].size) { /* Om allokering redan finns på den här
305 positionen, så reallokerar vi eller
306 friar. */
307 long old_size=my_mallocs[number].size;
308 if (my_mallocs[number].size && fill_with<40) {
309 free(my_mallocs[number].memory);
310 total_memory -= my_mallocs[number].size;
311 count_free++;
312 size_allocs[my_mallocs[number].table_position]--;
313 size=0;
314 my_mallocs[number].size = 0;
315 my_mallocs[number].memory = NULL;
316 } else {
318 * realloc() part
321 char *temp;
322 #if 0
323 if(my_mallocs[number].size > size) {
324 printf("*** %d is realloc()ed to %d\n",
325 my_mallocs[number].size, size);
327 #endif
328 if (total_memory-old_size+size>MAXIMAL_MEMORY_TO_ALLOCATE)
329 goto output; /* for-loop */
330 temp = (char *)realloc(my_mallocs[number].memory, size);
331 if (!temp)
332 out_of_memory=TRUE;
333 else {
334 my_mallocs[number].memory = temp;
336 my_mallocs[number].size=size;
337 size_allocs[my_mallocs[number].table_position]--;
338 size_allocs[table_position]++;
339 total_memory -= old_size;
340 total_memory += size;
341 old_size=min(old_size, size);
342 while (--old_size>0) {
343 if (my_mallocs[number].memory[old_size]!=my_mallocs[number].filled_with)
344 fprintf(stderr, "Wrong filling!\n");
346 count_realloc++;
349 } else {
350 if (total_memory+size>MAXIMAL_MEMORY_TO_ALLOCATE) {
351 goto output; /* for-loop */
353 my_mallocs[number].memory=(char *)malloc(size); /* Allokera! */
354 if (!my_mallocs[number].memory)
355 out_of_memory=TRUE;
356 else {
357 size_allocs[table_position]++;
358 count_malloc++;
359 total_memory += size;
363 if(!out_of_memory) {
364 my_mallocs[number].table_position=table_position;
365 my_mallocs[number].size=size;
366 my_mallocs[number].filled_with=fill_with;
367 memset(my_mallocs[number].memory, fill_with, size);
369 output:
370 if (out_of_memory || !(count%DISPLAY_WHEN)) {
371 printf("(%d) malloc %d, realloc %d, free %d, total size %d\n", count, count_malloc, count_realloc, count_free, total_memory);
373 int count;
374 printf("[size bytes]=[number of allocations]\n");
375 for (count=0; count<TABLESIZE; count++) {
376 printf("%ld=%ld, ", size_table[count], size_allocs[count]);
378 printf("\n\n");
381 if (out_of_memory) {
382 fprintf(stderr, "Memory is out! Continue (y/n)");
383 switch (getchar()) {
384 case 'y':
385 case 'Y':
386 out_of_memory=FALSE;
387 break;
389 fprintf(stderr, "\n");
392 for(i = 0;i < MAX_ALLOCATIONS;i++) {
393 if((my_mallocs[i].memory))
394 free(my_mallocs[i].memory);
397 print_lists();
399 printf("\n");
400 return 0;