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