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 11:06:50 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(void *block, char *msg)
92 if (m_heapchk) m_heapdump() ;
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) ;
100 snprintf(buffer, 32, " %9p", block) ;
103 m_trace("- Freed ") ;
109 if (m_heapchk) m_heapdump() ;
114 void m_heapdump(M_NOPAR)
116 struct _heapinfo hinfo ;
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) ;
129 printf("OK - empty heap\n\n") ;
132 printf("OK - end of heap\n\n") ;
135 printf("Error - bad pointer to heap\n\n") ;
138 printf("Error - bad start of heap\n\n") ;
141 printf("Error - bad node in heap\n\n") ;
148 void *m_malloc(size, msg)
155 size *= sizeof(M_WCHAR);
157 if (m_heapchk) m_heapdump() ;
159 if (! size) return(NULL) ;
160 p = (void *) malloc(size) ;
162 if (m_heapchk) m_heapdump() ;
165 m_errline("Unable to allocate space for ") ;
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) ;
175 snprintf(buffer, 32, " %9p", p) ;
178 m_trace("- Allocated ") ;
179 snprintf(buffer, 32, "%6d", size) ;
181 m_trace(" bytes for ") ;
188 void *m_realloc(ptr, size, msg)
196 size *= sizeof(M_WCHAR);
198 if (m_heapchk) m_heapdump() ;
200 if (! size) return(NULL) ;
201 p = (void *) realloc(ptr, size) ;
203 if (m_heapchk) m_heapdump() ;
206 m_errline("Unable to re-allocate space for ") ;
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) ;
216 snprintf(buffer, 32, " %9p", p) ;
219 m_trace("- Re-allocated ") ;
220 snprintf(buffer, 32, "%6d", size) ;
222 m_trace(" bytes for ") ;