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