dthelp: compiler warning and coverity warning fixes
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / 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 librararies 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:06:50 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(void *block, char *msg)
88   {
89     char buffer[32] ;
90
91 #if defined(MSDOS)
92     if (m_heapchk) m_heapdump() ;
93 #endif
94     if (m_malftrace) {
95 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
96       snprintf(buffer, 32, "%5x:%5x",
97         (unsigned int) ((unsigned long) block >> 16),
98         (unsigned int) block) ;
99 #else
100       snprintf(buffer, 32, "  %9p", block) ;
101 #endif
102       m_trace(buffer) ;
103       m_trace("- Freed                      ") ;
104       m_trace(msg) ;
105       m_trace("\n") ;
106       }      
107     free(block) ;
108 #if defined(MSDOS)
109     if (m_heapchk) m_heapdump() ;
110 #endif
111     }
112
113 #if defined(MSDOS)
114 void m_heapdump(M_NOPAR)
115   {
116     struct _heapinfo hinfo ;
117     int heapstatus ;
118
119     heapstatus = _heapchk() ;
120     if (heapstatus == _HEAPOK || heapstatus == _HEAPEMPTY) return ;
121     printf("\nDumping heap:\n") ;
122     hinfo._pentry = NULL ;
123     while ((heapstatus = _heapwalk(&hinfo)) == _HEAPOK) 
124       printf("%6s block at %p of size %4.4X\n",
125              hinfo._useflag == _USEDENTRY ? "USED" : "FREE",
126              hinfo._pentry, hinfo._size) ;
127     switch(heapstatus) {
128       case _HEAPEMPTY:
129         printf("OK - empty heap\n\n") ;
130         break ;
131       case _HEAPEND:
132         printf("OK - end of heap\n\n") ;
133         break ;
134       case _HEAPBADPTR:
135         printf("Error - bad pointer to heap\n\n") ;
136         break ;
137       case _HEAPBADBEGIN:
138         printf("Error - bad start of heap\n\n") ;
139         break ;
140       case _HEAPBADNODE:
141         printf("Error - bad node in heap\n\n") ;
142         break ;
143       }
144     m_exit(TRUE) ;
145     }
146 #endif
147
148 void *m_malloc(size, msg)
149   int size ;
150   char *msg ;
151   {
152     char buffer[32] ;
153     void *p ;
154
155     size *= sizeof(M_WCHAR);
156 #if defined(MSDOS)
157     if (m_heapchk) m_heapdump() ;
158 #endif
159     if (! size) return(NULL) ;
160     p = (void *) malloc(size) ;
161 #if defined(MSDOS)
162     if (m_heapchk) m_heapdump() ;
163 #endif
164     if (! p) {
165       m_errline("Unable to allocate space for ") ;
166       m_errline(msg) ;
167       m_errline("\n") ;
168       m_exit(TRUE) ;
169       }
170     if (m_malftrace) {
171 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
172       snprintf(buffer, 32, "%5x:%5x",
173         (unsigned int) ((unsigned long) p >> 16), (unsigned int) p) ;
174 #else
175       snprintf(buffer, 32, "  %9p", p) ;
176 #endif
177       m_trace(buffer) ;
178       m_trace("- Allocated ") ;
179       snprintf(buffer, 32, "%6d", size) ;
180       m_trace(buffer) ;
181       m_trace(" bytes for ") ;
182       m_trace(msg) ;
183       m_trace("\n") ;
184       }      
185     return(p) ;
186     }
187
188 void *m_realloc(ptr, size, msg)
189   void *ptr ;
190   int size ;
191   char *msg ;
192   {
193     char buffer[32] ;
194     void *p ;
195
196     size *= sizeof(M_WCHAR);
197 #if defined(MSDOS)
198     if (m_heapchk) m_heapdump() ;
199 #endif
200     if (! size) return(NULL) ;
201     p = (void *) realloc(ptr, size) ;
202 #if defined(MSDOS)
203     if (m_heapchk) m_heapdump() ;
204 #endif
205     if (! p) {
206       m_errline("Unable to re-allocate space for ") ;
207       m_errline(msg) ;
208       m_errline("\n") ;
209       m_exit(TRUE) ;
210       }
211     if (m_malftrace) {
212 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
213       snprintf(buffer, 32, "%5x:%5x",
214         (unsigned int) ((unsigned long) p >> 16), (unsigned int) p) ;
215 #else
216       snprintf(buffer, 32, "  %9p", p) ;
217 #endif
218       m_trace(buffer) ;
219       m_trace("- Re-allocated ") ;
220       snprintf(buffer, 32, "%6d", size) ;
221       m_trace(buffer) ;
222       m_trace(" bytes for ") ;
223       m_trace(msg) ;
224       m_trace("\n") ;
225       }      
226     return(p) ;
227     }
228
229