diff options
author | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
---|---|---|
committer | Peter D'Hoye <peter.dhoye@gmail.com> | 2009-05-22 21:58:48 +0000 |
commit | 513389b4c1bc8afe4b2dc9947c534bfeb105e3da (patch) | |
tree | 10e673b35651ac567fed2eda0c679c7ade64cbc6 /apps/plugins/pdbox/dbestfit-3.3/Malloc.c | |
parent | 95fa7f6a2ef466444fbe3fe87efc6d5db6b77b36 (diff) | |
download | rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.tar.gz rockbox-513389b4c1bc8afe4b2dc9947c534bfeb105e3da.zip |
Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin project of Wincent Balin. Stripped some non-sourcefiles and added a rockbox readme that needs a bit more info from Wincent. Is added to CATEGORIES and viewers, but not yet to SUBDIRS (ie doesn't build yet)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21044 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/pdbox/dbestfit-3.3/Malloc.c')
-rw-r--r-- | apps/plugins/pdbox/dbestfit-3.3/Malloc.c | 402 |
1 files changed, 402 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/dbestfit-3.3/Malloc.c b/apps/plugins/pdbox/dbestfit-3.3/Malloc.c new file mode 100644 index 0000000000..25e30706fe --- /dev/null +++ b/apps/plugins/pdbox/dbestfit-3.3/Malloc.c | |||
@@ -0,0 +1,402 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include <time.h> | ||
5 | |||
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. | ||
12 | |||
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". | ||
15 | |||
16 | När minnet är slut (malloc()=NULL), frågas användaren om han ska fortsätta. | ||
17 | |||
18 | Med jämna mellanrum skrivs statisktik ut på skärmen. (DISPLAY_WHEN) | ||
19 | |||
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. | ||
23 | |||
24 | Ingen initiering görs av slumptalen, så allt är upprepbart (men plocka bort | ||
25 | kommentaren på srand() och det löser sig. | ||
26 | |||
27 | */ | ||
28 | |||
29 | /*#undef BMALLOC*/ | ||
30 | |||
31 | #ifdef BMALLOC | ||
32 | #include "dmalloc.h" | ||
33 | |||
34 | #include "bmalloc.h" | ||
35 | #endif | ||
36 | |||
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 */ | ||
42 | |||
43 | #define DISPLAY_WHEN (123456) /* When to display statistic */ | ||
44 | |||
45 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | ||
46 | #define BOOL char | ||
47 | #define TRUE 1 | ||
48 | #define FALSE 0 | ||
49 | |||
50 | typedef struct { | ||
51 | char *memory; | ||
52 | long size; | ||
53 | char filled_with; | ||
54 | long table_position; | ||
55 | } MallocStruct; | ||
56 | |||
57 | /* | ||
58 | Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras | ||
59 | eller reallokeras i. | ||
60 | */ | ||
61 | |||
62 | MallocStruct my_mallocs[MAX_ALLOCATIONS]; | ||
63 | |||
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]; | ||
67 | |||
68 | int main(void) | ||
69 | { | ||
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 ); | ||
76 | |||
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 | ||
84 | |||
85 | seed = 1109323906; | ||
86 | |||
87 | srand( seed ); /* Initialize randomize */ | ||
88 | |||
89 | printf("seed: %d\n", seed); | ||
90 | |||
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; | ||
96 | |||
97 | count++; | ||
98 | |||
99 | size=rand()%(size_table[table_position+1]-size_table[table_position])+size_table[table_position]; | ||
100 | |||
101 | /* fprintf(stderr, "number %d size %d\n", number, size); */ | ||
102 | |||
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 { | ||
116 | /* | ||
117 | * realloc() part | ||
118 | * | ||
119 | */ | ||
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); | ||
125 | } | ||
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; | ||
134 | |||
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"); | ||
144 | } | ||
145 | count_realloc++; | ||
146 | } | ||
147 | } | ||
148 | } else { | ||
149 | if (total_memory+size>MAXIMAL_MEMORY_TO_ALLOCATE) { | ||
150 | goto output; /* for-loop */ | ||
151 | } | ||
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; | ||
159 | } | ||
160 | } | ||
161 | |||
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); | ||
167 | } | ||
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); | ||
171 | { | ||
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]); | ||
176 | } | ||
177 | printf("\n\n"); | ||
178 | } | ||
179 | } | ||
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; | ||
187 | } | ||
188 | fprintf(stderr, "\n"); | ||
189 | } | ||
190 | } | ||
191 | for(i = 0;i < MAX_ALLOCATIONS;i++) { | ||
192 | if((my_mallocs[i].memory)) | ||
193 | free(my_mallocs[i].memory); | ||
194 | } | ||
195 | |||
196 | print_lists(); | ||
197 | |||
198 | printf("\n"); | ||
199 | return 0; | ||
200 | } | ||
201 | |||
202 | #include <stdio.h> | ||
203 | #include <stdlib.h> | ||
204 | #include <string.h> | ||
205 | #include <time.h> | ||
206 | |||
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. | ||
213 | |||
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". | ||
216 | |||
217 | När minnet är slut (malloc()=NULL), frågas användaren om han ska fortsätta. | ||
218 | |||
219 | Med jämna mellanrum skrivs statisktik ut på skärmen. (DISPLAY_WHEN) | ||
220 | |||
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. | ||
224 | |||
225 | Ingen initiering görs av slumptalen, så allt är upprepbart (men plocka bort | ||
226 | kommentaren på srand() och det löser sig. | ||
227 | |||
228 | */ | ||
229 | |||
230 | /*#undef BMALLOC*/ | ||
231 | |||
232 | #ifdef BMALLOC | ||
233 | #include "dmalloc.h" | ||
234 | |||
235 | #include "bmalloc.h" | ||
236 | #endif | ||
237 | |||
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 */ | ||
243 | |||
244 | #define DISPLAY_WHEN (123456) /* When to display statistic */ | ||
245 | |||
246 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | ||
247 | #define BOOL char | ||
248 | #define TRUE 1 | ||
249 | #define FALSE 0 | ||
250 | |||
251 | typedef struct { | ||
252 | char *memory; | ||
253 | long size; | ||
254 | char filled_with; | ||
255 | long table_position; | ||
256 | } MallocStruct; | ||
257 | |||
258 | /* | ||
259 | Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras | ||
260 | eller reallokeras i. | ||
261 | */ | ||
262 | |||
263 | MallocStruct my_mallocs[MAX_ALLOCATIONS]; | ||
264 | |||
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]; | ||
268 | |||
269 | int main(void) | ||
270 | { | ||
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 ); | ||
277 | |||
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 | ||
285 | |||
286 | seed = 1109323906; | ||
287 | |||
288 | srand( seed ); /* Initialize randomize */ | ||
289 | |||
290 | printf("seed: %d\n", seed); | ||
291 | |||
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; | ||
297 | |||
298 | count++; | ||
299 | |||
300 | size=rand()%(size_table[table_position+1]-size_table[table_position])+size_table[table_position]; | ||
301 | |||
302 | /* fprintf(stderr, "number %d size %d\n", number, size); */ | ||
303 | |||
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 { | ||
317 | /* | ||
318 | * realloc() part | ||
319 | * | ||
320 | */ | ||
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); | ||
326 | } | ||
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; | ||
335 | |||
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"); | ||
345 | } | ||
346 | count_realloc++; | ||
347 | } | ||
348 | } | ||
349 | } else { | ||
350 | if (total_memory+size>MAXIMAL_MEMORY_TO_ALLOCATE) { | ||
351 | goto output; /* for-loop */ | ||
352 | } | ||
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; | ||
360 | } | ||
361 | } | ||
362 | |||
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); | ||
368 | } | ||
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); | ||
372 | { | ||
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]); | ||
377 | } | ||
378 | printf("\n\n"); | ||
379 | } | ||
380 | } | ||
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; | ||
388 | } | ||
389 | fprintf(stderr, "\n"); | ||
390 | } | ||
391 | } | ||
392 | for(i = 0;i < MAX_ALLOCATIONS;i++) { | ||
393 | if((my_mallocs[i].memory)) | ||
394 | free(my_mallocs[i].memory); | ||
395 | } | ||
396 | |||
397 | print_lists(); | ||
398 | |||
399 | printf("\n"); | ||
400 | return 0; | ||
401 | } | ||
402 | |||