1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 
5 #include "../smalloc.h"
6 #include "../flist.h"
7 #include "debug.h"
8 
9 #define MAGIC1	0xa9b1c8d2
10 #define MAGIC2	0xf0a1e9b3
11 
12 #define LOOPS	32
13 
14 struct elem {
15 	unsigned int magic1;
16 	struct flist_head list;
17 	unsigned int magic2;
18 };
19 
20 FLIST_HEAD(list);
21 
do_rand_allocs(void)22 static int do_rand_allocs(void)
23 {
24 	unsigned int size, nr, rounds = 0;
25 	unsigned long total;
26 	struct elem *e;
27 
28 	while (rounds++ < LOOPS) {
29 #ifdef STEST_SEED
30 		srand(MAGIC1);
31 #endif
32 		nr = total = 0;
33 		while (total < 128*1024*1024UL) {
34 			size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0)));
35 			e = smalloc(size);
36 			if (!e) {
37 				printf("fail at %lu, size %u\n", total, size);
38 				break;
39 			}
40 			e->magic1 = MAGIC1;
41 			e->magic2 = MAGIC2;
42 			total += size;
43 			flist_add_tail(&e->list, &list);
44 			nr++;
45 		}
46 
47 		printf("Got items: %u\n", nr);
48 
49 		while (!flist_empty(&list)) {
50 			e = flist_entry(list.next, struct elem, list);
51 			assert(e->magic1 == MAGIC1);
52 			assert(e->magic2 == MAGIC2);
53 			flist_del(&e->list);
54 			sfree(e);
55 		}
56 	}
57 
58 	return 0;
59 }
60 
do_specific_alloc(unsigned long size)61 static int do_specific_alloc(unsigned long size)
62 {
63 	void *ptr;
64 
65 	ptr = smalloc(size);
66 	sfree(ptr);
67 	return 0;
68 }
69 
main(int argc,char * argv[])70 int main(int argc, char *argv[])
71 {
72 	sinit();
73 	debug_init();
74 
75 	do_rand_allocs();
76 
77 	/* smalloc bug, commit 271067a6 */
78 	do_specific_alloc(671386584);
79 
80 	scleanup();
81 	return 0;
82 }
83