fnmatch: fix "[/b" test
[libc-test] / src / malloc / bench.c
index 28ae66d..030e5ee 100644 (file)
@@ -4,72 +4,92 @@
 #include <pthread.h>
 #include "test.h"
 
-void bench_malloc_sparse() {
-       void *p[N];
-       size_t i;
-       for (i=0; i<sizeof p/sizeof *p; i++) {
-               p[i] = malloc(4000);
-               memset(p[i], 0, 4000);
+enum { Len = 1000 };
+
+void bench_malloc_sparse(int N) {
+       void *p[Len];
+       int i,j;
+
+       for (j = 0; j < N; j++) {
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       p[i] = malloc(4000);
+                       memset(p[i], 0, 4000);
+               }
+               for (i=0; i<sizeof p/sizeof *p; i++)
+                       if (i%150) free(p[i]);
+               for (i=0; i<sizeof p/sizeof *p; i+=150)
+                       free(p[i]);
        }
-       for (i=0; i<sizeof p/sizeof *p; i++)
-               if (i%150) free(p[i]);
 }
 
-void bench_malloc_bubble() {
-       void *p[N];
-       size_t i;
-       for (i=0; i<sizeof p/sizeof *p; i++) {
-               p[i] = malloc(4000);
-               memset(p[i], 0, 4000);
+void bench_malloc_bubble(int N) {
+       void *p[Len];
+       int i,j;
+
+       for (j = 0; j < N; j++) {
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       p[i] = malloc(4000);
+                       memset(p[i], 0, 4000);
+               }
+               for (i=0; i<sizeof p/sizeof *p; i++)
+                       free(p[i]);
        }
-       for (i=0; i<sizeof p/sizeof *p-1; i++)
-               free(p[i]);
 }
 
-void bench_malloc_tiny1() {
-       void **p = malloc(N * sizeof *p);
-       size_t i;
-       for (i=0; i<N; i++) {
-               p[i] = malloc((i%4+1)*16);
-       }
-       for (i=0; i<N; i++) {
-               free(p[i]);
+void bench_malloc_tiny1(int N) {
+       void *p[Len];
+       int i,j;
+
+       for (j=0; j<N; j++) {
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       p[i] = malloc((i%4+1)*16);
+               }
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       free(p[i]);
+               }
        }
-       free(p);
 }
 
-void bench_malloc_tiny2() {
-       void **p = malloc(N * sizeof *p);
-       size_t i;
-       for (i=0; i<N; i++) {
-               p[i] = malloc((i%4+1)*16);
+void bench_malloc_tiny2(int N) {
+       void *p[Len];
+       int i,j;
+
+       for (j=0; j<N; j++) {
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       p[i] = malloc((i%4+1)*16);
+               }
+               for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
+                       free(p[i]);
+               free(p[0]);
        }
-       if (N>1) for (i=1; i; i = (i+1999)%N)
-               free(p[i]);
-       free(p[0]);
-       free(p);
 }
 
-void bench_malloc_big1() {
-       void *p[N];
-       size_t i;
-       for (i=0; i<sizeof p/sizeof *p; i++) {
-               p[i] = malloc((i%4+1)*16384);
-       }
-       for (i=0; i<sizeof p/sizeof *p; i++) {
-               free(p[i]);
+void bench_malloc_big1(int N) {
+       void *p[Len];
+       int i,j;
+
+       for (j = 0;  j < N; j++) {
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       p[i] = malloc((i%4+1)*16384);
+               }
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       free(p[i]);
+               }
        }
 }
 
-void bench_malloc_big2() {
-       void *p[N];
-       size_t i;
-       for (i=0; i<sizeof p/sizeof *p; i++) {
-               p[i] = malloc((i%4+1)*16384);
+void bench_malloc_big2(int N) {
+       void *p[Len];
+       int i,j;
+
+       for (j = 0; j < N; j++) {
+               for (i=0; i<sizeof p/sizeof *p; i++) {
+                       p[i] = malloc((i%4+1)*16384);
+               }
+               for (i=1; i; i = (i+57)%(sizeof p/sizeof *p))
+                       free(p[i]);
+               free(p[0]);
        }
-       if (N>1) for (i=1; i; i = (i+1999)%(sizeof p/sizeof *p))
-               free(p[i]);
-       free(p[0]);
 }
 
 
@@ -88,6 +108,7 @@ static unsigned rng(unsigned *r)
        return *r = *r * 1103515245 + 12345;
 }
 
+static int N;
 
 static void *stress(void *arg)
 {
@@ -116,26 +137,27 @@ static void *stress(void *arg)
        return 0;
 }
 
-void bench_malloc_thread_stress() {
+void bench_malloc_thread_stress(int n) {
        struct foo foo[SH_COUNT] = {{0}};
        pthread_t td1, td2;
        void *res;
 
+       N = n;
        pthread_create(&td1, 0, stress, foo);
        pthread_create(&td2, 0, stress, foo);
        pthread_join(td1, &res);
        pthread_join(td2, &res);
 }
 
-void bench_malloc_thread_local() {
+void bench_malloc_thread_local(int n) {
        struct foo foo1[SH_COUNT] = {{0}};
        struct foo foo2[SH_COUNT] = {{0}};
        pthread_t td1, td2;
        void *res;
 
+       N = n;
        pthread_create(&td1, 0, stress, foo1);
        pthread_create(&td2, 0, stress, foo2);
        pthread_join(td1, &res);
        pthread_join(td2, &res);
 }
-