blob: aa9b46a366f3643a57e716031c8aeaa2dbc4d03a [file] [log] [blame]
njn717cde52005-05-10 02:47:21 +00001
2/*--------------------------------------------------------------------*/
3/*--- Malloc replacement. replacemalloc_core.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj0f157dd2013-10-18 14:27:36 +000010 Copyright (C) 2000-2013 Julian Seward
njn717cde52005-05-10 02:47:21 +000011 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
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njn97405b22005-06-02 03:39:33 +000032#include "pub_core_libcbase.h"
njn36a20fa2005-06-03 03:08:39 +000033#include "pub_core_libcprint.h"
florianac3a1d82013-09-15 09:18:03 +000034#include "pub_core_libcassert.h"
35#include "pub_core_tooliface.h" // VG_(needs)
njnaf1d7df2005-06-11 01:31:52 +000036#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000037#include "pub_core_options.h"
njn717cde52005-05-10 02:47:21 +000038#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 */
49Bool 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. */
53UInt VG_(clo_alignment) = VG_MIN_MALLOC_SZB;
54
55
florian19f91bb2012-11-10 22:29:54 +000056Bool VG_(replacement_malloc_process_cmd_line_option)(const HChar* arg)
njn717cde52005-05-10 02:47:21 +000057{
njn83df0b62009-02-25 01:01:05 +000058 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 {
njnb1cc5d62010-07-06 04:05:23 +000063 VG_(fmsg_bad_option)(arg,
64 "Alignment must be a power of 2 in the range %d..4096.\n",
65 VG_MIN_MALLOC_SZB);
njn717cde52005-05-10 02:47:21 +000066 }
67 }
68
njn83df0b62009-02-25 01:01:05 +000069 else if VG_BOOL_CLO(arg, "--trace-malloc", VG_(clo_trace_malloc)) {}
njn717cde52005-05-10 02:47:21 +000070 else
71 return False;
72
73 return True;
74}
75
florianac3a1d82013-09-15 09:18:03 +000076SizeT VG_(malloc_effective_client_redzone_size)(void)
77{
78 vg_assert(VG_(needs).malloc_replacement);
79
80 return VG_(arena_redzone_size)(VG_AR_CLIENT);
81}
82
njn717cde52005-05-10 02:47:21 +000083/*------------------------------------------------------------*/
84/*--- Useful functions ---*/
85/*------------------------------------------------------------*/
86
87void* VG_(cli_malloc) ( SizeT align, SizeT nbytes )
88{
89 // 'align' should be valid (ie. big enough and a power of two) by now.
90 // VG_(arena_memalign)() will abort if it's not.
91 if (VG_MIN_MALLOC_SZB == align)
sewardj9c606bd2008-09-18 18:12:50 +000092 return VG_(arena_malloc) ( VG_AR_CLIENT, "replacemalloc.cm.1",
93 nbytes );
njn717cde52005-05-10 02:47:21 +000094 else
sewardj9c606bd2008-09-18 18:12:50 +000095 return VG_(arena_memalign) ( VG_AR_CLIENT, "replacemalloc.cm.2",
96 align, nbytes );
njn717cde52005-05-10 02:47:21 +000097}
98
99void VG_(cli_free) ( void* p )
100{
101 VG_(arena_free) ( VG_AR_CLIENT, p );
102}
103
104Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size, SizeT rz_szB )
105{
106 return ( start - rz_szB <= a && a < start + size + rz_szB );
107}
108
109/*--------------------------------------------------------------------*/
110/*--- end ---*/
111/*--------------------------------------------------------------------*/