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 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 10 | Copyright (C) 2000-2017 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" |
florian | ac3a1d8 | 2013-09-15 09:18:03 +0000 | [diff] [blame] | 34 | #include "pub_core_libcassert.h" |
| 35 | #include "pub_core_tooliface.h" // VG_(needs) |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 36 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 37 | #include "pub_core_options.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 38 | #include "pub_core_replacemalloc.h" |
| 39 | |
| 40 | /*------------------------------------------------------------*/ |
| 41 | /*--- Command line options ---*/ |
| 42 | /*------------------------------------------------------------*/ |
| 43 | |
| 44 | /* Nb: the allocator always rounds blocks up to a multiple of |
| 45 | VG_MIN_MALLOC_SZB. |
| 46 | */ |
| 47 | |
| 48 | /* DEBUG: print malloc details? default: NO */ |
| 49 | Bool VG_(clo_trace_malloc) = False; |
| 50 | |
| 51 | /* Minimum alignment in functions that don't specify alignment explicitly. |
| 52 | default: 0, i.e. use VG_MIN_MALLOC_SZB. */ |
| 53 | UInt VG_(clo_alignment) = VG_MIN_MALLOC_SZB; |
| 54 | |
| 55 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 56 | Bool VG_(replacement_malloc_process_cmd_line_option)(const HChar* arg) |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 57 | { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 58 | if VG_INT_CLO(arg, "--alignment", VG_(clo_alignment)) { |
| 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 | { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 63 | VG_(fmsg_bad_option)(arg, |
| 64 | "Alignment must be a power of 2 in the range %d..4096.\n", |
| 65 | VG_MIN_MALLOC_SZB); |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 68 | else if VG_XACT_CLO(arg, "--xtree-memory=none", |
| 69 | VG_(clo_xtree_memory), Vg_XTMemory_None) {} |
| 70 | else if VG_XACT_CLO(arg, "--xtree-memory=allocs", |
| 71 | VG_(clo_xtree_memory), Vg_XTMemory_Allocs) {} |
| 72 | else if VG_XACT_CLO(arg, "--xtree-memory=full", |
| 73 | VG_(clo_xtree_memory), Vg_XTMemory_Full) {} |
| 74 | else if VG_STR_CLO (arg, "--xtree-memory-file", |
| 75 | VG_(clo_xtree_memory_file)) {} |
| 76 | else if VG_BOOL_CLO(arg, "--xtree-compress-strings", |
| 77 | VG_(clo_xtree_compress_strings)) {} |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 78 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 79 | else if VG_BOOL_CLO(arg, "--trace-malloc", VG_(clo_trace_malloc)) {} |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 80 | else |
| 81 | return False; |
| 82 | |
| 83 | return True; |
| 84 | } |
| 85 | |
florian | ac3a1d8 | 2013-09-15 09:18:03 +0000 | [diff] [blame] | 86 | SizeT VG_(malloc_effective_client_redzone_size)(void) |
| 87 | { |
| 88 | vg_assert(VG_(needs).malloc_replacement); |
| 89 | |
| 90 | return VG_(arena_redzone_size)(VG_AR_CLIENT); |
| 91 | } |
| 92 | |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 93 | /*------------------------------------------------------------*/ |
| 94 | /*--- Useful functions ---*/ |
| 95 | /*------------------------------------------------------------*/ |
| 96 | |
| 97 | void* VG_(cli_malloc) ( SizeT align, SizeT nbytes ) |
| 98 | { |
| 99 | // 'align' should be valid (ie. big enough and a power of two) by now. |
| 100 | // VG_(arena_memalign)() will abort if it's not. |
| 101 | if (VG_MIN_MALLOC_SZB == align) |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 102 | return VG_(arena_malloc) ( VG_AR_CLIENT, "replacemalloc.cm.1", |
| 103 | nbytes ); |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 104 | else |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 105 | return VG_(arena_memalign) ( VG_AR_CLIENT, "replacemalloc.cm.2", |
| 106 | align, nbytes ); |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void VG_(cli_free) ( void* p ) |
| 110 | { |
| 111 | VG_(arena_free) ( VG_AR_CLIENT, p ); |
| 112 | } |
| 113 | |
florian | e464e80 | 2014-09-11 20:15:23 +0000 | [diff] [blame] | 114 | // Useful for querying user blocks. |
| 115 | SizeT VG_(cli_malloc_usable_size) ( void* p ) |
| 116 | { |
| 117 | return VG_(arena_malloc_usable_size)(VG_AR_CLIENT, p); |
| 118 | } |
| 119 | |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 120 | Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size, SizeT rz_szB ) |
| 121 | { |
| 122 | return ( start - rz_szB <= a && a < start + size + rz_szB ); |
| 123 | } |
| 124 | |
| 125 | /*--------------------------------------------------------------------*/ |
| 126 | /*--- end ---*/ |
| 127 | /*--------------------------------------------------------------------*/ |