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