2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: malloc.c /main/3 1995/11/08 09:52:58 rswiston $ */
24 /* Copyright (c) 1988, 1989 Hewlett-Packard Co. */
26 /* Interfaces to free and malloc with optional debugging traces */
36 extern LOGICAL m_heapchk ;
37 extern LOGICAL m_malftrace ;
53 void *block, char *msg
71 void *ptr, int size, char *msg
87 void m_free(block, msg)
94 if (m_heapchk) m_heapdump() ;
98 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
99 snprintf(buffer, 32, "%5x:%5x",
100 (unsigned int) ((unsigned long) block >> 16),
101 (unsigned int) block, msg) ;
103 snprintf(buffer, 32, " %9p", block) ;
106 m_trace("- Freed ") ;
111 if (m_heapchk) m_heapdump() ;
116 void m_heapdump(M_NOPAR)
118 struct _heapinfo hinfo ;
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) ;
131 printf("OK - empty heap\n\n") ;
134 printf("OK - end of heap\n\n") ;
137 printf("Error - bad pointer to heap\n\n") ;
140 printf("Error - bad start of heap\n\n") ;
143 printf("Error - bad node in heap\n\n") ;
150 void *m_malloc(size, msg)
157 size *= sizeof(M_WCHAR);
159 if (m_heapchk) m_heapdump() ;
161 if (! size) return(NULL) ;
162 p = (void *) malloc(size) ;
164 if (m_heapchk) m_heapdump() ;
167 m_errline("Unable to allocate space for ") ;
173 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
174 snprintf(buffer, 32, "%5x:%5x",
175 (unsigned int) ((unsigned long) p >> 16), (unsigned int) p) ;
177 snprintf(buffer, 32, " %9p", p) ;
180 m_trace("- Allocated ") ;
181 snprintf(buffer, 32, "%6d", size) ;
183 m_trace(" bytes for ") ;
190 void *m_realloc(ptr, size, msg)
198 size *= sizeof(M_WCHAR);
200 if (m_heapchk) m_heapdump() ;
202 if (! size) return(NULL) ;
203 p = (void *) realloc(ptr, size) ;
205 if (m_heapchk) m_heapdump() ;
208 m_errline("Unable to re-allocate space for ") ;
214 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
215 snprintf(buffer, 32, "%5x:%5x",
216 (unsigned int) ((unsigned long) p >> 16), (unsigned int) p) ;
218 snprintf(buffer, 32, " %9p", p) ;
221 m_trace("- Re-allocated ") ;
222 snprintf(buffer, 32, "%6d", size) ;
224 m_trace(" bytes for ") ;