2 /*--------------------------------------------------------------------*/
3 /*--- Malloc replacement. replacemalloc_core.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2017 Julian Seward
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics.h"
30 #include "pub_core_libcbase.h"
31 #include "pub_core_libcprint.h"
32 #include "pub_core_libcassert.h"
33 #include "pub_core_tooliface.h" // VG_(needs)
34 #include "pub_core_mallocfree.h"
35 #include "pub_core_options.h"
36 #include "pub_core_replacemalloc.h"
38 /*------------------------------------------------------------*/
39 /*--- Command line options ---*/
40 /*------------------------------------------------------------*/
42 /* Nb: the allocator always rounds blocks up to a multiple of
46 /* DEBUG: print malloc details? default: NO */
47 Bool
VG_(clo_trace_malloc
) = False
;
48 #if defined(VGO_linux) && !defined(MUSL_LIBC)
49 Bool
VG_(clo_realloc_zero_bytes_frees
) = True
;
51 Bool
VG_(clo_realloc_zero_bytes_frees
) = False
;
54 /* Minimum alignment in functions that don't specify alignment explicitly.
55 default: 0, i.e. use VG_MIN_MALLOC_SZB. */
56 UInt
VG_(clo_alignment
) = VG_MIN_MALLOC_SZB
;
59 Bool
VG_(replacement_malloc_process_cmd_line_option
)(const HChar
* arg
)
61 if VG_INT_CLO(arg
, "--alignment", VG_(clo_alignment
)) {
62 if (VG_(clo_alignment
) < VG_MIN_MALLOC_SZB
||
63 VG_(clo_alignment
) > 4096 ||
64 VG_(log2
)( VG_(clo_alignment
) ) == -1 /* not a power of 2 */)
66 VG_(fmsg_bad_option
)(arg
,
67 "Alignment must be a power of 2 in the range %d..4096.\n",
71 else if VG_XACT_CLO(arg
, "--xtree-memory=none",
72 VG_(clo_xtree_memory
), Vg_XTMemory_None
) {}
73 else if VG_XACT_CLO(arg
, "--xtree-memory=allocs",
74 VG_(clo_xtree_memory
), Vg_XTMemory_Allocs
) {}
75 else if VG_XACT_CLO(arg
, "--xtree-memory=full",
76 VG_(clo_xtree_memory
), Vg_XTMemory_Full
) {}
77 else if VG_STR_CLO (arg
, "--xtree-memory-file",
78 VG_(clo_xtree_memory_file
)) {}
79 else if VG_BOOL_CLO(arg
, "--xtree-compress-strings",
80 VG_(clo_xtree_compress_strings
)) {}
82 else if VG_BOOL_CLO(arg
, "--trace-malloc", VG_(clo_trace_malloc
)) {}
83 else if VG_BOOL_CLO(arg
, "--realloc-zero-bytes-frees", VG_(clo_realloc_zero_bytes_frees
)) {}
90 SizeT
VG_(malloc_effective_client_redzone_size
)(void)
92 vg_assert(VG_(needs
).malloc_replacement
);
94 return VG_(arena_redzone_size
)(VG_AR_CLIENT
);
97 /*------------------------------------------------------------*/
98 /*--- Useful functions ---*/
99 /*------------------------------------------------------------*/
101 void* VG_(cli_malloc
) ( SizeT align
, SizeT nbytes
)
103 // 'align' should be valid (ie. big enough and a power of two) by now.
104 // VG_(arena_memalign)() will abort if it's not.
105 if (VG_MIN_MALLOC_SZB
== align
)
106 return VG_(arena_malloc
) ( VG_AR_CLIENT
, "replacemalloc.cm.1",
109 return VG_(arena_memalign
) ( VG_AR_CLIENT
, "replacemalloc.cm.2",
113 void* VG_(cli_realloc
) ( void* ptr
, SizeT nbytes
)
115 return VG_(arena_realloc
) ( VG_AR_CLIENT
, "replacemalloc.cr.1",
119 void VG_(cli_free
) ( void* p
)
121 VG_(arena_free
) ( VG_AR_CLIENT
, p
);
124 // Useful for querying user blocks.
125 SizeT
VG_(cli_malloc_usable_size
) ( void* p
)
127 return VG_(arena_malloc_usable_size
)(VG_AR_CLIENT
, p
);
130 Bool
VG_(addr_is_in_block
)( Addr a
, Addr start
, SizeT size
, SizeT rz_szB
)
132 return ( start
- rz_szB
<= a
&& a
< start
+ size
+ rz_szB
);
135 /*--------------------------------------------------------------------*/
137 /*--------------------------------------------------------------------*/