annotate src/fftw-3.3.3/libbench2/util.c @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents 37bf6b4a2645
children
rev   line source
Chris@10 1 /*
Chris@10 2 * Copyright (c) 2000 Matteo Frigo
Chris@10 3 * Copyright (c) 2000 Massachusetts Institute of Technology
Chris@10 4 *
Chris@10 5 * This program is free software; you can redistribute it and/or modify
Chris@10 6 * it under the terms of the GNU General Public License as published by
Chris@10 7 * the Free Software Foundation; either version 2 of the License, or
Chris@10 8 * (at your option) any later version.
Chris@10 9 *
Chris@10 10 * This program is distributed in the hope that it will be useful,
Chris@10 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@10 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@10 13 * GNU General Public License for more details.
Chris@10 14 *
Chris@10 15 * You should have received a copy of the GNU General Public License
Chris@10 16 * along with this program; if not, write to the Free Software
Chris@10 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@10 18 *
Chris@10 19 */
Chris@10 20
Chris@10 21 #include "bench.h"
Chris@10 22 #include <stdlib.h>
Chris@10 23 #include <stdio.h>
Chris@10 24 #include <stddef.h>
Chris@10 25 #include <math.h>
Chris@10 26
Chris@10 27 #if defined(HAVE_DECL_MEMALIGN) && !HAVE_DECL_MEMALIGN
Chris@10 28 # if defined(HAVE_MALLOC_H)
Chris@10 29 # include <malloc.h>
Chris@10 30 # else
Chris@10 31 extern void *memalign(size_t, size_t);
Chris@10 32 # endif
Chris@10 33 #endif
Chris@10 34
Chris@10 35 #if defined(HAVE_DECL_POSIX_MEMALIGN) && !HAVE_DECL_POSIX_MEMALIGN
Chris@10 36 extern int posix_memalign(void **, size_t, size_t);
Chris@10 37 #endif
Chris@10 38
Chris@10 39 void bench_assertion_failed(const char *s, int line, const char *file)
Chris@10 40 {
Chris@10 41 ovtpvt_err("bench: %s:%d: assertion failed: %s\n", file, line, s);
Chris@10 42 bench_exit(EXIT_FAILURE);
Chris@10 43 }
Chris@10 44
Chris@10 45 #ifdef HAVE_DRAND48
Chris@10 46 # if defined(HAVE_DECL_DRAND48) && !HAVE_DECL_DRAND48
Chris@10 47 extern double drand48(void);
Chris@10 48 # endif
Chris@10 49 double bench_drand(void)
Chris@10 50 {
Chris@10 51 return drand48() - 0.5;
Chris@10 52 }
Chris@10 53 # if defined(HAVE_DECL_SRAND48) && !HAVE_DECL_SRAND48
Chris@10 54 extern void srand48(long);
Chris@10 55 # endif
Chris@10 56 void bench_srand(int seed)
Chris@10 57 {
Chris@10 58 srand48(seed);
Chris@10 59 }
Chris@10 60 #else
Chris@10 61 double bench_drand(void)
Chris@10 62 {
Chris@10 63 double d = rand();
Chris@10 64 return (d / (double) RAND_MAX) - 0.5;
Chris@10 65 }
Chris@10 66 void bench_srand(int seed)
Chris@10 67 {
Chris@10 68 srand(seed);
Chris@10 69 }
Chris@10 70 #endif
Chris@10 71
Chris@10 72 /**********************************************************
Chris@10 73 * DEBUGGING CODE
Chris@10 74 **********************************************************/
Chris@10 75 #ifdef BENCH_DEBUG
Chris@10 76 static int bench_malloc_cnt = 0;
Chris@10 77
Chris@10 78 /*
Chris@10 79 * debugging malloc/free. Initialize every malloced and freed area to
Chris@10 80 * random values, just to make sure we are not using uninitialized
Chris@10 81 * pointers. Also check for writes past the ends of allocated blocks,
Chris@10 82 * and a couple of other things.
Chris@10 83 *
Chris@10 84 * This code is a quick and dirty hack -- use at your own risk.
Chris@10 85 */
Chris@10 86
Chris@10 87 static int bench_malloc_total = 0, bench_malloc_max = 0, bench_malloc_cnt_max = 0;
Chris@10 88
Chris@10 89 #define MAGIC ((size_t)0xABadCafe)
Chris@10 90 #define PAD_FACTOR 2
Chris@10 91 #define TWO_SIZE_T (2 * sizeof(size_t))
Chris@10 92
Chris@10 93 #define VERBOSE_ALLOCATION 0
Chris@10 94
Chris@10 95 #if VERBOSE_ALLOCATION
Chris@10 96 #define WHEN_VERBOSE(a) a
Chris@10 97 #else
Chris@10 98 #define WHEN_VERBOSE(a)
Chris@10 99 #endif
Chris@10 100
Chris@10 101 void *bench_malloc(size_t n)
Chris@10 102 {
Chris@10 103 char *p;
Chris@10 104 size_t i;
Chris@10 105
Chris@10 106 bench_malloc_total += n;
Chris@10 107
Chris@10 108 if (bench_malloc_total > bench_malloc_max)
Chris@10 109 bench_malloc_max = bench_malloc_total;
Chris@10 110
Chris@10 111 p = (char *) malloc(PAD_FACTOR * n + TWO_SIZE_T);
Chris@10 112 BENCH_ASSERT(p);
Chris@10 113
Chris@10 114 /* store the size in a known position */
Chris@10 115 ((size_t *) p)[0] = n;
Chris@10 116 ((size_t *) p)[1] = MAGIC;
Chris@10 117 for (i = 0; i < PAD_FACTOR * n; i++)
Chris@10 118 p[i + TWO_SIZE_T] = (char) (i ^ 0xDEADBEEF);
Chris@10 119
Chris@10 120 ++bench_malloc_cnt;
Chris@10 121
Chris@10 122 if (bench_malloc_cnt > bench_malloc_cnt_max)
Chris@10 123 bench_malloc_cnt_max = bench_malloc_cnt;
Chris@10 124
Chris@10 125 /* skip the size we stored previously */
Chris@10 126 return (void *) (p + TWO_SIZE_T);
Chris@10 127 }
Chris@10 128
Chris@10 129 void bench_free(void *p)
Chris@10 130 {
Chris@10 131 char *q;
Chris@10 132
Chris@10 133 BENCH_ASSERT(p);
Chris@10 134
Chris@10 135 q = ((char *) p) - TWO_SIZE_T;
Chris@10 136 BENCH_ASSERT(q);
Chris@10 137
Chris@10 138 {
Chris@10 139 size_t n = ((size_t *) q)[0];
Chris@10 140 size_t magic = ((size_t *) q)[1];
Chris@10 141 size_t i;
Chris@10 142
Chris@10 143 ((size_t *) q)[0] = 0; /* set to zero to detect duplicate free's */
Chris@10 144
Chris@10 145 BENCH_ASSERT(magic == MAGIC);
Chris@10 146 ((size_t *) q)[1] = ~MAGIC;
Chris@10 147
Chris@10 148 bench_malloc_total -= n;
Chris@10 149 BENCH_ASSERT(bench_malloc_total >= 0);
Chris@10 150
Chris@10 151 /* check for writing past end of array: */
Chris@10 152 for (i = n; i < PAD_FACTOR * n; ++i)
Chris@10 153 if (q[i + TWO_SIZE_T] != (char) (i ^ 0xDEADBEEF)) {
Chris@10 154 BENCH_ASSERT(0 /* array bounds overwritten */);
Chris@10 155 }
Chris@10 156 for (i = 0; i < PAD_FACTOR * n; ++i)
Chris@10 157 q[i + TWO_SIZE_T] = (char) (i ^ 0xBEEFDEAD);
Chris@10 158
Chris@10 159 --bench_malloc_cnt;
Chris@10 160
Chris@10 161 BENCH_ASSERT(bench_malloc_cnt >= 0);
Chris@10 162
Chris@10 163 BENCH_ASSERT(
Chris@10 164 (bench_malloc_cnt == 0 && bench_malloc_total == 0) ||
Chris@10 165 (bench_malloc_cnt > 0 && bench_malloc_total > 0));
Chris@10 166
Chris@10 167 free(q);
Chris@10 168 }
Chris@10 169 }
Chris@10 170
Chris@10 171 #else
Chris@10 172 /**********************************************************
Chris@10 173 * NON DEBUGGING CODE
Chris@10 174 **********************************************************/
Chris@10 175 /* production version, no hacks */
Chris@10 176
Chris@10 177 #define MIN_ALIGNMENT 128 /* must be power of two */
Chris@10 178
Chris@10 179 #define real_free free /* memalign and malloc use ordinary free */
Chris@10 180
Chris@10 181 void *bench_malloc(size_t n)
Chris@10 182 {
Chris@10 183 void *p;
Chris@10 184 if (n == 0) n = 1;
Chris@10 185
Chris@10 186 #if defined(WITH_OUR_MALLOC)
Chris@10 187 /* Our own aligned malloc/free. Assumes sizeof(void*) is
Chris@10 188 a power of two <= 8 and that malloc is at least
Chris@10 189 sizeof(void*)-aligned. Assumes size_t = uintptr_t. */
Chris@10 190 {
Chris@10 191 void *p0;
Chris@10 192 if ((p0 = malloc(n + MIN_ALIGNMENT))) {
Chris@10 193 p = (void *) (((size_t) p0 + MIN_ALIGNMENT) & (~((size_t) (MIN_ALIGNMENT - 1))));
Chris@10 194 *((void **) p - 1) = p0;
Chris@10 195 }
Chris@10 196 else
Chris@10 197 p = (void *) 0;
Chris@10 198 }
Chris@10 199 #elif defined(HAVE_MEMALIGN)
Chris@10 200 p = memalign(MIN_ALIGNMENT, n);
Chris@10 201 #elif defined(HAVE_POSIX_MEMALIGN)
Chris@10 202 /* note: posix_memalign is broken in glibc 2.2.5: it constrains
Chris@10 203 the size, not the alignment, to be (power of two) * sizeof(void*).
Chris@10 204 The bug seems to have been fixed as of glibc 2.3.1. */
Chris@10 205 if (posix_memalign(&p, MIN_ALIGNMENT, n))
Chris@10 206 p = (void*) 0;
Chris@10 207 #elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC)
Chris@10 208 /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */
Chris@10 209 p = (void *) _mm_malloc(n, MIN_ALIGNMENT);
Chris@10 210 # undef real_free
Chris@10 211 # define real_free _mm_free
Chris@10 212 #else
Chris@10 213 p = malloc(n);
Chris@10 214 #endif
Chris@10 215
Chris@10 216 BENCH_ASSERT(p);
Chris@10 217 return p;
Chris@10 218 }
Chris@10 219
Chris@10 220 void bench_free(void *p)
Chris@10 221 {
Chris@10 222 #ifdef WITH_OUR_MALLOC
Chris@10 223 if (p) free(*((void **) p - 1));
Chris@10 224 #else
Chris@10 225 real_free(p);
Chris@10 226 #endif
Chris@10 227 }
Chris@10 228
Chris@10 229 #endif
Chris@10 230
Chris@10 231 void bench_free0(void *p)
Chris@10 232 {
Chris@10 233 if (p) bench_free(p);
Chris@10 234 }