annotate src/fftw-3.3.5/libbench2/util.c @ 84:08ae793730bd

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