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