1
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
10   Copyright (C) 2000-2013 Julian Seward
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
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
43      CORE      for the core's and tools' general use.
44      DINFO     for debug info (symbols, line #s, CFI, etc) storage.
45      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.
48      TTAUX     for storing TT/TC auxiliary structures (address range
49                equivalence classes).
50
51   When adding a new arena, remember also to add it to ensure_mm_init().
52*/
53typedef Int ArenaId;
54
55#define VG_N_ARENAS        5
56
57#define VG_AR_CORE         0
58#define VG_AR_DINFO        1
59#define VG_AR_CLIENT       2
60#define VG_AR_DEMANGLE     3
61#define VG_AR_TTAUX        4
62
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.
66#if   defined(VGP_x86_linux)   || \
67      defined(VGP_arm_linux)   || \
68      defined(VGP_mips32_linux)
69#  define VG_MIN_MALLOC_SZB        8
70// 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.
72// Also, use 16 bytes for any PPC variant, since 16 is required to make
73// Altiveccery work right.
74#elif defined(VGP_amd64_linux)  || \
75      defined(VGP_ppc32_linux)  || \
76      defined(VGP_ppc64_linux)  || \
77      defined(VGP_s390x_linux)  || \
78      defined(VGP_mips64_linux) || \
79      defined(VGP_x86_darwin)   || \
80      defined(VGP_amd64_darwin) || \
81      defined(VGP_arm64_linux)
82#  define VG_MIN_MALLOC_SZB       16
83#else
84#  error Unknown platform
85#endif
86
87/* This struct definition MUST match the system one. */
88/* SVID2/XPG mallinfo structure */
89struct vg_mallinfo {
90   int arena;    /* total space allocated from system */
91   int ordblks;  /* number of non-inuse chunks */
92   int smblks;   /* unused -- always zero */
93   int hblks;    /* number of mmapped regions */
94   int hblkhd;   /* total space in mmapped regions */
95   int usmblks;  /* unused -- always zero */
96   int fsmblks;  /* unused -- always zero */
97   int uordblks; /* total allocated space */
98   int fordblks; /* total non-inuse space */
99   int keepcost; /* top-most, releasable (via malloc_trim) space */
100};
101
102extern void* VG_(arena_malloc)  ( ArenaId arena, const HChar* cc, SizeT nbytes );
103extern void  VG_(arena_free)    ( ArenaId arena, void* ptr );
104extern void* VG_(arena_calloc)  ( ArenaId arena, const HChar* cc,
105                                  SizeT nmemb, SizeT bytes_per_memb );
106extern void* VG_(arena_realloc) ( ArenaId arena, const HChar* cc,
107                                  void* ptr, SizeT size );
108extern void* VG_(arena_memalign)( ArenaId aid, const HChar* cc,
109                                  SizeT req_alignB, SizeT req_pszB );
110extern HChar* VG_(arena_strdup)  ( ArenaId aid, const HChar* cc,
111                                   const HChar* s);
112
113extern SizeT VG_(arena_malloc_usable_size) ( ArenaId aid, void* payload );
114
115extern SizeT VG_(arena_redzone_size) ( ArenaId aid );
116
117extern void  VG_(mallinfo) ( ThreadId tid, struct vg_mallinfo* mi );
118
119// VG_(arena_perm_malloc) is for permanent allocation of small blocks.
120// See VG_(perm_malloc) in pub_tool_mallocfree.h for more details.
121// Do not call any VG_(arena_*) functions with these permanent blocks.
122extern void* VG_(arena_perm_malloc) ( ArenaId aid, SizeT nbytes, Int align );
123
124extern void  VG_(sanity_check_malloc_all) ( void );
125
126extern void  VG_(print_all_arena_stats) ( void );
127
128extern void  VG_(print_arena_cc_analysis) ( void );
129
130typedef
131   struct _AddrArenaInfo
132   AddrArenaInfo;
133
134struct _AddrArenaInfo {
135   ArenaId aid;
136   const HChar* name; // arena name, !NULL if Addr a points in an arena.
137   SizeT       block_szB;
138   PtrdiffT    rwoffset;
139   Bool        free;  // True if this is in the arena free zone.
140};
141/* If Addr a points in one of the allocation arenas, describes Addr a in *aai
142   otherwise sets *aai to 0/NULL/...
143   Note that no information is produced for addresses allocated with
144   VG_(arena_perm_malloc). */
145extern void VG_(describe_arena_addr) ( Addr a, /*OUT*/AddrArenaInfo* aai );
146
147#endif   // __PUB_CORE_MALLOCFREE_H
148
149/*--------------------------------------------------------------------*/
150/*--- end                                                          ---*/
151/*--------------------------------------------------------------------*/
152