Merge branch 'security-token' of https://github.com/sfc-gh-rnarubin/fio
[fio.git] / t / stest.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <assert.h>
4
5#include "../smalloc.h"
6#include "../flist.h"
7#include "../arch/arch.h"
8#include "debug.h"
9
10#define MAGIC1 0xa9b1c8d2
11#define MAGIC2 0xf0a1e9b3
12
13#define LOOPS 32
14#define MAXSMALLOC 120*1024*1024UL
15#define LARGESMALLOC 128*1024U
16
17struct elem {
18 unsigned int magic1;
19 struct flist_head list;
20 unsigned int magic2;
21 unsigned int size;
22};
23
24static FLIST_HEAD(list);
25
26static int do_rand_allocs(void)
27{
28 unsigned int i, size, nr, rounds = 0, ret = 0;
29 unsigned long total;
30 struct elem *e;
31 bool error;
32 char *c;
33
34 while (rounds++ < LOOPS) {
35#ifdef STEST_SEED
36 srand(MAGIC1);
37#endif
38 error = false;
39 nr = total = 0;
40 while (total < MAXSMALLOC) {
41 size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0)));
42 e = scalloc(1, size);
43 if (!e) {
44 printf("fail at %lu, size %u\n", total, size);
45 ret++;
46 break;
47 }
48
49 c = (char *)e;
50 for (i = 0; i < size; i++) {
51 if (*(c+i) != 0) {
52 printf("buffer not cleared at %lu, size %u\n", total, size);
53 ret++;
54 break;
55 }
56 }
57
58 /* stop the while loop if buffer was not cleared */
59 if (i < size)
60 break;
61
62 e->magic1 = MAGIC1;
63 e->magic2 = MAGIC2;
64 e->size = size;
65 total += size;
66 flist_add_tail(&e->list, &list);
67 nr++;
68 }
69
70 printf("Got items: %u\n", nr);
71
72 while (!flist_empty(&list)) {
73 e = flist_entry(list.next, struct elem, list);
74 assert(e->magic1 == MAGIC1);
75 assert(e->magic2 == MAGIC2);
76 total -= e->size;
77 flist_del(&e->list);
78 sfree(e);
79
80 if (!error) {
81 e = scalloc(1, LARGESMALLOC);
82 if (!e) {
83 ret++;
84 printf("failure allocating %u bytes at %lu allocated during sfree phase\n",
85 LARGESMALLOC, total);
86 break;
87 }
88
89 c = (char *)e;
90 for (i = 0; i < LARGESMALLOC; i++) {
91 if (*(c+i) != 0) {
92 error = true;
93 ret++;
94 printf("large buffer not cleared at %lu, size %u\n", total, size);
95 break;
96 }
97 }
98
99 sfree(e);
100 }
101 }
102 }
103
104 return ret;
105}
106
107int main(int argc, char *argv[])
108{
109 int ret;
110
111 arch_init(argv);
112 sinit();
113 debug_init();
114
115 ret = do_rand_allocs();
116 smalloc_debug(0); /* TODO: check that free and total blocks
117 ** match */
118
119 scleanup();
120 return ret;
121}