1 /* Copyright (c) 2008, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Author: Craig Silverstein
33 * C shims for the C++ malloc_hook.h. See malloc_hook.h for details
34 * on how to use these.
37 #ifndef _MALLOC_HOOK_C_H_
38 #define _MALLOC_HOOK_C_H_
41 #include <sys/types.h>
43 /* Annoying stuff for windows; makes sure clients can import these functions */
44 #ifndef PERFTOOLS_DLL_DECL
46 # define PERFTOOLS_DLL_DECL __declspec(dllimport)
48 # define PERFTOOLS_DLL_DECL
56 /* Get the current stack trace. Try to skip all routines up to and
57 * and including the caller of MallocHook::Invoke*.
58 * Use "skip_count" (similarly to GetStackTrace from stacktrace.h)
59 * as a hint about how many routines to skip if better information
63 int MallocHook_GetCallerStackTrace(void** result
, int max_depth
,
66 /* The MallocHook_{Add,Remove}*Hook functions return 1 on success and 0 on
70 typedef void (*MallocHook_NewHook
)(const void* ptr
, size_t size
);
72 int MallocHook_AddNewHook(MallocHook_NewHook hook
);
74 int MallocHook_RemoveNewHook(MallocHook_NewHook hook
);
76 typedef void (*MallocHook_DeleteHook
)(const void* ptr
);
78 int MallocHook_AddDeleteHook(MallocHook_DeleteHook hook
);
80 int MallocHook_RemoveDeleteHook(MallocHook_DeleteHook hook
);
82 typedef void (*MallocHook_PreMmapHook
)(const void *start
,
89 int MallocHook_AddPreMmapHook(MallocHook_PreMmapHook hook
);
91 int MallocHook_RemovePreMmapHook(MallocHook_PreMmapHook hook
);
93 typedef void (*MallocHook_MmapHook
)(const void* result
,
101 int MallocHook_AddMmapHook(MallocHook_MmapHook hook
);
103 int MallocHook_RemoveMmapHook(MallocHook_MmapHook hook
);
105 typedef int (*MallocHook_MmapReplacement
)(const void* start
,
112 int MallocHook_SetMmapReplacement(MallocHook_MmapReplacement hook
);
113 int MallocHook_RemoveMmapReplacement(MallocHook_MmapReplacement hook
);
115 typedef void (*MallocHook_MunmapHook
)(const void* ptr
, size_t size
);
117 int MallocHook_AddMunmapHook(MallocHook_MunmapHook hook
);
119 int MallocHook_RemoveMunmapHook(MallocHook_MunmapHook hook
);
121 typedef int (*MallocHook_MunmapReplacement
)(const void* ptr
,
124 int MallocHook_SetMunmapReplacement(MallocHook_MunmapReplacement hook
);
125 int MallocHook_RemoveMunmapReplacement(MallocHook_MunmapReplacement hook
);
127 typedef void (*MallocHook_MremapHook
)(const void* result
,
128 const void* old_addr
,
132 const void* new_addr
);
134 int MallocHook_AddMremapHook(MallocHook_MremapHook hook
);
136 int MallocHook_RemoveMremapHook(MallocHook_MremapHook hook
);
138 typedef void (*MallocHook_PreSbrkHook
)(ptrdiff_t increment
);
140 int MallocHook_AddPreSbrkHook(MallocHook_PreSbrkHook hook
);
142 int MallocHook_RemovePreSbrkHook(MallocHook_PreSbrkHook hook
);
144 typedef void (*MallocHook_SbrkHook
)(const void* result
, ptrdiff_t increment
);
146 int MallocHook_AddSbrkHook(MallocHook_SbrkHook hook
);
148 int MallocHook_RemoveSbrkHook(MallocHook_SbrkHook hook
);
150 /* The following are DEPRECATED. */
152 MallocHook_NewHook
MallocHook_SetNewHook(MallocHook_NewHook hook
);
154 MallocHook_DeleteHook
MallocHook_SetDeleteHook(MallocHook_DeleteHook hook
);
156 MallocHook_PreMmapHook
MallocHook_SetPreMmapHook(MallocHook_PreMmapHook hook
);
158 MallocHook_MmapHook
MallocHook_SetMmapHook(MallocHook_MmapHook hook
);
160 MallocHook_MunmapHook
MallocHook_SetMunmapHook(MallocHook_MunmapHook hook
);
162 MallocHook_MremapHook
MallocHook_SetMremapHook(MallocHook_MremapHook hook
);
164 MallocHook_PreSbrkHook
MallocHook_SetPreSbrkHook(MallocHook_PreSbrkHook hook
);
166 MallocHook_SbrkHook
MallocHook_SetSbrkHook(MallocHook_SbrkHook hook
);
167 /* End of DEPRECATED functions. */
173 #endif /* _MALLOC_HOOK_C_H_ */