2 * defines for the dmalloc library
4 * Copyright 1995 by Gray Watson
6 * This file is part of the dmalloc package.
8 * Permission to use, copy, modify, and distribute this software for any
9 * NON-COMMERCIAL purpose and without fee is hereby granted, provided
10 * that the above copyright notice and this permission notice appear
11 * in all copies, and that the name of Gray Watson not be used in
12 * advertising or publicity pertaining to distribution of the document
13 * or software without specific, written prior permission.
15 * Gray Watson makes no representations about the suitability of the
16 * software described herein for any purpose. It is provided "as is"
17 * without express or implied warranty.
19 * The author may be contacted at gray.watson@letters.com
27 /* this is inserted into dmalloc.h */
28 /* used to handle the const operator */
29 /* const is available */
31 /* NOTE: start of $Id$ */
34 * NOTE: some archetectures have malloc, realloc, etc. using size_t or
35 * an unsigned long instead of the traditional unsigned int. You may
36 * have to edit this by hand due to the difficulties I am having
37 * getting it to auto configure. Finding the definition of size_t in
38 * the /usr/include files may be a good guess. YEACH!!
40 * Uncomment the include file and set to the correct file which
41 * defines the size type, if necessary.
43 #define DMALLOC_SIZE unsigned long
44 /* #include <sys/types.h> */
46 /* this defines what type the standard void memory-pointer is */
47 #if defined(__STDC__) && __STDC__ == 1
48 #define DMALLOC_PNT void *
49 #define DMALLOC_FREE_RET void
51 #define DMALLOC_PNT void *
52 #define DMALLOC_FREE_RET void
56 * malloc function return codes
58 #define CALLOC_ERROR 0L /* error from calloc */
59 #define MALLOC_ERROR 0L /* error from malloc */
60 #define REALLOC_ERROR 0L /* error from realloc */
62 /* NOTE: this if for non- __STDC__ systems only */
63 #define FREE_ERROR 0 /* error from free */
64 #define FREE_NOERROR 1 /* no error from free */
66 #define DMALLOC_VERIFY_ERROR 0 /* checks failed, error */
67 #define DMALLOC_VERIFY_NOERROR 1 /* checks passed, no error */
70 * default values if _malloc_file and _malloc_line are not set
72 #define DMALLOC_DEFAULT_FILE 0L
73 #define DMALLOC_DEFAULT_LINE 0
74 #define DMALLOC_UNKNOWN_FILE "unknown"
77 * alloc macros to improve memory usage readibility...
80 #define ALLOC(type, count) \
81 (type *)malloc((DMALLOC_SIZE)(sizeof(type) * (count)))
84 #define MALLOC(size) \
85 (char *)malloc((DMALLOC_SIZE)(size))
87 /* NOTICE: the arguments are REVERSED from normal calloc() */
89 #define CALLOC(type, count) \
90 (type *)calloc((DMALLOC_SIZE)(count), (DMALLOC_SIZE)sizeof(type))
93 #define REALLOC(ptr, type, count) \
94 (type *)realloc((char *)(ptr), (DMALLOC_SIZE)(sizeof(type) * (count)))
97 #define REMALLOC(ptr, size) \
98 (char *)realloc((char *)(ptr), (DMALLOC_SIZE)(size))
104 /*<<<<<<<<<< The below prototypes are auto-generated by fillproto */
110 /* logfile for dumping dmalloc info, DMALLOC_LOGFILE env var overrides this */
111 extern char *dmalloc_logpath;
113 /* internal dmalloc error number for reference purposes only */
114 extern int dmalloc_errno;
116 /* address to look for. when discovered call dmalloc_error() */
117 extern DMALLOC_PNT dmalloc_address;
120 * argument to dmalloc_address, if 0 then never call dmalloc_error()
121 * else call it after seeing dmalloc_address for this many times.
123 extern int dmalloc_address_count;
126 * shutdown memory-allocation module, provide statistics if necessary
128 extern void dmalloc_shutdown(void);
131 * allocate and return a SIZE block of bytes. returns 0L on error.
133 extern DMALLOC_PNT malloc(DMALLOC_SIZE size);
136 * allocate and return a block of bytes able to hold NUM_ELEMENTS of elements
137 * of SIZE bytes and zero the block. returns 0L on error.
139 extern DMALLOC_PNT calloc(DMALLOC_SIZE num_elements, DMALLOC_SIZE size);
142 * resizes OLD_PNT to SIZE bytes and return the new space after either copying
143 * all of OLD_PNT to the new area or truncating. returns 0L on error.
145 extern DMALLOC_PNT realloc(DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size);
148 * release PNT in the heap, returning FREE_ERROR, FREE_NOERROR or void
149 * depending on whether STDC is defined by your compiler.
151 extern DMALLOC_FREE_RET free(DMALLOC_PNT pnt);
156 extern DMALLOC_FREE_RET cfree(DMALLOC_PNT pnt);
159 * log the heap structure plus information on the blocks if necessary.
161 extern void dmalloc_log_heap_map(void);
164 * dump dmalloc statistics to logfile
166 extern void dmalloc_log_stats(void);
169 * dump unfreed-memory info to logfile
171 extern void dmalloc_log_unfreed(void);
174 * verify pointer PNT, if PNT is 0 then check the entire heap.
175 * returns MALLOC_VERIFY_ERROR or MALLOC_VERIFY_NOERROR
177 extern int dmalloc_verify(const DMALLOC_PNT pnt);
180 * same as dmalloc_verify
182 extern int _malloc_verify(const DMALLOC_PNT pnt);
185 * set the global debug functionality flags to DEBUG (0 to disable).
186 * NOTE: after this module has started up, you cannot set certain flags
187 * such as fence-post or free-space checking.
189 extern void dmalloc_debug(const int debug);
192 * returns the current debug functionality flags. this allows you to
193 * save a dmalloc library state to be restored later.
195 extern int dmalloc_debug_current(void);
198 * examine pointer PNT and returns SIZE, and FILE / LINE info on it,
199 * or return-address RET_ADDR if any of the pointers are not 0L.
200 * if FILE returns 0L then RET_ATTR may have a value and vice versa.
201 * returns NOERROR or ERROR depending on whether PNT is good or not
203 extern int dmalloc_examine(const DMALLOC_PNT pnt, DMALLOC_SIZE * size,
204 char ** file, unsigned int * line,
205 DMALLOC_PNT * ret_attr);
208 * dmalloc version of strerror to return the string version of ERRNUM
209 * returns the string for MALLOC_BAD_ERRNO if ERRNUM is out-of-range.
211 extern char *dmalloc_strerror(const int errnum);
217 /*<<<<<<<<<< This is end of the auto-generated output from fillproto. */
220 * alloc macros to provide for memory FILE/LINE debugging information.
223 #ifndef DMALLOC_DISABLE
226 #define calloc(count, size) \
227 _calloc_leap(__FILE__, __LINE__, count, size)
230 _free_leap(__FILE__, __LINE__, ptr)
232 #define malloc(size) \
233 _malloc_leap(__FILE__, __LINE__, size)
235 #define realloc(ptr, size) \
236 _realloc_leap(__FILE__, __LINE__, ptr, size)
239 #define xcalloc(count, size) \
240 _xcalloc_leap(__FILE__, __LINE__, count, size)
243 _xfree_leap(__FILE__, __LINE__, ptr)
245 #define xmalloc(size) \
246 _xmalloc_leap(__FILE__, __LINE__, size)
248 #define xrealloc(ptr, size) \
249 _xrealloc_leap(__FILE__, __LINE__, ptr, size)
251 #define xstrdup(str) \
252 _xstrdup_leap(__FILE__, __LINE__, str)
254 #ifdef DMALLOC_FUNC_CHECK
257 * do debugging on the following functions. this may cause compilation or
258 * other problems depending on your architecture.
261 #define bcmp(b1, b2, len) _dmalloc_bcmp(b1, b2, len)
263 #define bcopy(from, to, len) _dmalloc_bcopy(from, to, len)
266 #define memcmp(b1, b2, len) _dmalloc_memcmp(b1, b2, len)
268 #define memcpy(to, from, len) _dmalloc_memcpy(to, from, len)
270 #define memset(buf, ch, len) _dmalloc_memset(buf, ch, len)
273 #define index(str, ch) _dmalloc_index(str, ch)
275 #define rindex(str, ch) _dmalloc_rindex(str, ch)
278 #define strcat(to, from) _dmalloc_strcat(to, from)
280 #define strcmp(s1, s2) _dmalloc_strcmp(s1, s2)
282 #define strlen(str) _dmalloc_strlen(str)
284 #define strtok(str, sep) _dmalloc_strtok(str, sep)
287 #define bzero(buf, len) _dmalloc_bzero(buf, len)
290 #define memccpy(s1, s2, ch, len) _dmalloc_memccpy(s1, s2, ch, len)
292 #define memchr(s1, ch, len) _dmalloc_memchr(s1, ch, len)
295 #define strchr(str, ch) _dmalloc_strchr(str, ch)
297 #define strrchr(str, ch) _dmalloc_strrchr(str, ch)
300 #define strcpy(to, from) _dmalloc_strcpy(to, from)
302 #define strncpy(to, from, len) _dmalloc_strncpy(to, from, len)
304 #define strcasecmp(s1, s2) _dmalloc_strcasecmp(s1, s2)
306 #define strncasecmp(s1, s2, len) _dmalloc_strncasecmp(s1, s2, len)
308 #define strspn(str, list) _dmalloc_strspn(str, list)
310 #define strcspn(str, list) _dmalloc_strcspn(str, list)
312 #define strncat(to, from, len) _dmalloc_strncat(to, from, len)
314 #define strncmp(s1, s2, len) _dmalloc_strncmp(s1, s2, len)
316 #define strpbrk(str, list) _dmalloc_strpbrk(str, list)
318 #define strstr(str, pat) _dmalloc_strstr(str, pat)
321 * feel free to add your favorite functions here and to arg_check.[ch]
329 * copied in from arg_check.h with IMPORT -> extern, all comments removed
331 extern int _dmalloc_bcmp(const void * b1, const void * b2,
332 const DMALLOC_SIZE len);
333 extern void _dmalloc_bcopy(const char * from, char * to,
334 const DMALLOC_SIZE len);
335 extern int _dmalloc_memcmp(const void * b1, const void * b2,
336 const DMALLOC_SIZE len);
337 extern char *_dmalloc_memcpy(char * to, const char * from,
338 const DMALLOC_SIZE len);
339 extern char *_dmalloc_memset(void * buf, const char ch,
340 const DMALLOC_SIZE len);
341 extern char *_dmalloc_index(const char * str, const char ch);
342 extern char *_dmalloc_rindex(const char * str, const char ch);
343 extern char *_dmalloc_strcat(char * to, const char * from);
344 extern int _dmalloc_strcmp(const char * s1, const char * s2);
345 extern DMALLOC_SIZE _dmalloc_strlen(const char * str);
346 extern char *_dmalloc_strtok(char * str, const char * sep);
347 extern void _dmalloc_bzero(void * buf, const DMALLOC_SIZE len);
348 extern char *_dmalloc_memccpy(char * s1, const char * s2, const char ch,
349 const DMALLOC_SIZE len);
350 extern char *_dmalloc_memchr(const char * s1, const char ch,
351 const DMALLOC_SIZE len);
352 extern char *_dmalloc_strchr(const char * str, const char ch);
353 extern char *_dmalloc_strrchr(const char * str, const char ch);
354 extern char *_dmalloc_strcpy(char * to, const char * from);
355 extern char *_dmalloc_strncpy(char * to, const char * from,
356 const DMALLOC_SIZE len);
357 extern int _dmalloc_strcasecmp(const char * s1, const char * s2);
358 extern int _dmalloc_strncasecmp(const char * s1, const char * s2,
359 const DMALLOC_SIZE len);
360 extern int _dmalloc_strspn(const char * str, const char * list);
361 extern int _dmalloc_strcspn(const char * str, const char * list);
362 extern char *_dmalloc_strncat(char * to, const char * from,
363 const DMALLOC_SIZE len);
364 extern int _dmalloc_strncmp(const char * s1, const char * s2,
365 const DMALLOC_SIZE len);
366 extern char *_dmalloc_strpbrk(const char * str, const char * list);
367 extern char *_dmalloc_strstr(const char * str, const char * pat);
373 #endif /* DMALLOC_FUNC_CHECK */
380 * copied directly from malloc_lp.h with IMPORT -> extern, routines trimmed
382 /* to inform the dmalloc library from which file the call comes from */
383 extern char *_dmalloc_file;
385 /* to inform the library from which line-number the call comes from */
386 extern unsigned int _dmalloc_line;
389 * leap routine to calloc
391 extern DMALLOC_PNT _calloc_leap(const char * file, const int line,
392 DMALLOC_SIZE elen, DMALLOC_SIZE size);
395 * leap routine to free
397 extern DMALLOC_FREE_RET _free_leap(const char * file, const int line,
401 * leap routine to malloc
403 extern DMALLOC_PNT _malloc_leap(const char * file, const int line,
407 * leap routine to realloc
409 extern DMALLOC_PNT _realloc_leap(const char * file, const int line,
410 DMALLOC_PNT oldp, DMALLOC_SIZE new_size);
413 * leap routine to calloc with error checking
415 extern DMALLOC_PNT _xcalloc_leap(const char * file, const int line,
416 DMALLOC_SIZE elen, DMALLOC_SIZE size);
419 * leap routine to free
421 extern DMALLOC_FREE_RET _xfree_leap(const char * file, const int line,
425 * leap routine to malloc with error checking
427 extern DMALLOC_PNT _xmalloc_leap(const char * file, const int line,
431 * leap routine to realloc with error checking
433 extern DMALLOC_PNT _xrealloc_leap(const char * file, const int line,
434 DMALLOC_PNT oldp, DMALLOC_SIZE new_size);
437 * leap routine for strdup with error checking
439 extern char *_xstrdup_leap(const char * file, const int line,
446 #endif /* ! DMALLOC_DISABLE */
448 #endif /* ! __DMALLOC_H__ */