1 /**************************************************************************** 2 * 3 * Open Watcom Project 4 * 5 * Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. 6 * 7 * ======================================================================== 8 * 9 * This file contains Original Code and/or Modifications of Original 10 * Code as defined in and that are subject to the Sybase Open Watcom 11 * Public License version 1.0 (the 'License'). You may not use this file 12 * except in compliance with the License. BY USING THIS FILE YOU AGREE TO 13 * ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is 14 * provided with the Original Code and Modifications, and is also 15 * available at www.sybase.com/developer/opensource. 16 * 17 * The Original Code and all software distributed under the License are 18 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 19 * EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM 20 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR 22 * NON-INFRINGEMENT. Please see the License for the specific language 23 * governing rights and limitations under the License. 24 * 25 * ======================================================================== 26 * 27 * Description: Interface to the trmem memory allocation tracker and 28 * validator. This isn't used normally and will work for 29 * Open Watcom only. 30 ****************************************************************************/ 31 32 33 #ifndef _TRMEM_H_INCLUDED 34 #define _TRMEM_H_INCLUDED 35 36 #include <stddef.h> 37 38 typedef struct _trmem_internal *_trmem_hdl; 39 40 typedef void (*_trmem_who)( void ); /* generic pointer to code */ 41 #define _TRMEM_NO_ROUTINE ((_trmem_who)0) 42 43 /* generic pointer to code with realloc signature */ 44 typedef void *(*_trmem_realloc_who)(void*,size_t); 45 #define _TRMEM_NO_REALLOC ((_trmem_realloc_who)0) 46 47 /* 48 These are some special conditions that trmem can detect. OR together 49 the ones you're interested in and pass them to _trmem_open in the __flags 50 parameter. 51 */ 52 enum { 53 _TRMEM_ALLOC_SIZE_0 =0x0001,/* attempted alloc of size 0 */ 54 _TRMEM_REALLOC_SIZE_0 =0x0002,/* attempted realloc/expand of size 0 */ 55 _TRMEM_REALLOC_NULL =0x0004,/* attempted realloc/expand of a NULL ptr */ 56 _TRMEM_FREE_NULL =0x0008,/* attempted free of a NULL pointer */ 57 _TRMEM_OUT_OF_MEMORY =0x0010,/* warn if trmem can't allocate memory 58 for its own purposes */ 59 _TRMEM_CLOSE_CHECK_FREE =0x0020 /* _trmem_close checks if all chunks 60 were freed */ 61 }; 62 63 /* 64 _trmem_open: 65 66 __alloc must be supplied and behave like malloc() when passed a size of 0. 67 68 __free must be supplied and behave like free() when passed a NULL ptr. 69 70 __realloc may be omitted (use _TRMEM_NO_REALLOC). __realloc must behave 71 like realloc() when passed a NULL pointer or a size of 0. 72 73 __expand may be omitted (use _TRMEM_NO_REALLOC). __expand must behave 74 like _expand() when passed a NULL pointer or a size of 0. 75 76 __prt_parm is passed to __prt_line only. 77 78 __prt_line must be supplied. It is called to output any messages trmem 79 needs to communicate. __buf is a null-terminated string of length 80 __len (including a trailing '\n'). 81 82 __flags see enum above for more information. 83 84 trmem uses __alloc and __free for its own internal structures. None of 85 the internal structures will appear in the memory statistics given by 86 _trmem_prt_usage or _trmem_prt_list. 87 88 The handle returned uniquely identifies the tracker. Multiple trackers 89 may be used simultaneously. 90 91 A NULL return indicates failure, for one of any reason. 92 93 _trmem_open can/will use any of __alloc, __free, or __prt_line; so be 94 sure they are initialized before calling _trmem_open. 95 */ 96 _trmem_hdl _trmem_open( 97 void *(*__alloc)(size_t), 98 void (*__free)(void*), 99 void * (*__realloc)(void*,size_t), 100 void * (*__expand)(void*,size_t), 101 FILE *__prt_parm, 102 void (*__prt_line)( FILE *__prt_parm, const char *__buf, size_t __len ), 103 unsigned __flags 104 ); 105 106 107 /* 108 If ( __flags & _TRMEM_CLOSE_CHECK_FREE ) then _trmem_close checks if all 109 allocated chunks were freed before closing the handle. 110 Returns number of unfreed chunks. 111 */ 112 unsigned _trmem_close( _trmem_hdl ); 113 114 115 /* 116 Replace calls such as 117 ptr = malloc( size ); 118 with 119 ptr = _trmem_alloc( size, _trmem_guess_who(), hdl ); 120 */ 121 void *_trmem_alloc( size_t, _trmem_who, _trmem_hdl ); 122 void _trmem_free( void *, _trmem_who, _trmem_hdl ); 123 void *_trmem_realloc( void *, size_t, _trmem_who, _trmem_hdl ); 124 void *_trmem_expand( void *, size_t, _trmem_who, _trmem_hdl ); 125 char *_trmem_strdup( const char *str, _trmem_who who, _trmem_hdl hdl ); 126 size_t _trmem_msize( void *, _trmem_hdl ); 127 128 129 /* 130 _trmem_prt_usage prints the current memory usage, and peak usage. 131 _trmem_prt_list prints a list of all currently allocated chunks. 132 */ 133 void _trmem_prt_usage( _trmem_hdl ); 134 unsigned _trmem_prt_list( _trmem_hdl ); 135 136 /* 137 _trmem_get_current_usage retrieves the current memory usage. 138 _trmem_get_peak_usage retrieves the peak memory usage. 139 */ 140 unsigned long _trmem_get_current_usage( _trmem_hdl ); 141 unsigned long _trmem_get_peak_usage( _trmem_hdl ); 142 143 _trmem_who _trmem_guess_who( void * ); 144 #ifdef __WATCOMC__ 145 #pragma aux _trmem_guess_who = \ 146 0x8b 0x45 0x04 /* mov eax,[ebp+4] */ \ 147 parm caller [] \ 148 value [eax] \ 149 modify exact [eax]; 150 #endif 151 152 #endif 153