blob: eeb4aa110c56b86c5cfd922240ad88ef515d93be [file] [log] [blame]
njn717cde52005-05-10 02:47:21 +00001
2/*--------------------------------------------------------------------*/
3/*--- High-level memory management. pub_core_mallocfree.h ---*/
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
31#ifndef __PUB_CORE_MALLOCFREE_H
32#define __PUB_CORE_MALLOCFREE_H
33
34#include "pub_tool_mallocfree.h"
35
36//--------------------------------------------------------------------
37// PURPOSE: high-level memory allocation (malloc/free), for the core and
38// tools.
39//--------------------------------------------------------------------
40
41/* Allocation arenas.
42
floriana2968cc2013-09-20 21:34:40 +000043 CORE for the core's and tools' general use.
sewardjb8b79ad2008-03-03 01:35:41 +000044 DINFO for debug info (symbols, line #s, CFI, etc) storage.
njn717cde52005-05-10 02:47:21 +000045 CLIENT for the client's mallocs/frees, if the tool replaces glibc's
46 malloc() et al -- redzone size is chosen by the tool.
47 DEMANGLE for the C++ demangler.
sewardjcd0309b2005-10-18 02:20:18 +000048 TTAUX for storing TT/TC auxiliary structures (address range
49 equivalence classes).
njn717cde52005-05-10 02:47:21 +000050
51 When adding a new arena, remember also to add it to ensure_mm_init().
52*/
53typedef Int ArenaId;
54
floriana2968cc2013-09-20 21:34:40 +000055#define VG_N_ARENAS 5
njn717cde52005-05-10 02:47:21 +000056
57#define VG_AR_CORE 0
floriana2968cc2013-09-20 21:34:40 +000058#define VG_AR_DINFO 1
59#define VG_AR_CLIENT 2
60#define VG_AR_DEMANGLE 3
61#define VG_AR_TTAUX 4
njn717cde52005-05-10 02:47:21 +000062
63// This is both the minimum payload size of a malloc'd block, and its
64// minimum alignment. Must be a power of 2 greater than 4, and should be
65// greater than 8.
njn88d560f2009-05-21 03:36:21 +000066#if defined(VGP_x86_linux) || \
petarj17d3a442012-09-15 01:04:06 +000067 defined(VGP_arm_linux) || \
dejanj2a6b81c2013-07-17 08:51:53 +000068 defined(VGP_mips32_linux)
njn88d560f2009-05-21 03:36:21 +000069# define VG_MIN_MALLOC_SZB 8
njnf76d27a2009-05-28 01:53:07 +000070// Nb: We always use 16 bytes for Darwin, even on 32-bits, so it can be used
71// for any AltiVec- or SSE-related type. This matches the Darwin libc.
sewardj11684aa2011-04-27 23:25:15 +000072// Also, use 16 bytes for any PPC variant, since 16 is required to make
73// Altiveccery work right.
carllcae0cc22014-08-07 23:17:29 +000074#elif defined(VGP_amd64_linux) || \
75 defined(VGP_ppc32_linux) || \
76 defined(VGP_ppc64be_linux) || \
77 defined(VGP_ppc64le_linux) || \
78 defined(VGP_s390x_linux) || \
79 defined(VGP_mips64_linux) || \
80 defined(VGP_x86_darwin) || \
81 defined(VGP_amd64_darwin) || \
sewardj112711a2015-04-10 12:30:09 +000082 defined(VGP_arm64_linux) || \
83 defined(VGP_tilegx_linux)
njn88d560f2009-05-21 03:36:21 +000084# define VG_MIN_MALLOC_SZB 16
85#else
86# error Unknown platform
87#endif
88
njn088bfb42005-08-17 05:01:37 +000089/* This struct definition MUST match the system one. */
90/* SVID2/XPG mallinfo structure */
91struct vg_mallinfo {
92 int arena; /* total space allocated from system */
93 int ordblks; /* number of non-inuse chunks */
94 int smblks; /* unused -- always zero */
95 int hblks; /* number of mmapped regions */
96 int hblkhd; /* total space in mmapped regions */
97 int usmblks; /* unused -- always zero */
98 int fsmblks; /* unused -- always zero */
99 int uordblks; /* total allocated space */
100 int fordblks; /* total non-inuse space */
101 int keepcost; /* top-most, releasable (via malloc_trim) space */
102};
103
florian54fe2022012-10-27 23:07:42 +0000104extern void* VG_(arena_malloc) ( ArenaId arena, const HChar* cc, SizeT nbytes );
njn717cde52005-05-10 02:47:21 +0000105extern void VG_(arena_free) ( ArenaId arena, void* ptr );
florian54fe2022012-10-27 23:07:42 +0000106extern void* VG_(arena_calloc) ( ArenaId arena, const HChar* cc,
njn717cde52005-05-10 02:47:21 +0000107 SizeT nmemb, SizeT bytes_per_memb );
florian54fe2022012-10-27 23:07:42 +0000108extern void* VG_(arena_realloc) ( ArenaId arena, const HChar* cc,
sewardj9c606bd2008-09-18 18:12:50 +0000109 void* ptr, SizeT size );
florian54fe2022012-10-27 23:07:42 +0000110extern void* VG_(arena_memalign)( ArenaId aid, const HChar* cc,
sewardj9c606bd2008-09-18 18:12:50 +0000111 SizeT req_alignB, SizeT req_pszB );
florian19f91bb2012-11-10 22:29:54 +0000112extern HChar* VG_(arena_strdup) ( ArenaId aid, const HChar* cc,
113 const HChar* s);
njn717cde52005-05-10 02:47:21 +0000114
philippe0b9d0642014-06-30 19:47:24 +0000115/* Specialised version of realloc, that shrinks the size of the block ptr from
116 its current size to req_pszB.
117 req_pszB must be <= to the current size of ptr (otherwise it will assert).
118 Compared to VG_(arena_realloc):
119 * VG_(arena_realloc_shrink) cannot increase the size of ptr.
120 * If large enough, the unused memory is made usable for other allocation.
121 * ptr is shrunk in place, so as to avoid temporary allocation and memcpy. */
122extern void VG_(arena_realloc_shrink) ( ArenaId aid,
123 void* ptr, SizeT req_pszB);
124
njn8b140de2009-02-17 04:31:18 +0000125extern SizeT VG_(arena_malloc_usable_size) ( ArenaId aid, void* payload );
126
florianac3a1d82013-09-15 09:18:03 +0000127extern SizeT VG_(arena_redzone_size) ( ArenaId aid );
128
njn088bfb42005-08-17 05:01:37 +0000129extern void VG_(mallinfo) ( ThreadId tid, struct vg_mallinfo* mi );
130
philippe6e4b7132013-01-18 06:19:49 +0000131// VG_(arena_perm_malloc) is for permanent allocation of small blocks.
132// See VG_(perm_malloc) in pub_tool_mallocfree.h for more details.
133// Do not call any VG_(arena_*) functions with these permanent blocks.
134extern void* VG_(arena_perm_malloc) ( ArenaId aid, SizeT nbytes, Int align );
135
njn717cde52005-05-10 02:47:21 +0000136extern void VG_(sanity_check_malloc_all) ( void );
137
138extern void VG_(print_all_arena_stats) ( void );
139
sewardj9c606bd2008-09-18 18:12:50 +0000140extern void VG_(print_arena_cc_analysis) ( void );
141
philippe07c08522014-05-14 20:39:27 +0000142typedef
143 struct _AddrArenaInfo
144 AddrArenaInfo;
145
146struct _AddrArenaInfo {
147 ArenaId aid;
148 const HChar* name; // arena name, !NULL if Addr a points in an arena.
149 SizeT block_szB;
150 PtrdiffT rwoffset;
151 Bool free; // True if this is in the arena free zone.
152};
153/* If Addr a points in one of the allocation arenas, describes Addr a in *aai
154 otherwise sets *aai to 0/NULL/...
155 Note that no information is produced for addresses allocated with
156 VG_(arena_perm_malloc). */
157extern void VG_(describe_arena_addr) ( Addr a, /*OUT*/AddrArenaInfo* aai );
158
njn717cde52005-05-10 02:47:21 +0000159#endif // __PUB_CORE_MALLOCFREE_H
160
161/*--------------------------------------------------------------------*/
162/*--- end ---*/
163/*--------------------------------------------------------------------*/