diff --git a/error.c b/error.c index 4faaee10..62f2a207 100644 --- a/error.c +++ b/error.c @@ -18,7 +18,7 @@ * * The author may be contacted via http://dmalloc.com/ * - * $Id: error.c,v 1.103 2003/05/16 04:09:13 gray Exp $ + * $Id: error.c,v 1.104 2003/05/19 18:14:15 gray Exp $ */ /* @@ -93,11 +93,7 @@ extern const char *dmalloc_strerror(const int errnum); /* * exported variables */ -/* internal dmalloc error number for reference purposes only */ -int dmalloc_errno = ERROR_NONE; -/* logfile for dumping dmalloc info, DMALLOC_LOGFILE env var overrides this */ -char *dmalloc_logpath = NULL; /* address to look for. when discovered call dmalloc_error() */ DMALLOC_PNT _dmalloc_address = NULL; /* when to stop at an address */ @@ -349,7 +345,7 @@ char *_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size, #endif /* - * void dmalloc_vmessage + * void _dmalloc_vmessage * * DESCRIPTION: * @@ -366,7 +362,7 @@ char *_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size, * * args -> Already converted pointer to a stdarg list. */ -void dmalloc_vmessage(const char *format, va_list args) +void _dmalloc_vmessage(const char *format, va_list args) { char str[1024], *str_p, *bounds_p; int len; @@ -439,34 +435,6 @@ void dmalloc_vmessage(const char *format, va_list args) } } -/* - * void dmalloc_message - * - * DESCRIPTION: - * - * Message writer with printf like arguments which adds a line to the - * dmalloc logfile. - * - * RETURNS: - * - * None. - * - * ARGUMENTS: - * - * format -> Printf-style format statement. - * - * ... -> Variable argument list. - */ -void dmalloc_message(const char *format, ...) - /* __attribute__ ((format (printf, 1, 2))) */ -{ - va_list args; - - va_start(args, format); - dmalloc_vmessage(format, args); - va_end(args); -} - /* * void _dmalloc_die * diff --git a/error.h b/error.h index aeebf331..af77971a 100644 --- a/error.h +++ b/error.h @@ -18,7 +18,7 @@ * * The author may be contacted via http://dmalloc.com/ * - * $Id: error.h,v 1.51 2003/05/16 04:09:13 gray Exp $ + * $Id: error.h,v 1.52 2003/05/19 18:14:16 gray Exp $ */ #ifndef __ERROR_H__ @@ -44,14 +44,6 @@ /*<<<<<<<<<< The below prototypes are auto-generated by fillproto */ -/* internal dmalloc error number for reference purposes only */ -extern -int dmalloc_errno; - -/* logfile for dumping dmalloc info, DMALLOC_LOGFILE env var overrides this */ -extern -char *dmalloc_logpath; - /* address to look for. when discovered call dmalloc_error() */ extern DMALLOC_PNT _dmalloc_address; @@ -188,7 +180,7 @@ char *_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size, #endif /* if STORE_TIMEVAL == 0 && HAVE_TIME */ /* - * void dmalloc_vmessage + * void _dmalloc_vmessage * * DESCRIPTION: * @@ -206,32 +198,7 @@ char *_dmalloc_ptime(const TIME_TYPE *time_p, char *buf, const int buf_size, * args -> Already converted pointer to a stdarg list. */ extern -void dmalloc_vmessage(const char *format, va_list args); - -/* - * void dmalloc_message - * - * DESCRIPTION: - * - * Message writer with printf like arguments which adds a line to the - * dmalloc logfile. - * - * RETURNS: - * - * None. - * - * ARGUMENTS: - * - * format -> Printf-style format statement. - * - * ... -> Variable argument list. - */ -extern -void dmalloc_message(const char *format, ...) -#ifdef __GNUC__ - __attribute__ ((format (printf, 1, 2))) -#endif -; +void _dmalloc_vmessage(const char *format, va_list args); /* * void _dmalloc_die diff --git a/malloc.c b/malloc.c index ca79080f..f0eada82 100644 --- a/malloc.c +++ b/malloc.c @@ -18,7 +18,7 @@ * * The author may be contacted via http://dmalloc.com/ * - * $Id: malloc.c,v 1.163 2003/05/16 04:09:12 gray Exp $ + * $Id: malloc.c,v 1.164 2003/05/19 18:14:16 gray Exp $ */ /* @@ -88,6 +88,14 @@ static char *information = "@(#) $Information: lock-threads is enabled $" #endif #endif +/* exported variables */ + +/* internal dmalloc error number for reference purposes only */ +int dmalloc_errno = ERROR_NONE; + +/* logfile for dumping dmalloc info, DMALLOC_LOGFILE env var overrides this */ +char *dmalloc_logpath = NULL; + /* local variables */ static int enabled_b = 0; /* have we started yet? */ static int in_alloc_b = 0; /* can't be here twice */ @@ -1314,10 +1322,8 @@ int malloc_verify(const DMALLOC_PNT pnt) * Set the global debug functionality flags. You can also use * dmalloc_debug_setup. * - * Note: you cannot remove certain flags such as signal handlers since - * they are setup at initialization time only. Also you cannot add - * certain flags such as free-space checking since they must be on - * from the start. + * Note: you cannot add or remove certain flags such as signal + * handlers since they are setup at initialization time only. * * RETURNS: * @@ -1616,6 +1622,57 @@ void dmalloc_log_changed(const unsigned long mark, const int not_freed_b, dmalloc_out(); } +/* + * void dmalloc_vmessage + * + * DESCRIPTION: + * + * Message writer with vprintf like arguments which adds a line to the + * dmalloc logfile. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * format -> Printf-style format statement. + * + * args -> Already converted pointer to a stdarg list. + */ +void dmalloc_vmessage(const char *format, va_list args) +{ + _dmalloc_vmessage(format, args); +} + +/* + * void dmalloc_message + * + * DESCRIPTION: + * + * Message writer with printf like arguments which adds a line to the + * dmalloc logfile. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * format -> Printf-style format statement. + * + * ... -> Variable argument list. + */ +void dmalloc_message(const char *format, ...) + /* __attribute__ ((format (printf, 1, 2))) */ +{ + va_list args; + + va_start(args, format); + _dmalloc_vmessage(format, args); + va_end(args); +} + /* * const char *dmalloc_strerror * diff --git a/malloc.h b/malloc.h new file mode 100644 index 00000000..7c6c2812 --- /dev/null +++ b/malloc.h @@ -0,0 +1,700 @@ +/* + * Malloc.h. + * + * This file is built into dmalloc.h and is not to be included by any + * other .c or .h file. + */ + +/*<<<<<<<<<< The below prototypes are auto-generated by fillproto */ + +/* internal dmalloc error number for reference purposes only */ +extern +int dmalloc_errno; + +/* logfile for dumping dmalloc info, DMALLOC_LOGFILE env var overrides this */ +extern +char *dmalloc_logpath; + +/* + * void dmalloc_shutdown + * + * DESCRIPTION: + * + * Shutdown the dmalloc library and provide statistics if necessary. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * None. + */ +extern +void dmalloc_shutdown(void); + +#if FINI_DMALLOC +/* + * void __fini_dmalloc + * + * DESCRIPTION: + * + * Automatic function to close dmalloc supported by some operating + * systems. Pretty cool OS/compiler hack. By default it is not + * necessary because we use atexit() and on_exit() to register the + * close functions which are more portable. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * None. + */ +extern +void __fini_dmalloc(void); +#endif /* if FINI_DMALLOC */ + +/* + * DMALLOC_PNT dmalloc_malloc + * + * DESCRIPTION: + * + * Allocate and return a memory block of a certain size. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * file -> File-name or return-address of the caller. + * + * line -> Line-number of the caller. + * + * size -> Number of bytes requested. + * + * func_id -> Function-id to identify the type of call. See + * dmalloc.h. + * + * alignment -> To align the new block to a certain number of bytes, + * set this to a value greater than 0. + * + * xalloc_b -> If set to 1 then print an error and exit if we run out + * of memory. + */ +extern +DMALLOC_PNT dmalloc_malloc(const char *file, const int line, + const DMALLOC_SIZE size, const int func_id, + const DMALLOC_SIZE alignment, + const int xalloc_b); + +/* + * DMALLOC_PNT dmalloc_realloc + * + * DESCRIPTION: + * + * Resizes and old pointer to a new number of bytes. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * file -> File-name or return-address of the caller. + * + * line -> Line-number of the caller. + * + * old_pnt -> Pointer to an existing memory chunk that we are + * resizing. If this is NULL then it basically does a malloc. + * + * new_size -> New number of bytes requested for the old pointer. + * + * func_id -> Function-id to identify the type of call. See + * dmalloc.h. + * + * xalloc_b -> If set to 1 then print an error and exit if we run out + * of memory. + */ +extern +DMALLOC_PNT dmalloc_realloc(const char *file, const int line, + DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size, + const int func_id, const int xalloc_b); + +/* + * int dmalloc_free + * + * DESCRIPTION: + * + * Release a pointer back into the heap. + * + * RETURNS: + * + * Success - FREE_NOERROR + * + * Failure - FREE_ERROR + * + * Note: many operating systems define free to return (void) so this + * return values may be filtered. Dumb. + * + * ARGUMENTS: + * + * file -> File-name or return-address of the caller. + * + * line -> Line-number of the caller. + * + * pnt -> Existing pointer we are freeing. + * + * func_id -> Function-id to identify the type of call. See + * dmalloc.h. + */ +extern +int dmalloc_free(const char *file, const int line, DMALLOC_PNT pnt, + const int func_id); + +/* + * DMALLOC_PNT dmalloc_strdup + * + * DESCRIPTION: + * + * Allocate and return an allocated block of memory holding a copy of + * a string. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * file -> File-name or return-address of the caller. + * + * line -> Line-number of the caller. + * + * string -> String we are duplicating. + * + * xalloc_b -> If set to 1 then print an error and exit if we run out + * of memory. + */ +extern +DMALLOC_PNT dmalloc_strdup(const char *file, const int line, + const char *string, const int xalloc_b); + +/* + * DMALLOC_PNT malloc + * + * DESCRIPTION: + * + * Overloading the malloc(3) function. Allocate and return a memory + * block of a certain size. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * size -> Number of bytes requested. + */ +extern +DMALLOC_PNT malloc(DMALLOC_SIZE size); + +/* + * DMALLOC_PNT malloc + * + * DESCRIPTION: + * + * Overloading the calloc(3) function. Returns a block of zeroed memory. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * num_elements -> Number of elements being allocated. + * + * size -> The number of bytes in each element. + */ +extern +DMALLOC_PNT calloc(DMALLOC_SIZE num_elements, DMALLOC_SIZE size); + +/* + * DMALLOC_PNT realloc + * + * DESCRIPTION: + * + * Overload of realloc(3). Resizes and old pointer to a new number of + * bytes. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * old_pnt -> Pointer to an existing memory chunk that we are + * resizing. If this is NULL then it basically does a malloc. + * + * new_size -> New number of bytes requested for the old pointer. + */ +extern +DMALLOC_PNT realloc(DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size); + +/* + * DMALLOC_PNT recalloc + * + * DESCRIPTION: + * + * Overload of recalloc(3) which exists on some systems. Resizes and + * old pointer to a new number of bytes. If we are expanding, then + * any new bytes will be zeroed. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * old_pnt -> Pointer to an existing memory chunk that we are + * resizing. + * + * new_size -> New number of bytes requested for the old pointer. + */ +extern +DMALLOC_PNT recalloc(DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size); + +/* + * DMALLOC_PNT memalign + * + * DESCRIPTION: + * + * Overloading the memalign(3) function. Allocate and return a memory + * block of a certain size which have been aligned to a certain + * alignment. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * alignment -> Value to which the allocation must be aligned. This + * should probably be a multiple of 2 with a maximum value equivalent + * to the block-size which is often 1k or 4k. + * + * size -> Number of bytes requested. + */ +extern +DMALLOC_PNT memalign(DMALLOC_SIZE alignment, DMALLOC_SIZE size); + +/* + * DMALLOC_PNT valloc + * + * DESCRIPTION: + * + * Overloading the valloc(3) function. Allocate and return a memory + * block of a certain size which have been aligned to page boundaries + * which are often 1k or 4k. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * size -> Number of bytes requested. + */ +extern +DMALLOC_PNT valloc(DMALLOC_SIZE size); + +#ifndef DMALLOC_STRDUP_MACRO +/* + * DMALLOC_PNT strdup + * + * DESCRIPTION: + * + * Overload of strdup(3). Allocate and return an allocated block of + * memory holding a copy of a string. + * + * RETURNS: + * + * Success - Valid pointer. + * + * Failure - 0L + * + * ARGUMENTS: + * + * string -> String we are duplicating. + */ +extern +char *strdup(const char *string); +#endif /* ifndef DMALLOC_STRDUP_MACRO */ + +/* + * DMALLOC_FREE_RET free + * + * DESCRIPTION: + * + * Release a pointer back into the heap. + * + * RETURNS: + * + * Returns FREE_ERROR, FREE_NOERROR or void depending on whether STDC + * is defined by your compiler. + * + * ARGUMENTS: + * + * pnt -> Existing pointer we are freeing. + */ +extern +DMALLOC_FREE_RET free(DMALLOC_PNT pnt); + +/* + * DMALLOC_FREE_RET cfree + * + * DESCRIPTION: + * + * Same as free. + * + * RETURNS: + * + * Returns FREE_ERROR, FREE_NOERROR or void depending on whether STDC + * is defined by your compiler. + * + * ARGUMENTS: + * + * pnt -> Existing pointer we are freeing. + */ +extern +DMALLOC_FREE_RET cfree(DMALLOC_PNT pnt); + +/* + * int dmalloc_verify + * + * DESCRIPTION: + * + * Verify a pointer which has previously been allocated by the + * library. + * + * RETURNS: + * + * Success - MALLOC_VERIFY_NOERROR + * + * Failure - MALLOC_VERIFY_ERROR + * + * ARGUMENTS: + * + * pnt -> Pointer we are verifying. If 0L then check the entire heap. + */ +extern +int dmalloc_verify(const DMALLOC_PNT pnt); + +/* + * int malloc_verify + * + * DESCRIPTION: + * + * Verify a pointer which has previously been allocated by the + * library. Same as dmalloc_verify. + * + * RETURNS: + * + * Success - MALLOC_VERIFY_NOERROR + * + * Failure - MALLOC_VERIFY_ERROR + * + * ARGUMENTS: + * + * pnt -> Pointer we are verifying. If 0L then check the entire heap. + */ +extern +int malloc_verify(const DMALLOC_PNT pnt); + +/* + * unsigned int dmalloc_debug + * + * DESCRIPTION: + * + * Set the global debug functionality flags. You can also use + * dmalloc_debug_setup. + * + * Note: you cannot add or remove certain flags such as signal + * handlers since they are setup at initialization time only. + * + * RETURNS: + * + * The old debug flag value. + * + * ARGUMENTS: + * + * flags -> Flag value to set. Pass in 0 to disable all debugging. + */ +extern +unsigned int dmalloc_debug(const unsigned int flags); + +/* + * unsigned int dmalloc_debug_current + * + * DESCRIPTION: + * + * Returns the current debug functionality flags. This allows you to + * save a dmalloc library state to be restored later. + * + * RETURNS: + * + * Current debug flags. + * + * ARGUMENTS: + * + * None. + */ +extern +unsigned int dmalloc_debug_current(void); + +/* + * void dmalloc_debug_setup + * + * DESCRIPTION: + * + * Set the global debugging functionality as an option string. + * Normally this would be pased in in the DMALLOC_OPTIONS + * environmental variable. This is here to override the env or for + * circumstances where modifying the environment is not possible or + * does not apply such as servers or cgi-bin programs. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * options_str -> Options string to set the library flags. + */ +extern +void dmalloc_debug_setup(const char *options_str); + +/* + * int dmalloc_examine + * + * DESCRIPTION: + * + * Examine a pointer and pass back information on its allocation size + * as well as the file and line-number where it was allocated. If the + * file and line number is not available, then it will pass back the + * allocation location's return-address if available. + * + * RETURNS: + * + * Success - DMALLOC_NOERROR + * + * Failure - DMALLOC_ERROR + * + * ARGUMENTS: + * + * pnt -> Pointer we are checking. + * + * size_p <- Pointer to an unsigned int which, if not NULL, will be + * set to the size of bytes from the pointer. + * + * file_p <- Pointer to a character pointer which, if not NULL, will + * be set to the file where the pointer was allocated. + * + * line_p <- Pointer to a character pointer which, if not NULL, will + * be set to the line-number where the pointer was allocated. + * + * ret_attr_p <- Pointer to a void pointer, if not NULL, will be set + * to the return-address where the pointer was allocated. + */ +extern +int dmalloc_examine(const DMALLOC_PNT pnt, DMALLOC_SIZE *size_p, + char **file_p, unsigned int *line_p, + DMALLOC_PNT *ret_attr_p); + +/* + * void dmalloc_track + * + * DESCRIPTION: + * + * Register an allocation tracking function which will be called each + * time an allocation occurs. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * track_func -> Function to register as the tracking function. Set + * to NULL to disable. + */ +extern +void dmalloc_track(const dmalloc_track_t track_func); + +/* + * unsigned long dmalloc_mark + * + * DESCRIPTION: + * + * Return to the caller the current "mark" which can be used later by + * dmalloc_log_changed to log the changed pointers since this point. + * Multiple marks can be saved and used. + * + * RETURNS: + * + * Current mark value + * + * ARGUMENTS: + * + * None. + */ +extern +unsigned long dmalloc_mark(void); + +/* + * void dmalloc_log_status + * + * DESCRIPTION: + * + * Dump dmalloc statistics to logfile. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * None. + */ +extern +void dmalloc_log_stats(void); + +/* + * void dmalloc_log_unfreed + * + * DESCRIPTION: + * + * Dump unfreed-memory info to logfile. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * None. + */ +extern +void dmalloc_log_unfreed(void); + +/* + * void dmalloc_log_changed + * + * DESCRIPTION: + * + * Dump the pointers that have changed since a point in time. + * + * RETURNS: + * + * mark -> Sets the point to compare against. You can use + * dmalloc_mark to get the current mark value which can later be + * passed in here. Pass in 0 to log what has changed since the + * program started. + * + * not_freed_b -> Set to 1 to log the new pointers that are non-freed. + * + * free_b -> Set to 1 to log the new pointers that are freed. + * + * details_b -> Set to 1 to dump the individual pointers that have + * changed otherwise the summaries will be logged. + */ +extern +void dmalloc_log_changed(const unsigned long mark, const int not_freed_b, + const int free_b, const int details_b); + +/* + * void dmalloc_vmessage + * + * DESCRIPTION: + * + * Message writer with vprintf like arguments which adds a line to the + * dmalloc logfile. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * format -> Printf-style format statement. + * + * args -> Already converted pointer to a stdarg list. + */ +extern +void dmalloc_vmessage(const char *format, va_list args); + +/* + * void dmalloc_message + * + * DESCRIPTION: + * + * Message writer with printf like arguments which adds a line to the + * dmalloc logfile. + * + * RETURNS: + * + * None. + * + * ARGUMENTS: + * + * format -> Printf-style format statement. + * + * ... -> Variable argument list. + */ +extern +void dmalloc_message(const char *format, ...) +#ifdef __GNUC__ + __attribute__ ((format (printf, 1, 2))) +#endif +; + +/* + * const char *dmalloc_strerror + * + * DESCRIPTION: + * + * Convert a dmalloc error code into its string equivalent. + * + * RETURNS: + * + * Success - String version of the error + * + * Failure - The string "unknown error" + * + * ARGUMENTS: + * + * error_num -> Error number we are converting. + */ +extern +const char *dmalloc_strerror(const int error_num); + +/*<<<<<<<<<< This is end of the auto-generated output from fillproto. */