c9608fad29e037e2298bdf8f1a4d470e0b3f6e00
[oweals/cde.git] / cde / programs / dthelp / parser.ccdf / htag / util / malloc.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: malloc.c /main/3 1995/11/08 11:41:28 rswiston $ */
24 /* Copyright (c) 1988, 1989 Hewlett-Packard Co. */
25
26 /* Interfaces to free and malloc with optional debugging traces */
27
28 /**/
29 #include <stdlib.h>
30 #include <stdio.h>
31 #if defined(MSDOS)
32 #include <process.h>
33 #endif
34 #include "basic.h"
35
36 extern LOGICAL m_heapchk ;
37 extern LOGICAL m_malftrace ;
38
39 void m_errline(
40 #if defined(M_PROTO)
41   char *text
42 #endif
43   ) ;
44
45 void m_exit(
46 #if defined(M_PROTO)
47   int status
48 #endif
49   ) ;
50
51 void m_free(
52 #if defined(M_PROTO)
53   void *block, char *msg
54 #endif
55   ) ;
56
57 void m_heapdump(
58 #if defined(M_PROTO)
59   M_NOPAR
60 #endif
61   ) ;
62
63 void *m_malloc(
64 #if defined(M_PROTO)
65   int size, char *msg
66 #endif
67   ) ;
68
69 void *m_realloc(
70 #if defined(M_PROTO)
71   void *ptr, int size, char *msg
72 #endif
73   ) ;
74
75 void *m_trace(
76 #if defined(M_PROTO)
77   char *text
78 #endif
79   ) ;
80
81 void *m_wctrace(
82 #if defined(M_PROTO)
83   M_WCHAR *text
84 #endif
85   ) ;
86
87 void m_free(block, msg)
88   void *block ;
89   char *msg ;
90   {
91     char buffer[32] ;
92
93 #if defined(MSDOS)
94     if (m_heapchk) m_heapdump() ;
95 #endif
96     if (m_malftrace) {
97 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
98       sprintf(buffer, "%5x:%5x",
99         (unsigned int) ((unsigned long) block >> 16),
100         (unsigned int) block, msg) ;
101 #else
102       sprintf(buffer, "  %9p", block, msg) ;
103 #endif
104       m_trace(buffer) ;
105       m_trace("- Freed                      ") ;
106       m_trace(msg) ;
107       m_trace("\n") ;
108       }      
109     free(block) ;
110 #if defined(MSDOS)
111     if (m_heapchk) m_heapdump() ;
112 #endif
113     }
114
115 #if defined(MSDOS)
116 void m_heapdump(M_NOPAR)
117   {
118     struct _heapinfo hinfo ;
119     int heapstatus ;
120
121     heapstatus = _heapchk() ;
122     if (heapstatus == _HEAPOK || heapstatus == _HEAPEMPTY) return ;
123     printf("\nDumping heap:\n") ;
124     hinfo._pentry = NULL ;
125     while ((heapstatus = _heapwalk(&hinfo)) == _HEAPOK) 
126       printf("%6s block at %p of size %4.4X\n",
127              hinfo._useflag == _USEDENTRY ? "USED" : "FREE",
128              hinfo._pentry, hinfo._size) ;
129     switch(heapstatus) {
130       case _HEAPEMPTY:
131         printf("OK - empty heap\n\n") ;
132         break ;
133       case _HEAPEND:
134         printf("OK - end of heap\n\n") ;
135         break ;
136       case _HEAPBADPTR:
137         printf("Error - bad pointer to heap\n\n") ;
138         break ;
139       case _HEAPBADBEGIN:
140         printf("Error - bad start of heap\n\n") ;
141         break ;
142       case _HEAPBADNODE:
143         printf("Error - bad node in heap\n\n") ;
144         break ;
145       }
146     m_exit(TRUE) ;
147     }
148 #endif
149
150 void *m_malloc(size, msg)
151   int size ;
152   char *msg ;
153   {
154     char buffer[32] ;
155     void *p ;
156
157     size *= sizeof(M_WCHAR);
158 #if defined(MSDOS)
159     if (m_heapchk) m_heapdump() ;
160 #endif
161     if (! size) return(NULL) ;
162     p = (void *) malloc(size) ;
163 #if defined(MSDOS)
164     if (m_heapchk) m_heapdump() ;
165 #endif
166     if (! p) {
167       m_errline("Unable to allocate space for ") ;
168       m_errline(msg) ;
169       m_errline("\n") ;
170       m_exit(TRUE) ;
171       }
172     if (m_malftrace) {
173 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
174       sprintf(buffer, "%5x:%5x",
175         (unsigned int) ((unsigned long) p >> 16), (unsigned int) p) ;
176 #else
177       sprintf(buffer, "  %9p", p) ;
178 #endif
179       m_trace(buffer) ;
180       m_trace("- Allocated ") ;
181       sprintf(buffer, "%6d", size) ;
182       m_trace(buffer) ;
183       m_trace(" bytes for ") ;
184       m_trace(msg) ;
185       m_trace("\n") ;
186       }      
187     return(p) ;
188     }
189
190 void *m_realloc(ptr, size, msg)
191   void *ptr ;
192   int size ;
193   char *msg ;
194   {
195     char buffer[32] ;
196     void *p ;
197
198     size *= sizeof(M_WCHAR);
199 #if defined(MSDOS)
200     if (m_heapchk) m_heapdump() ;
201 #endif
202     if (! size) return(NULL) ;
203     p = (void *) realloc(ptr, size) ;
204 #if defined(MSDOS)
205     if (m_heapchk) m_heapdump() ;
206 #endif
207     if (! p) {
208       m_errline("Unable to re-allocate space for ") ;
209       m_errline(msg) ;
210       m_errline("\n") ;
211       m_exit(TRUE) ;
212       }
213     if (m_malftrace) {
214 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
215       sprintf(buffer, "%5x:%5x",
216         (unsigned int) ((unsigned long) p >> 16), (unsigned int) p) ;
217 #else
218       sprintf(buffer, "  %9p", p) ;
219 #endif
220       m_trace(buffer) ;
221       m_trace("- Re-allocated ") ;
222       sprintf(buffer, "%6d", size) ;
223       m_trace(buffer) ;
224       m_trace(" bytes for ") ;
225       m_trace(msg) ;
226       m_trace("\n") ;
227       }      
228     return(p) ;
229     }
230
231