annotate src/fftw-3.3.5/libbench2/util.c @ 169:223a55898ab9 tip default

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