njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Malloc replacement. replacemalloc_core.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2007 Julian Seward |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 32 | #include "pub_core_libcbase.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 33 | #include "pub_core_libcprint.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 34 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 35 | #include "pub_core_options.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 36 | #include "pub_core_replacemalloc.h" |
| 37 | |
| 38 | /*------------------------------------------------------------*/ |
| 39 | /*--- Command line options ---*/ |
| 40 | /*------------------------------------------------------------*/ |
| 41 | |
| 42 | /* Nb: the allocator always rounds blocks up to a multiple of |
| 43 | VG_MIN_MALLOC_SZB. |
| 44 | */ |
| 45 | |
| 46 | /* DEBUG: print malloc details? default: NO */ |
| 47 | Bool VG_(clo_trace_malloc) = False; |
| 48 | |
| 49 | /* Minimum alignment in functions that don't specify alignment explicitly. |
| 50 | default: 0, i.e. use VG_MIN_MALLOC_SZB. */ |
| 51 | UInt VG_(clo_alignment) = VG_MIN_MALLOC_SZB; |
| 52 | |
| 53 | |
| 54 | Bool VG_(replacement_malloc_process_cmd_line_option)(Char* arg) |
| 55 | { |
| 56 | if (VG_CLO_STREQN(12, arg, "--alignment=")) { |
| 57 | VG_(clo_alignment) = (UInt)VG_(atoll)(&arg[12]); |
| 58 | |
| 59 | if (VG_(clo_alignment) < VG_MIN_MALLOC_SZB |
| 60 | || VG_(clo_alignment) > 4096 |
| 61 | || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) { |
| 62 | VG_(message)(Vg_UserMsg, ""); |
| 63 | VG_(message)(Vg_UserMsg, |
| 64 | "Invalid --alignment= setting. " |
| 65 | "Should be a power of 2, >= %d, <= 4096.", VG_MIN_MALLOC_SZB); |
sewardj | 6893d65 | 2006-10-15 01:25:13 +0000 | [diff] [blame] | 66 | VG_(err_bad_option)("--alignment"); |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | else VG_BOOL_CLO(arg, "--trace-malloc", VG_(clo_trace_malloc)) |
| 71 | else |
| 72 | return False; |
| 73 | |
| 74 | return True; |
| 75 | } |
| 76 | |
| 77 | void VG_(replacement_malloc_print_usage)(void) |
| 78 | { |
| 79 | VG_(printf)( |
tom | 06beb38 | 2005-11-04 17:18:18 +0000 | [diff] [blame] | 80 | " --alignment=<number> set minimum alignment of allocations [%d]\n", |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 81 | VG_MIN_MALLOC_SZB |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | void VG_(replacement_malloc_print_debug_usage)(void) |
| 86 | { |
| 87 | VG_(printf)( |
| 88 | " --trace-malloc=no|yes show client malloc details? [no]\n" |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /*------------------------------------------------------------*/ |
| 93 | /*--- Useful functions ---*/ |
| 94 | /*------------------------------------------------------------*/ |
| 95 | |
| 96 | void* VG_(cli_malloc) ( SizeT align, SizeT nbytes ) |
| 97 | { |
| 98 | // 'align' should be valid (ie. big enough and a power of two) by now. |
| 99 | // VG_(arena_memalign)() will abort if it's not. |
| 100 | if (VG_MIN_MALLOC_SZB == align) |
| 101 | return VG_(arena_malloc) ( VG_AR_CLIENT, nbytes ); |
| 102 | else |
| 103 | return VG_(arena_memalign) ( VG_AR_CLIENT, align, nbytes ); |
| 104 | } |
| 105 | |
| 106 | void VG_(cli_free) ( void* p ) |
| 107 | { |
| 108 | VG_(arena_free) ( VG_AR_CLIENT, p ); |
| 109 | } |
| 110 | |
| 111 | Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size, SizeT rz_szB ) |
| 112 | { |
| 113 | return ( start - rz_szB <= a && a < start + size + rz_szB ); |
| 114 | } |
| 115 | |
| 116 | /*--------------------------------------------------------------------*/ |
| 117 | /*--- end ---*/ |
| 118 | /*--------------------------------------------------------------------*/ |