diff options
author | Daniel Stenberg <daniel@haxx.se> | 2004-01-08 15:43:37 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2004-01-08 15:43:37 +0000 |
commit | e11a1e29dff621cd1a9c1640732766ec87fc2726 (patch) | |
tree | 6d6ef7b62412cd1340ac51bbc51fcad389994d89 /firmware/test | |
parent | 21fba08fc3a66c7f395b2a95b3e6dd3dfd9c8002 (diff) | |
download | rockbox-e11a1e29dff621cd1a9c1640732766ec87fc2726.tar.gz rockbox-e11a1e29dff621cd1a9c1640732766ec87fc2726.zip |
malloc is not used in Rockbox
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4210 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/test')
-rw-r--r-- | firmware/test/malloc/Makefile | 36 | ||||
-rw-r--r-- | firmware/test/malloc/Malloc.c | 196 | ||||
-rw-r--r-- | firmware/test/malloc/dmytest.c | 173 | ||||
-rw-r--r-- | firmware/test/malloc/mytest.c | 70 |
4 files changed, 0 insertions, 475 deletions
diff --git a/firmware/test/malloc/Makefile b/firmware/test/malloc/Makefile deleted file mode 100644 index c7fdedb499..0000000000 --- a/firmware/test/malloc/Makefile +++ /dev/null | |||
@@ -1,36 +0,0 @@ | |||
1 | |||
2 | OBJS1 = mytest.o | ||
3 | TARGET1 = mytest | ||
4 | |||
5 | OBJS2 = Malloc.o | ||
6 | TARGET2 = mtest | ||
7 | |||
8 | OBJS3 = dmytest.o | ||
9 | TARGET3 = dmytest | ||
10 | |||
11 | # define this to talk a lot in runtime | ||
12 | # -DDEBUG_VERBOSE | ||
13 | CFLAGS = -g -Wall -DDEBUG -I../../malloc | ||
14 | CC = gcc | ||
15 | AR = ar | ||
16 | |||
17 | LDFLAGS = -L../../malloc -ldmalloc | ||
18 | |||
19 | all: $(TARGET1) $(TARGET2) $(TARGET3) | ||
20 | |||
21 | clean: | ||
22 | rm -f core *~ $(TARGET1) $(TARGET2) $(TARGET3) \ | ||
23 | $(OBJS1) $(OBJS2) $(OBJS3) | ||
24 | |||
25 | $(TARGET1): $(OBJS1) | ||
26 | $(CC) -g -o $(TARGET1) $(OBJS1) $(LDFLAGS) | ||
27 | |||
28 | $(TARGET2): $(OBJS2) | ||
29 | $(CC) -g -o $(TARGET2) $(OBJS2) $(LDFLAGS) | ||
30 | |||
31 | $(TARGET3): $(OBJS3) | ||
32 | $(CC) -g -o $(TARGET3) $(OBJS3) $(LDFLAGS) | ||
33 | |||
34 | dmytest.o: dmytest.c | ||
35 | Malloc.o: Malloc.c | ||
36 | mytest.o: mytest.c | ||
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 | |||
diff --git a/firmware/test/malloc/dmytest.c b/firmware/test/malloc/dmytest.c deleted file mode 100644 index a63bb30d74..0000000000 --- a/firmware/test/malloc/dmytest.c +++ /dev/null | |||
@@ -1,173 +0,0 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | |||
4 | #include "dmalloc.h" | ||
5 | #include "bmalloc.h" | ||
6 | |||
7 | #define MAX 500 | ||
8 | #define MAX2 1000 | ||
9 | #define MAXC 2 | ||
10 | |||
11 | #define TESTA | ||
12 | #define TESTB | ||
13 | #define TESTC | ||
14 | #define TESTD | ||
15 | |||
16 | int test1(void) | ||
17 | { | ||
18 | #define MAXK 100 | ||
19 | int i; | ||
20 | void *wow[MAXK]; | ||
21 | for(i=0; i<MAXK; i++) | ||
22 | if(!(wow[i]=malloc(412))) { | ||
23 | printf("*** Couldn't allocate memory, exiting\n"); | ||
24 | return -2; | ||
25 | } | ||
26 | for(i=MAXK-1; i>=0; i-=2) | ||
27 | free(wow[i]); | ||
28 | return 0; | ||
29 | } | ||
30 | |||
31 | int test2(void) | ||
32 | { | ||
33 | #define MAXS 10 | ||
34 | #define MAXS1 0 | ||
35 | int i; | ||
36 | void *ptr[MAXS]; | ||
37 | |||
38 | for(i=MAXS1; i< MAXS; i++) { | ||
39 | printf("%d malloc(%d)\n", i, i*55); | ||
40 | ptr[i] = malloc (i*55); | ||
41 | } | ||
42 | for(i=MAXS1; i< MAXS; i++) { | ||
43 | void *tmp; | ||
44 | printf("%d realloc(%d)\n", i, i*155); | ||
45 | tmp=realloc(ptr[i], i*155); | ||
46 | if(tmp) | ||
47 | ptr[i] = tmp; | ||
48 | } | ||
49 | for(i=MAXS1; i< MAXS; i++) { | ||
50 | if(ptr[i]) { | ||
51 | printf("%d free(%d)\n", i, i*155); | ||
52 | free(ptr[i]); | ||
53 | } | ||
54 | } | ||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | int test3(void) | ||
59 | { | ||
60 | int i; | ||
61 | void *ptr[MAXC]; | ||
62 | printf("This is test C:\n"); | ||
63 | |||
64 | for(i=0; i< MAXC; i++) { | ||
65 | printf("%d malloc(100)\n", i+1); | ||
66 | ptr[i] = malloc(100); | ||
67 | printf(" ...returned %p\n", ptr[i]); | ||
68 | } | ||
69 | |||
70 | for(i=0; i< MAXC; i++) { | ||
71 | printf("%d free()\n", i+1); | ||
72 | if(ptr[i]) | ||
73 | free(ptr[i]); | ||
74 | } | ||
75 | |||
76 | printf("End of test C:\n"); | ||
77 | |||
78 | return 0; | ||
79 | } | ||
80 | |||
81 | int test4(void) | ||
82 | { | ||
83 | int i; | ||
84 | int memory = 0; | ||
85 | void *pointers[MAX]; | ||
86 | printf("This is test I:\n"); | ||
87 | |||
88 | for(i=0; i<MAX; i++) { | ||
89 | printf("%d attempts malloc(%d)\n", i, i*6); | ||
90 | pointers[i]=malloc(i*6); | ||
91 | if(!pointers[i]) { | ||
92 | printf("cant get more memory!"); | ||
93 | return(0); | ||
94 | } | ||
95 | memory += (i*6); | ||
96 | } | ||
97 | printf("\namount: %d\n", memory); | ||
98 | memory = 0; | ||
99 | for(i=0; i<MAX; i++) { | ||
100 | printf("%d attempts realloc(%d)\n", i, i*7); | ||
101 | pointers[i]=realloc(pointers[i], i*7); | ||
102 | memory += i*7; | ||
103 | } | ||
104 | printf("\namount: %d\n", memory); | ||
105 | for(i=0; i<MAX; i++) { | ||
106 | printf("%d attempts free(%d)\n", i, i*7); | ||
107 | free(pointers[i]); | ||
108 | } | ||
109 | printf("\nend of test 1\n"); | ||
110 | |||
111 | return 0; | ||
112 | } | ||
113 | |||
114 | int test5(void) | ||
115 | { | ||
116 | int memory = 0; | ||
117 | int i; | ||
118 | void *pointers2[MAX2]; | ||
119 | memory = 0; | ||
120 | printf("\nTest II\n"); | ||
121 | for(i=0; i< MAX2; i++) { | ||
122 | printf("%d attempts malloc(%d)\n", i, 7); | ||
123 | pointers2[i] = malloc(7); | ||
124 | memory += 7; | ||
125 | } | ||
126 | printf("\namount: %d\n", memory); | ||
127 | for(i=0; i< MAX2; i++) { | ||
128 | if(pointers2[i]) | ||
129 | free(pointers2[i]); | ||
130 | } | ||
131 | printf("\nend of test II\n"); | ||
132 | |||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | #define HEAPSIZE 10000 | ||
137 | |||
138 | void smallblocks(void) | ||
139 | { | ||
140 | void *ptr; | ||
141 | int i=0; | ||
142 | do { | ||
143 | |||
144 | ptr = malloc(16); | ||
145 | i++; | ||
146 | } while(ptr); | ||
147 | |||
148 | printf("I: %d\n", i); | ||
149 | } | ||
150 | |||
151 | int main(int argc, char **argv) | ||
152 | { | ||
153 | void *heap = (malloc)(HEAPSIZE); | ||
154 | if(!heap) | ||
155 | return -1; | ||
156 | dmalloc_initialize(); | ||
157 | bmalloc_add_pool(heap, HEAPSIZE); | ||
158 | |||
159 | smallblocks(); | ||
160 | |||
161 | bmalloc_status(); | ||
162 | dmalloc_status(); | ||
163 | |||
164 | return 0; | ||
165 | |||
166 | test1(); | ||
167 | test2(); | ||
168 | test3(); | ||
169 | test4(); | ||
170 | test5(); | ||
171 | |||
172 | return 0; | ||
173 | } | ||
diff --git a/firmware/test/malloc/mytest.c b/firmware/test/malloc/mytest.c deleted file mode 100644 index 2ee5bd0308..0000000000 --- a/firmware/test/malloc/mytest.c +++ /dev/null | |||
@@ -1,70 +0,0 @@ | |||
1 | |||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | |||
5 | #include "bmalloc.h" | ||
6 | |||
7 | int main(int argc, char **argv) | ||
8 | { | ||
9 | void *pointers[5]; | ||
10 | int i; | ||
11 | void *area; | ||
12 | |||
13 | for(i=0; i<5; i++) | ||
14 | pointers[i] = malloc(8000); | ||
15 | |||
16 | if(argc>1) { | ||
17 | switch(argv[1][0]) { | ||
18 | case '1': | ||
19 | for(i=0; i<5; i++) { | ||
20 | bmalloc_add_pool(pointers[i], 4000); | ||
21 | bmalloc_add_pool((char *)pointers[i]+4000, 4000); | ||
22 | } | ||
23 | break; | ||
24 | case '2': | ||
25 | area = malloc(20000); | ||
26 | bmalloc_add_pool(area, 3000); | ||
27 | bmalloc_add_pool((char *)area+6000, 3000); | ||
28 | bmalloc_add_pool((char *)area+3000, 3000); | ||
29 | bmalloc_add_pool((char *)area+12000, 3000); | ||
30 | bmalloc_add_pool((char *)area+9000, 3000); | ||
31 | break; | ||
32 | case '3': | ||
33 | { | ||
34 | void *ptr[10]; | ||
35 | area = malloc(20000); | ||
36 | bmalloc_add_pool(area, 20000); | ||
37 | |||
38 | printf(" ** TEST USAGE\n"); | ||
39 | for(i=0; i<9; i++) | ||
40 | ptr[i]=bmalloc(200); | ||
41 | bmalloc_status(); | ||
42 | for(i=0; i<9; i++) | ||
43 | bfree(ptr[i]); | ||
44 | printf(" ** END OF TEST USAGE\n"); | ||
45 | } | ||
46 | |||
47 | break; | ||
48 | case '4': | ||
49 | { | ||
50 | void *ptr[10]; | ||
51 | area = malloc(20000); | ||
52 | bmalloc_add_pool(area, 20000); | ||
53 | |||
54 | ptr[0]=bmalloc(4080); | ||
55 | bmalloc_status(); | ||
56 | bfree(ptr[0]); | ||
57 | printf(" ** END OF TEST USAGE\n"); | ||
58 | } | ||
59 | |||
60 | break; | ||
61 | } | ||
62 | } | ||
63 | else | ||
64 | for(i=4; i>=0; i--) | ||
65 | bmalloc_add_pool(pointers[i], 8000-i*100); | ||
66 | |||
67 | bmalloc_status(); | ||
68 | |||
69 | return 0; | ||
70 | } | ||