From fbc2792b20276f7fa14c44f7b235ca448b383b50 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 16 Aug 2011 09:56:27 +0200 Subject: [PATCH] Add simple stest smalloc tester Signed-off-by: Jens Axboe --- Makefile | 3 ++ t/stest.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 t/stest.c diff --git a/Makefile b/Makefile index 40d47d5e..3831ab8d 100644 --- a/Makefile +++ b/Makefile @@ -80,6 +80,9 @@ all: .depend $(PROGS) $(SCRIPTS) .c.o: .depend $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $(CPPFLAGS) $< +t/stest: t/stest.o smalloc.o mutex.o + $(QUIET_CC)$(CC) $(LDFLAGS) $(CFLAGS) -o $@ t/stest.o smalloc.o mutex.o $(LIBS) $(LDFLAGS) + fio: $(OBJS) $(QUIET_CC)$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJS) $(LIBS) $(LDFLAGS) diff --git a/t/stest.c b/t/stest.c new file mode 100644 index 00000000..c1794843 --- /dev/null +++ b/t/stest.c @@ -0,0 +1,84 @@ +#include +#include +#include + +#include "../smalloc.h" +#include "../flist.h" + +FILE *f_err; + +#define MAGIC1 0xa9b1c8d2 +#define MAGIC2 0xf0a1e9b3 + +#define LOOPS 32 + +struct elem { + unsigned int magic1; + struct flist_head list; + unsigned int magic2; +}; + +FLIST_HEAD(list); + +static int do_rand_allocs(void) +{ + unsigned int size, nr, rounds = 0; + unsigned long total; + struct elem *e; + + while (rounds++ < LOOPS) { +#ifdef STEST_SEED + srand(MAGIC1); +#endif + nr = total = 0; + while (total < 128*1024*1024UL) { + size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0))); + e = smalloc(size); + if (!e) { + printf("fail at %lu, size %u\n", total, size); + break; + } + e->magic1 = MAGIC1; + e->magic2 = MAGIC2; + total += size; + flist_add_tail(&e->list, &list); + nr++; + } + + printf("Got items: %u\n", nr); + + while (!flist_empty(&list)) { + e = flist_entry(list.next, struct elem, list); + assert(e->magic1 == MAGIC1); + assert(e->magic2 == MAGIC2); + flist_del(&e->list); + sfree(e); + } + } + + return 0; +} + +static int do_specific_alloc(unsigned long size) +{ + void *ptr; + + ptr = smalloc(size); + sfree(ptr); + return 0; +} + +int main(int argc, char *argv[]) +{ + f_err = stderr; + + sinit(); + + do_rand_allocs(); + + /* smalloc bug, commit 271067a6 */ + do_specific_alloc(671386584); + + scleanup(); + return 0; +} -- 2.25.1