supernova: allocators - fix construct method
[supercollider.git] / external_libraries / TLSF-2.4.6 / examples / test1.c
blobda2c9cee3bdec1d57f3a1fa2bd6d2ca1fb331121
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <math.h>
5 #include <time.h>
6 #include <sys/time.h>
7 #include <limits.h>
8 #include <string.h>
9 #include "tlsf.h"
11 #define NUM_MALLOC (10000)
12 #define SIZE_MALLOC (10000)
13 #define POOL_SIZE (NUM_MALLOC*SIZE_MALLOC)
15 #define FILL_MEM (0)
17 typedef struct
19 const char *name;
20 double min;
21 double max;
22 double n;
23 double sum;
24 double sumquad;
25 } minmax;
27 static double
28 getcurtime (void)
30 struct timeval tv;
31 struct timezone tz;
33 gettimeofday (&tv, &tz);
34 return ((tv.tv_usec / 1000000.0) + (1.0 * tv.tv_sec));
37 static void
38 init (minmax * data)
40 data->name = "";
41 data->min = 1e37;
42 data->max = 0.0;
43 data->n = 0.0;
44 data->sum = 0.0;
45 data->sumquad = 0.0;
48 static void
49 update (int print, const char *n, double s, double e, minmax * data)
51 double d = e - s;
52 double mean, sigma;
54 if (n) {
55 data->name = n;
56 data->n++;
57 data->sum += d;
58 data->sumquad += d * d;
59 mean = data->sum / data->n;
60 sigma = sqrt ((data->sumquad - ((data->sum * data->sum) / data->n)) /
61 (data->n));
62 if (d < data->min) {
63 data->min = d;
65 if (d > data->max) {
66 data->max = d;
69 else {
70 mean = data->sum / data->n;
71 sigma = sqrt ((data->sumquad - ((data->sum * data->sum) / data->n)) /
72 (data->n));
74 if (print) {
75 printf ("%s min=%.9f, max=%.9f, mean=%.9f, sigma=%.9f\n",
76 n ? n : data->name, data->min, data->max, mean, sigma);
77 if (n == NULL) {
78 data->min = 1e37;
79 data->max = 0.0;
84 static char pool[POOL_SIZE];
86 int
87 main (void)
89 int i, free_mem;
90 int j;
91 size_t t;
92 int n = 0;
93 double s, first, last_h;
94 void **m;
95 minmax maldata, raldata, freedata;
97 free_mem = init_memory_pool (POOL_SIZE, pool);
98 printf ("Total free memory = %d\n", free_mem);
99 init (&maldata);
100 init (&raldata);
101 init (&freedata);
102 m = (void **) tlsf_malloc (NUM_MALLOC * sizeof (void *));
103 for (i = 0; i < NUM_MALLOC; i++) {
104 m[i] = NULL;
106 first = last_h = getcurtime ();
107 for (i = 0; i < NUM_MALLOC; i++) {
108 t = (size_t) (1 + drand48 () * SIZE_MALLOC);
109 m[i] = tlsf_calloc (t, 1);
110 if (((unsigned long) m[i] & (sizeof(void *) * 2 - 1)) != 0) {
111 fprintf(stderr,"Alignment error %p\n", m[i]);
113 #if FILL_MEM
114 memset (m[i], -1, t);
115 #endif
117 for (j = 0; j < 1000; j++) {
118 for (i = 0; i < NUM_MALLOC; i++) {
119 if (m[i]) {
120 t = (size_t) (1 + drand48 () * SIZE_MALLOC);
121 s = getcurtime ();
122 m[i] = tlsf_realloc (m[i], t);
123 update (0, "realloc", s, getcurtime (), &raldata);
124 if (((unsigned long) m[i] & (sizeof(void *) * 2 - 1)) != 0) {
125 fprintf(stderr,"Alignment error %p\n", m[i]);
127 #if FILL_MEM
128 memset (m[i], -1, t);
129 #endif
131 if (m[i]) {
132 s = getcurtime ();
133 tlsf_free (m[i]);
134 update (0, "free ", s, getcurtime (), &freedata);
136 t = (size_t) (1 + drand48 () * SIZE_MALLOC);
137 s = getcurtime ();
138 m[i] = tlsf_malloc (t);
139 update (0, "malloc ", s, getcurtime (), &maldata);
140 if (((unsigned long) m[i] & (sizeof(void *) * 2 - 1)) != 0) {
141 fprintf(stderr,"Alignment error %p\n", m[i]);
143 #if FILL_MEM
144 memset (m[i], -1, t);
145 #endif
147 n++;
148 s = getcurtime ();
149 if ((s - last_h) > 10) {
150 last_h = s;
151 printf ("Count = %d %f\n",
152 n * NUM_MALLOC, last_h - first);
153 update (1, NULL, 0.0, getcurtime (), &maldata);
154 update (1, NULL, 0.0, getcurtime (), &raldata);
155 update (1, NULL, 0.0, getcurtime (), &freedata);
158 for (i = 0; i < NUM_MALLOC; i++) {
159 tlsf_free (m[i]);
161 tlsf_free (m);
162 return 0;