diff options
Diffstat (limited to 'firmware/test/malloc/Malloc.c')
-rw-r--r-- | firmware/test/malloc/Malloc.c | 196 |
1 files changed, 0 insertions, 196 deletions
diff --git a/firmware/test/malloc/Malloc.c b/firmware/test/malloc/Malloc.c deleted file mode 100644 index 63239fe2ab..0000000000 --- a/firmware/test/malloc/Malloc.c +++ /dev/null | |||
@@ -1,196 +0,0 @@ | |||
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 | #define BMALLOC /* go go go */ | ||
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 100000 /* bytes */ | ||
39 | #define MAXIMAL_MEMORY_TO_ALLOCATE 49000 /* 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 (10000) /* 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}; | ||
65 | #define TABLESIZE ((sizeof(size_table)-1)/sizeof(long)) | ||
66 | long size_allocs[TABLESIZE]; | ||
67 | |||
68 | int main(void) | ||
69 | { | ||
70 | long count=-1; | ||
71 | long count_free=0, count_malloc=0, count_realloc=0; | ||
72 | long total_memory=0; | ||
73 | long out_of_memory=FALSE; | ||
74 | |||
75 | dmalloc_initialize(); | ||
76 | |||
77 | #ifdef BMALLOC | ||
78 | void *thisisourheap; | ||
79 | thisisourheap = (malloc)(AMOUNT_OF_MEMORY); | ||
80 | if(!thisisourheap) | ||
81 | return -1; /* can't get memory */ | ||
82 | bmalloc_add_pool(thisisourheap, AMOUNT_OF_MEMORY); | ||
83 | #endif | ||
84 | |||
85 | srand( 0 ); /* Initialize to a fixed random */ | ||
86 | |||
87 | while (!out_of_memory) { | ||
88 | long number=rand()%MAX_ALLOCATIONS; | ||
89 | long size; | ||
90 | long table_position=rand()%TABLESIZE; | ||
91 | char fill_with=rand()&255; | ||
92 | |||
93 | count++; | ||
94 | |||
95 | size=rand()%(size_table[table_position+1]- | ||
96 | size_table[table_position])+ | ||
97 | size_table[table_position]; | ||
98 | |||
99 | /* fprintf(stderr, "number %d size %d\n", number, size); */ | ||
100 | |||
101 | if (my_mallocs[number].size) { /* Om allokering redan finns på den här | ||
102 | positionen, så reallokerar vi eller | ||
103 | friar. */ | ||
104 | long old_size=my_mallocs[number].size; | ||
105 | if (my_mallocs[number].size && fill_with<40) { | ||
106 | free(my_mallocs[number].memory); | ||
107 | total_memory -= my_mallocs[number].size; | ||
108 | count_free++; | ||
109 | size_allocs[my_mallocs[number].table_position]--; | ||
110 | size=0; | ||
111 | } else { | ||
112 | /* | ||
113 | * realloc() part | ||
114 | * | ||
115 | */ | ||
116 | char *temp; | ||
117 | #if 0 | ||
118 | if(my_mallocs[number].size > size) { | ||
119 | printf("*** %d is realloc()ed to %d\n", | ||
120 | my_mallocs[number].size, size); | ||
121 | } | ||
122 | #endif | ||
123 | if (total_memory-old_size+size>MAXIMAL_MEMORY_TO_ALLOCATE) | ||
124 | goto output; /* for-loop */ | ||
125 | temp = (char *)realloc(my_mallocs[number].memory, size); | ||
126 | if (!temp) | ||
127 | out_of_memory=size; | ||
128 | else { | ||
129 | my_mallocs[number].memory = temp; | ||
130 | |||
131 | my_mallocs[number].size=size; | ||
132 | size_allocs[my_mallocs[number].table_position]--; | ||
133 | size_allocs[table_position]++; | ||
134 | total_memory -= old_size; | ||
135 | total_memory += size; | ||
136 | old_size=min(old_size, size); | ||
137 | while (--old_size>0) { | ||
138 | if (my_mallocs[number].memory[old_size]!=my_mallocs[number].filled_with) | ||
139 | fprintf(stderr, "Wrong filling!\n"); | ||
140 | } | ||
141 | count_realloc++; | ||
142 | } | ||
143 | } | ||
144 | } else { | ||
145 | if (total_memory+size>MAXIMAL_MEMORY_TO_ALLOCATE) { | ||
146 | goto output; /* for-loop */ | ||
147 | } | ||
148 | my_mallocs[number].memory=(char *)malloc(size); /* Allokera! */ | ||
149 | if (!my_mallocs[number].memory) | ||
150 | out_of_memory=size; | ||
151 | else { | ||
152 | size_allocs[table_position]++; | ||
153 | count_malloc++; | ||
154 | total_memory += size; | ||
155 | } | ||
156 | } | ||
157 | if(!out_of_memory) { | ||
158 | my_mallocs[number].table_position=table_position; | ||
159 | my_mallocs[number].size=size; | ||
160 | my_mallocs[number].filled_with=fill_with; | ||
161 | memset(my_mallocs[number].memory, fill_with, size); | ||
162 | } | ||
163 | output: | ||
164 | if (out_of_memory || !(count%DISPLAY_WHEN)) { | ||
165 | printf("(%ld) malloc %ld, realloc %ld, free %ld, total size %ld\n", | ||
166 | count, count_malloc, count_realloc, count_free, total_memory); | ||
167 | { | ||
168 | int count; | ||
169 | printf("[size bytes]=[number of allocations]\n"); | ||
170 | for (count=0; count<TABLESIZE; count++) { | ||
171 | printf(" %ld=%ld\n", size_table[count], size_allocs[count]); | ||
172 | } | ||
173 | printf("\n\n"); | ||
174 | } | ||
175 | } | ||
176 | if (out_of_memory) { | ||
177 | if(out_of_memory) | ||
178 | printf("Couldn't get %ld bytes\n", out_of_memory); | ||
179 | |||
180 | dmalloc_status(); | ||
181 | bmalloc_status(); | ||
182 | |||
183 | fprintf(stderr, "Memory is out! Continue (y/n)"); | ||
184 | switch (getchar()) { | ||
185 | case 'y': | ||
186 | case 'Y': | ||
187 | out_of_memory=FALSE; | ||
188 | break; | ||
189 | } | ||
190 | fprintf(stderr, "\n"); | ||
191 | } | ||
192 | } | ||
193 | printf("\n"); | ||
194 | return 0; | ||
195 | } | ||
196 | |||