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
24 Ingen initiering görs av slumptalen, så allt är upprepbart (men plocka bort
25 kommentaren på srand() och det löser sig.
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))
58 Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras
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
];
72 long count_free
=0, count_malloc
=0, count_realloc
=0;
74 BOOL out_of_memory
=FALSE
;
75 unsigned int seed
= time( NULL
);
79 thisisourheap
= (malloc
)(AMOUNT_OF_MEMORY
);
81 return -1; /* can't get memory */
82 add_pool(thisisourheap
, AMOUNT_OF_MEMORY
);
87 srand( seed
); /* Initialize randomize */
89 printf("seed: %d\n", seed
);
91 while (!out_of_memory
) {
92 long number
=rand()%MAX_ALLOCATIONS
;
94 long table_position
=rand()%TABLESIZE
;
95 char fill_with
=rand()&255;
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
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
;
111 size_allocs
[my_mallocs
[number
].table_position
]--;
113 my_mallocs
[number
].size
= 0;
114 my_mallocs
[number
].memory
= NULL
;
122 if(my_mallocs
[number
].size
> size
) {
123 printf("*** %d is realloc()ed to %d\n",
124 my_mallocs
[number
].size
, size
);
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
);
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");
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
)
156 size_allocs
[table_position
]++;
158 total_memory
+= size
;
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
);
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
);
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
]);
181 fprintf(stderr
, "Memory is out! Continue (y/n)");
188 fprintf(stderr
, "\n");
191 for(i
= 0;i
< MAX_ALLOCATIONS
;i
++) {
192 if((my_mallocs
[i
].memory
))
193 free(my_mallocs
[i
].memory
);
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
225 Ingen initiering görs av slumptalen, så allt är upprepbart (men plocka bort
226 kommentaren på srand() och det löser sig.
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))
259 Skapar en lista med MAX_ALLOCATIONS storlek där det slumpvis allokeras
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
];
273 long count_free
=0, count_malloc
=0, count_realloc
=0;
275 BOOL out_of_memory
=FALSE
;
276 unsigned int seed
= time( NULL
);
280 thisisourheap
= (malloc
)(AMOUNT_OF_MEMORY
);
282 return -1; /* can't get memory */
283 add_pool(thisisourheap
, AMOUNT_OF_MEMORY
);
288 srand( seed
); /* Initialize randomize */
290 printf("seed: %d\n", seed
);
292 while (!out_of_memory
) {
293 long number
=rand()%MAX_ALLOCATIONS
;
295 long table_position
=rand()%TABLESIZE
;
296 char fill_with
=rand()&255;
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
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
;
312 size_allocs
[my_mallocs
[number
].table_position
]--;
314 my_mallocs
[number
].size
= 0;
315 my_mallocs
[number
].memory
= NULL
;
323 if(my_mallocs
[number
].size
> size
) {
324 printf("*** %d is realloc()ed to %d\n",
325 my_mallocs
[number
].size
, size
);
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
);
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");
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
)
357 size_allocs
[table_position
]++;
359 total_memory
+= size
;
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
);
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
);
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
]);
382 fprintf(stderr
, "Memory is out! Continue (y/n)");
389 fprintf(stderr
, "\n");
392 for(i
= 0;i
< MAX_ALLOCATIONS
;i
++) {
393 if((my_mallocs
[i
].memory
))
394 free(my_mallocs
[i
].memory
);