1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #ifndef _MCW_MALLOC_HEADER_
8 #define _MCW_MALLOC_HEADER_
9 
10 /*----- 24 Jan 2001: modified slightly to add some comments, and
11                      to fit in with the hashtable-ized mcw_malloc.c -----*/
12 
13 #ifdef  __cplusplus
14 extern "C" {
15 #endif
16 
17 #ifndef ALLOW_MCW_MALLOC
18 #ifndef DONT_USE_MCW_MALLOC  /* don't redefine to avoid warnings */
19 #define DONT_USE_MCW_MALLOC  /* old way to mark  mcw_malloc usage/non-usage */
20 #endif
21 #else
22 # undef  DONT_USE_MCW_MALLOC
23 #endif
24 
25 /*---------------------------------------------------------------------------*/
26 #ifdef DONT_USE_MCW_MALLOC
27 
28 #undef USING_MCW_MALLOC
29 
30 #define MCW_MALLOC_enabled 0
31 
32 #define MCW_MALLOC_status  NULL
33 #define MCW_MALLOC_total   0
34 
35 #undef  mcw_malloc
36 #define mcw_malloc  malloc
37 
38 #undef  mcw_malloc_OK
39 #define mcw_malloc_OK(p) 1
40 
41 #undef  mcw_realloc
42 #define mcw_realloc realloc
43 
44 #undef  mcw_calloc
45 #define mcw_calloc  calloc
46 
47 #undef  mcw_free
48 #define mcw_free(a,b,c)  free(a)
49 
50 #undef  mcw_strdup
51 #define mcw_strdup  strdup
52 
53 #define mcw_malloc_dump_fp(x) /*nada*/
54 
55 /***** extern void   mcw_malloc_dump_fp(FILE *fp) ; *****/
56 
57 /*---------------------------------------------------------------------------*/
58 #else
59 
60 #define USING_MCW_MALLOC
61 
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <stdint.h>
65 #include "replaceXt.h"
66 
67 /* Use mrix for X dependent programs */
68 #ifdef __BUILDING_QUICKLOOK_PLUGIN__
69   #include "IntrinsicQuickLook.h"
70 #else
71   #include <X11/Intrinsic.h>
72 #endif
73 
74 #include "machdep.h"
75 
76 /*-- define macros to replace the source code's use of malloc(), etc. --*/
77 
78 #undef malloc
79 #undef realloc
80 #undef calloc
81 #undef free
82 
83 #define malloc(a)     mcw_malloc((a),__FILE__,__LINE__)
84 #define realloc(a,b)  mcw_realloc((a),(b),__FILE__,__LINE__)
85 #define calloc(a,b)   mcw_calloc((a),(b),__FILE__,__LINE__)
86 #define free(a)       mcw_free((a),__FILE__,__LINE__)
87 
88 #undef  strdup
89 #define strdup(a)     mcw_strdup((a),__FILE__,__LINE__)
90 
91 /*-- prototypes for interface functions --*/
92 
93 extern void   enable_mcw_malloc() ;
94 extern void * mcw_malloc( size_t , char * , int ) ;
95 extern void * mcw_realloc( void * , size_t , char * , int ) ;
96 extern void * mcw_calloc( size_t , size_t , char * , int ) ;
97 extern void   mcw_free( void * , char * , int ) ;
98 extern char * mcw_strdup( char *, char * , int ) ; /* 06 May 2015 */
99 
100 extern char * mcw_malloc_status(const char *,int) ;
101 extern void   mcw_malloc_dump(void) ;
102 extern void   mcw_malloc_dump_sort(int opt) ;
103 extern void   mcw_malloc_dump_fp(FILE *fp) ;  /* 23 Apr 2015 */
104 extern int    mcw_malloc_enabled(void) ;
105 extern void   pause_mcw_malloc(void);
106 extern void   resume_mcw_malloc(void);
107 extern int    mcw_malloc_paused(void);
108 extern int    mcw_malloc_OK(void *);      /* 10 Jun 2014 */
109 
110 extern long long mcw_malloc_total(void) ; /* 01 Feb 2007 */
111 
112 /*-- how to check if the tracking routines are working --*/
113 
114 #define MCW_MALLOC_enabled mcw_malloc_enabled()
115 
116 #define MCW_MALLOC_status  mcw_malloc_status(__FILE__,__LINE__)
117 #define MCW_MALLOC_total   mcw_malloc_total()
118 
119 /*-- do the same macro thing for the Xt library functions --*/
120 
121 #undef XtMalloc
122 #undef XtRealloc
123 #undef XtFree
124 #undef XtCalloc
125 
126 #define XtMalloc(a)     mcw_XtMalloc((a),__FILE__,__LINE__)
127 #define XtRealloc(a,b)  mcw_XtRealloc((char *)(a),(b),__FILE__,__LINE__)
128 #define XtCalloc(a,b)   mcw_XtCalloc((a),(b),__FILE__,__LINE__)
129 #define XtFree(a)       mcw_XtFree((char *)(a))
130 
131 extern char * mcw_XtMalloc( RwcCardinal , char * ,  int ) ;
132 extern char * mcw_XtRealloc( char * , RwcCardinal , char * ,  int ) ;
133 extern char * mcw_XtCalloc( RwcCardinal , RwcCardinal , char * ,  int ) ;
134 extern void   mcw_XtFree( char * ) ;
135 
136 #endif /* DONT_USE_MCW_MALLOC */
137 /*---------------------------------------------------------------------------*/
138 
139 /*-- some macros used in various AFNI places --*/
140 
141 #define myXtFree(xp)  (RwcFree((char *)(xp)) , (xp)=NULL)
142 #define myXtNew(type) ((type *) RwcCalloc(1,(RwcCardinal) sizeof(type)))
143 #define myfree(xp)    (free((xp)) , (xp)=NULL)
144 
145 #ifdef  __cplusplus
146 }
147 #endif
148 
149 #endif /* _MCW_MALLOC_HEADER_ */
150