1 /*
2  * Copyright (c) 1999, 2002, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Debug Memory Manager
28  *
29  * - inits allocated memory to predefined byte to expose uninitialized variables
30  * - fills freed memory with predefined byte to expose dangling pointers
31  * - catches under/overwrites with 'guard' bytes around allocated blocks
32  * - tags blocks with the file name and line number where they were allocated
33  * - reports unfreed blocks to help find memory leaks
34  *
35  */
36 
37 #if !defined(_DEBUGMEM_H)
38 #define _DEBUGMEM_H
39 
40 #if defined(__cplusplus)
41 extern "C" {
42 #endif
43 
44 #if defined(DEBUG)
45 
46 #include "debug_util.h"
47 
48 /* prototype for allocation callback function */
49 typedef void * (*DMEM_ALLOCFN)(size_t size);
50 
51 /* prototype for deallocation callback function */
52 typedef void (*DMEM_FREEFN)(void * pointer);
53 
54 /* prototype for pointer validation function */
55 typedef dbool_t (*DMEM_CHECKPTRFN)(void * ptr, size_t size);
56 
57 /* Debug memory manager global state */
58 /* DO NOT REFERENCE this structure in code, it is only exported */
59 /* to ease it's use inside a source level debugger */
60 typedef struct DMemState {
61     DMEM_ALLOCFN        pfnAlloc;       /* block allocate callback */
62     DMEM_FREEFN         pfnFree;        /* block free callback */
63     DMEM_CHECKPTRFN     pfnCheckPtr;    /* pointer validation callback */
64     size_t              biggestBlock;   /* largest block allocated so far */
65     size_t              maxHeap;        /* maximum size of the debug heap */
66     size_t              totalHeapUsed;  /* total memory allocated so far */
67     dbool_t             failNextAlloc;  /* whether the next allocation fails (automatically resets)*/
68     int                 totalAllocs;    /* total number of allocations so far */
69 } DMemState;
70 
71 /* Exported global var so you can view/change settings in the debugger */
72 extern const DMemState  * DMemStatePtr;
73 
74 /* General memory manager functions */
75 extern void DMem_Initialize();
76 extern void DMem_Shutdown();
77 extern void * DMem_AllocateBlock(size_t size, const char * filename, int linenumber);
78 extern void DMem_FreeBlock(void *ptr);
79 extern void DMem_ReportLeaks();
80 
81 /* Routines to customize behaviour with callbacks */
82 extern void DMem_SetAllocCallback( DMEM_ALLOCFN pfn );
83 extern void DMem_SetFreeCallback( DMEM_FREEFN pfn );
84 extern void DMem_SetCheckPtrCallback( DMEM_CHECKPTRFN pfn );
85 extern void DMem_DisableMutex();
86 
87 #endif /* defined(DEBUG) */
88 
89 #if defined(__cplusplus)
90 } /* extern "C" */
91 #endif
92 
93 #endif /* _DEBUGMEM_H */
94