diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2007-07-25 23:13:18 +0000 |
---|---|---|
committer | Nicholas Marriott <nicholas.marriott@gmail.com> | 2007-07-25 23:13:18 +0000 |
commit | 4b62b1d16caec36c4c843108c40cde25f9aa64e4 (patch) | |
tree | 04559a4d50e22759417f5109f1d8bf3535f105cc /xmalloc.c | |
parent | a3bfe208b3ccea28676917d3b41fb575b19484ef (diff) |
Sync with fdm.
Diffstat (limited to 'xmalloc.c')
-rw-r--r-- | xmalloc.c | 96 |
1 files changed, 39 insertions, 57 deletions
@@ -1,4 +1,4 @@ -/* $Id: xmalloc.c,v 1.1.1.1 2007-07-09 19:03:33 nicm Exp $ */ +/* $Id: xmalloc.c,v 1.2 2007-07-25 23:13:18 nicm Exp $ */ /* * Copyright (c) 2004 Nicholas Marriott <nicm@users.sourceforge.net> @@ -16,14 +16,10 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <sys/types.h> +#include <sys/param.h> #include <errno.h> #include <libgen.h> -#include <limits.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdint.h> #include <stdlib.h> #include <string.h> @@ -33,10 +29,10 @@ void * ensure_for(void *buf, size_t *len, size_t size, size_t adj) { if (adj == 0) - log_fatalx("ensure_for: zero adj"); + fatalx("zero adj"); if (SIZE_MAX - size < adj) - log_fatalx("ensure_for: size + adj > SIZE_MAX"); + fatalx("size + adj > SIZE_MAX"); size += adj; if (*len == 0) { @@ -56,9 +52,9 @@ void * ensure_size(void *buf, size_t *len, size_t nmemb, size_t size) { if (nmemb == 0 || size == 0) - log_fatalx("ensure_size: zero size"); + fatalx("zero size"); if (SIZE_MAX / nmemb < size) - log_fatalx("ensure_size: nmemb * size > SIZE_MAX"); + fatalx("nmemb * size > SIZE_MAX"); if (*len == 0) { *len = BUFSIZ; @@ -91,25 +87,31 @@ xcalloc(size_t nmemb, size_t size) void *ptr; if (size == 0 || nmemb == 0) - log_fatalx("xcalloc: zero size"); + fatalx("zero size"); if (SIZE_MAX / nmemb < size) - log_fatalx("xcalloc: nmemb * size > SIZE_MAX"); + fatalx("nmemb * size > SIZE_MAX"); if ((ptr = calloc(nmemb, size)) == NULL) - log_fatal("xcalloc"); + fatal("xcalloc failed"); +#ifdef DEBUG + xmalloc_new(xmalloc_caller(), ptr, nmemb * size); +#endif return (ptr); } void * xmalloc(size_t size) { - void *ptr; + void *ptr; if (size == 0) - log_fatalx("xmalloc: zero size"); + fatalx("zero size"); if ((ptr = malloc(size)) == NULL) - log_fatal("xmalloc"); + fatal("xmalloc failed"); +#ifdef DEBUG + xmalloc_new(xmalloc_caller(), ptr, size); +#endif return (ptr); } @@ -120,12 +122,15 @@ xrealloc(void *oldptr, size_t nmemb, size_t size) void *newptr; if (newsize == 0) - log_fatalx("xrealloc: zero size"); + fatalx("zero size"); if (SIZE_MAX / nmemb < size) - log_fatal("xrealloc: nmemb * size > SIZE_MAX"); + fatalx("nmemb * size > SIZE_MAX"); if ((newptr = realloc(oldptr, newsize)) == NULL) - log_fatal("xrealloc"); + fatal("xrealloc failed"); +#ifdef DEBUG + xmalloc_change(xmalloc_caller(), oldptr, newptr, nmemb * size); +#endif return (newptr); } @@ -133,8 +138,12 @@ void xfree(void *ptr) { if (ptr == NULL) - log_fatalx("xfree: null pointer"); + fatalx("null pointer"); free(ptr); + +#ifdef DEBUG + xmalloc_free(ptr); +#endif } int printflike2 @@ -156,10 +165,12 @@ xvasprintf(char **ret, const char *fmt, va_list ap) int i; i = vasprintf(ret, fmt, ap); - if (i < 0 || *ret == NULL) - log_fatal("xvasprintf"); + fatal("xvasprintf failed"); +#ifdef DEBUG + xmalloc_new(xmalloc_caller(), *ret, i + 1); +#endif return (i); } @@ -181,53 +192,24 @@ xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { int i; - if (len > INT_MAX) { - errno = EINVAL; - log_fatal("xvsnprintf"); - } + if (len > INT_MAX) + fatalx("len > INT_MAX"); i = vsnprintf(buf, len, fmt, ap); - if (i < 0) - log_fatal("xvsnprintf"); + fatal("vsnprintf failed"); return (i); } /* - * Print a path. Same as xsnprintf, but return ENAMETOOLONG on truncation. - */ -int printflike3 -printpath(char *buf, size_t len, const char *fmt, ...) -{ - va_list ap; - int n; - - if (len > INT_MAX) { - errno = ENAMETOOLONG; - return (1); - } - - va_start(ap, fmt); - n = xvsnprintf(buf, len, fmt, ap); - va_end(ap); - - if ((size_t) n > len) { - errno = ENAMETOOLONG; - return (1); - } - - return (0); -} - -/* - * Some system modify the path in place. This function and xbasename below + * Some systems modify the path in place. This function and xbasename below * avoid that by using a temporary buffer. */ char * xdirname(const char *src) { - char dst[MAXPATHLEN]; + static char dst[MAXPATHLEN]; strlcpy(dst, src, sizeof dst); return (dirname(dst)); @@ -236,7 +218,7 @@ xdirname(const char *src) char * xbasename(const char *src) { - char dst[MAXPATHLEN]; + static char dst[MAXPATHLEN]; strlcpy(dst, src, sizeof dst); return (basename(dst)); |