cannam@95: /* cannam@95: * Copyright (c) 2000 Matteo Frigo cannam@95: * Copyright (c) 2000 Massachusetts Institute of Technology cannam@95: * cannam@95: * This program is free software; you can redistribute it and/or modify cannam@95: * it under the terms of the GNU General Public License as published by cannam@95: * the Free Software Foundation; either version 2 of the License, or cannam@95: * (at your option) any later version. cannam@95: * cannam@95: * This program is distributed in the hope that it will be useful, cannam@95: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@95: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@95: * GNU General Public License for more details. cannam@95: * cannam@95: * You should have received a copy of the GNU General Public License cannam@95: * along with this program; if not, write to the Free Software cannam@95: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@95: * cannam@95: */ cannam@95: cannam@95: #include "bench.h" cannam@95: #include cannam@95: #include cannam@95: #include cannam@95: #include cannam@95: cannam@95: #if defined(HAVE_DECL_MEMALIGN) && !HAVE_DECL_MEMALIGN cannam@95: # if defined(HAVE_MALLOC_H) cannam@95: # include cannam@95: # else cannam@95: extern void *memalign(size_t, size_t); cannam@95: # endif cannam@95: #endif cannam@95: cannam@95: #if defined(HAVE_DECL_POSIX_MEMALIGN) && !HAVE_DECL_POSIX_MEMALIGN cannam@95: extern int posix_memalign(void **, size_t, size_t); cannam@95: #endif cannam@95: cannam@95: void bench_assertion_failed(const char *s, int line, const char *file) cannam@95: { cannam@95: ovtpvt_err("bench: %s:%d: assertion failed: %s\n", file, line, s); cannam@95: bench_exit(EXIT_FAILURE); cannam@95: } cannam@95: cannam@95: #ifdef HAVE_DRAND48 cannam@95: # if defined(HAVE_DECL_DRAND48) && !HAVE_DECL_DRAND48 cannam@95: extern double drand48(void); cannam@95: # endif cannam@95: double bench_drand(void) cannam@95: { cannam@95: return drand48() - 0.5; cannam@95: } cannam@95: # if defined(HAVE_DECL_SRAND48) && !HAVE_DECL_SRAND48 cannam@95: extern void srand48(long); cannam@95: # endif cannam@95: void bench_srand(int seed) cannam@95: { cannam@95: srand48(seed); cannam@95: } cannam@95: #else cannam@95: double bench_drand(void) cannam@95: { cannam@95: double d = rand(); cannam@95: return (d / (double) RAND_MAX) - 0.5; cannam@95: } cannam@95: void bench_srand(int seed) cannam@95: { cannam@95: srand(seed); cannam@95: } cannam@95: #endif cannam@95: cannam@95: /********************************************************** cannam@95: * DEBUGGING CODE cannam@95: **********************************************************/ cannam@95: #ifdef BENCH_DEBUG cannam@95: static int bench_malloc_cnt = 0; cannam@95: cannam@95: /* cannam@95: * debugging malloc/free. Initialize every malloced and freed area to cannam@95: * random values, just to make sure we are not using uninitialized cannam@95: * pointers. Also check for writes past the ends of allocated blocks, cannam@95: * and a couple of other things. cannam@95: * cannam@95: * This code is a quick and dirty hack -- use at your own risk. cannam@95: */ cannam@95: cannam@95: static int bench_malloc_total = 0, bench_malloc_max = 0, bench_malloc_cnt_max = 0; cannam@95: cannam@95: #define MAGIC ((size_t)0xABadCafe) cannam@95: #define PAD_FACTOR 2 cannam@95: #define TWO_SIZE_T (2 * sizeof(size_t)) cannam@95: cannam@95: #define VERBOSE_ALLOCATION 0 cannam@95: cannam@95: #if VERBOSE_ALLOCATION cannam@95: #define WHEN_VERBOSE(a) a cannam@95: #else cannam@95: #define WHEN_VERBOSE(a) cannam@95: #endif cannam@95: cannam@95: void *bench_malloc(size_t n) cannam@95: { cannam@95: char *p; cannam@95: size_t i; cannam@95: cannam@95: bench_malloc_total += n; cannam@95: cannam@95: if (bench_malloc_total > bench_malloc_max) cannam@95: bench_malloc_max = bench_malloc_total; cannam@95: cannam@95: p = (char *) malloc(PAD_FACTOR * n + TWO_SIZE_T); cannam@95: BENCH_ASSERT(p); cannam@95: cannam@95: /* store the size in a known position */ cannam@95: ((size_t *) p)[0] = n; cannam@95: ((size_t *) p)[1] = MAGIC; cannam@95: for (i = 0; i < PAD_FACTOR * n; i++) cannam@95: p[i + TWO_SIZE_T] = (char) (i ^ 0xDEADBEEF); cannam@95: cannam@95: ++bench_malloc_cnt; cannam@95: cannam@95: if (bench_malloc_cnt > bench_malloc_cnt_max) cannam@95: bench_malloc_cnt_max = bench_malloc_cnt; cannam@95: cannam@95: /* skip the size we stored previously */ cannam@95: return (void *) (p + TWO_SIZE_T); cannam@95: } cannam@95: cannam@95: void bench_free(void *p) cannam@95: { cannam@95: char *q; cannam@95: cannam@95: BENCH_ASSERT(p); cannam@95: cannam@95: q = ((char *) p) - TWO_SIZE_T; cannam@95: BENCH_ASSERT(q); cannam@95: cannam@95: { cannam@95: size_t n = ((size_t *) q)[0]; cannam@95: size_t magic = ((size_t *) q)[1]; cannam@95: size_t i; cannam@95: cannam@95: ((size_t *) q)[0] = 0; /* set to zero to detect duplicate free's */ cannam@95: cannam@95: BENCH_ASSERT(magic == MAGIC); cannam@95: ((size_t *) q)[1] = ~MAGIC; cannam@95: cannam@95: bench_malloc_total -= n; cannam@95: BENCH_ASSERT(bench_malloc_total >= 0); cannam@95: cannam@95: /* check for writing past end of array: */ cannam@95: for (i = n; i < PAD_FACTOR * n; ++i) cannam@95: if (q[i + TWO_SIZE_T] != (char) (i ^ 0xDEADBEEF)) { cannam@95: BENCH_ASSERT(0 /* array bounds overwritten */); cannam@95: } cannam@95: for (i = 0; i < PAD_FACTOR * n; ++i) cannam@95: q[i + TWO_SIZE_T] = (char) (i ^ 0xBEEFDEAD); cannam@95: cannam@95: --bench_malloc_cnt; cannam@95: cannam@95: BENCH_ASSERT(bench_malloc_cnt >= 0); cannam@95: cannam@95: BENCH_ASSERT( cannam@95: (bench_malloc_cnt == 0 && bench_malloc_total == 0) || cannam@95: (bench_malloc_cnt > 0 && bench_malloc_total > 0)); cannam@95: cannam@95: free(q); cannam@95: } cannam@95: } cannam@95: cannam@95: #else cannam@95: /********************************************************** cannam@95: * NON DEBUGGING CODE cannam@95: **********************************************************/ cannam@95: /* production version, no hacks */ cannam@95: cannam@95: #define MIN_ALIGNMENT 128 /* must be power of two */ cannam@95: cannam@95: #define real_free free /* memalign and malloc use ordinary free */ cannam@95: cannam@95: void *bench_malloc(size_t n) cannam@95: { cannam@95: void *p; cannam@95: if (n == 0) n = 1; cannam@95: cannam@95: #if defined(WITH_OUR_MALLOC) cannam@95: /* Our own aligned malloc/free. Assumes sizeof(void*) is cannam@95: a power of two <= 8 and that malloc is at least cannam@95: sizeof(void*)-aligned. Assumes size_t = uintptr_t. */ cannam@95: { cannam@95: void *p0; cannam@95: if ((p0 = malloc(n + MIN_ALIGNMENT))) { cannam@95: p = (void *) (((size_t) p0 + MIN_ALIGNMENT) & (~((size_t) (MIN_ALIGNMENT - 1)))); cannam@95: *((void **) p - 1) = p0; cannam@95: } cannam@95: else cannam@95: p = (void *) 0; cannam@95: } cannam@95: #elif defined(HAVE_MEMALIGN) cannam@95: p = memalign(MIN_ALIGNMENT, n); cannam@95: #elif defined(HAVE_POSIX_MEMALIGN) cannam@95: /* note: posix_memalign is broken in glibc 2.2.5: it constrains cannam@95: the size, not the alignment, to be (power of two) * sizeof(void*). cannam@95: The bug seems to have been fixed as of glibc 2.3.1. */ cannam@95: if (posix_memalign(&p, MIN_ALIGNMENT, n)) cannam@95: p = (void*) 0; cannam@95: #elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC) cannam@95: /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */ cannam@95: p = (void *) _mm_malloc(n, MIN_ALIGNMENT); cannam@95: # undef real_free cannam@95: # define real_free _mm_free cannam@95: #else cannam@95: p = malloc(n); cannam@95: #endif cannam@95: cannam@95: BENCH_ASSERT(p); cannam@95: return p; cannam@95: } cannam@95: cannam@95: void bench_free(void *p) cannam@95: { cannam@95: #ifdef WITH_OUR_MALLOC cannam@95: if (p) free(*((void **) p - 1)); cannam@95: #else cannam@95: real_free(p); cannam@95: #endif cannam@95: } cannam@95: cannam@95: #endif cannam@95: cannam@95: void bench_free0(void *p) cannam@95: { cannam@95: if (p) bench_free(p); cannam@95: }