Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / external_libraries / TLSF-2.4.6 / examples / test2.c
blob7c1c5b9945b3ec8fc89b024048ada7b3638fe9bf
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 <malloc.h>
11 #define NUM_MALLOC (10000)
12 #define SIZE_MALLOC (10000)
14 #define FILL_MEM (0)
16 typedef struct
18 const char *name;
19 double min;
20 double max;
21 double n;
22 double sum;
23 double sumquad;
24 } minmax;
26 static double
27 getcurtime (void)
29 struct timeval tv;
30 struct timezone tz;
32 gettimeofday (&tv, &tz);
33 return ((tv.tv_usec / 1000000.0) + (1.0 * tv.tv_sec));
36 static void
37 init (minmax * data)
39 data->name = "";
40 data->min = 1e37;
41 data->max = 0.0;
42 data->n = 0.0;
43 data->sum = 0.0;
44 data->sumquad = 0.0;
47 static void
48 update (int print, const char *n, double s, double e, minmax * data)
50 double d = e - s;
51 double mean, sigma;
53 if (n) {
54 data->name = n;
55 data->n++;
56 data->sum += d;
57 data->sumquad += d * d;
58 mean = data->sum / data->n;
59 sigma = sqrt ((data->sumquad - ((data->sum * data->sum) / data->n)) /
60 (data->n));
61 if (d < data->min) {
62 data->min = d;
64 if (d > data->max) {
65 data->max = d;
68 else {
69 mean = data->sum / data->n;
70 sigma = sqrt ((data->sumquad - ((data->sum * data->sum) / data->n)) /
71 (data->n));
73 if (print) {
74 printf ("%s min=%.9f, max=%.9f, mean=%.9f, sigma=%.9f\n",
75 n ? n : data->name, data->min, data->max, mean, sigma);
76 if (n == NULL) {
77 data->min = 1e37;
78 data->max = 0.0;
83 int
84 main (void)
86 int i;
87 int j;
88 size_t t;
89 int n = 0;
90 int max_size = 0;
91 double s, first, last_h;
92 void **m;
93 minmax maldata, raldata, freedata;
95 init (&maldata);
96 init (&raldata);
97 init (&freedata);
98 m = (void **) malloc (NUM_MALLOC * sizeof (void *));
99 for (i = 0; i < NUM_MALLOC; i++) {
100 m[i] = NULL;
102 first = last_h = getcurtime ();
103 for (i = 0; i < NUM_MALLOC; i++) {
104 t = (size_t) (1 + drand48 () * SIZE_MALLOC);
105 m[i] = calloc (t, 1);
106 if (((unsigned long) m[i] & (sizeof(void *) * 2 - 1)) != 0) {
107 fprintf(stderr,"Alignment error %p\n", m[i]);
109 #if FILL_MEM
110 memset (m[i], -1, t);
111 #endif
113 for (j = 0; j < 1000; j++) {
114 for (i = 0; i < NUM_MALLOC; i++) {
115 if (m[i]) {
116 t = (size_t) (1 + drand48 () * SIZE_MALLOC);
117 s = getcurtime ();
118 m[i] = realloc (m[i], t);
119 update (0, "realloc", s, getcurtime (), &raldata);
120 if (((unsigned long) m[i] & (sizeof(void *) * 2 - 1)) != 0) {
121 fprintf(stderr,"Alignment error %p\n", m[i]);
123 #if FILL_MEM
124 memset (m[i], -1, t);
125 #endif
127 if (m[i]) {
128 s = getcurtime ();
129 free (m[i]);
130 update (0, "free ", s, getcurtime (), &freedata);
132 t = (size_t) (1 + drand48 () * SIZE_MALLOC);
133 s = getcurtime ();
134 m[i] = malloc (t);
135 update (0, "malloc ", s, getcurtime (), &maldata);
136 if (((unsigned long) m[i] & (sizeof(void *) * 2 - 1)) != 0) {
137 fprintf(stderr,"Alignment error %p\n", m[i]);
139 #if FILL_MEM
140 memset (m[i], -1, t);
141 #endif
143 if (mallinfo().uordblks > max_size) {
144 max_size = mallinfo().uordblks;
146 n++;
147 s = getcurtime ();
148 if ((s - last_h) > 10) {
149 last_h = s;
150 printf ("Count = %d %f, max memory %d\n",
151 n * NUM_MALLOC, last_h - first, mallinfo().uordblks);
152 update (1, NULL, 0.0, getcurtime (), &maldata);
153 update (1, NULL, 0.0, getcurtime (), &raldata);
154 update (1, NULL, 0.0, getcurtime (), &freedata);
157 for (i = 0; i < NUM_MALLOC; i++) {
158 free (m[i]);
160 free (m);
161 printf ("Total used memory = %d, max memory %d\n",
162 mallinfo().uordblks, max_size);
163 return 0;