From owner-p4-projects Sat Jun 29 0:57:12 2002 Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id EC32937B401; Sat, 29 Jun 2002 00:56:46 -0700 (PDT) Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8E5E237B400 for ; Sat, 29 Jun 2002 00:56:46 -0700 (PDT) Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3A78243E13 for ; Sat, 29 Jun 2002 00:56:46 -0700 (PDT) (envelope-from peter@freebsd.org) Received: from freefall.freebsd.org (perforce@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.4/8.12.4) with ESMTP id g5T7ujJU026880 for ; Sat, 29 Jun 2002 00:56:45 -0700 (PDT) (envelope-from peter@freebsd.org) Received: (from perforce@localhost) by freefall.freebsd.org (8.12.4/8.12.4/Submit) id g5T7uj78026877 for perforce@freebsd.org; Sat, 29 Jun 2002 00:56:45 -0700 (PDT) Date: Sat, 29 Jun 2002 00:56:45 -0700 (PDT) Message-Id: <200206290756.g5T7uj78026877@freefall.freebsd.org> X-Authentication-Warning: freefall.freebsd.org: perforce set sender to peter@freebsd.org using -f From: Peter Wemm Subject: PERFORCE change 13580 for review To: Perforce Change Reviews Sender: owner-p4-projects@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG http://people.freebsd.org/~peter/p4db/chv.cgi?CH=13580 Change 13580 by peter@peter_ia64 on 2002/06/29 00:56:12 before I lose it: Make libz use stand.h instead of libc's headers Implement malloc debugging. Sigh, I have triggered a heisenbug here. Adding debugging makes it work. Affected files ... .. //depot/projects/ia64/lib/libstand/Makefile#8 edit .. //depot/projects/ia64/lib/libstand/stand.h#7 edit .. //depot/projects/ia64/lib/libstand/zalloc_defs.h#2 edit .. //depot/projects/ia64/lib/libstand/zalloc_malloc.c#2 edit Differences ... ==== //depot/projects/ia64/lib/libstand/Makefile#8 (text+ko) ==== @@ -13,6 +13,7 @@ MAN= libstand.3 CFLAGS+= -ffreestanding +CFLAGS+= -I${.CURDIR} .if ${MACHINE_ARCH} == "alpha" CFLAGS+= -mno-fp-regs @@ -25,7 +26,7 @@ .endif # standalone components and stuff we have modified locally -SRCS+= __main.c assert.c bcd.c bswap.c environment.c getopt.c gets.c \ +SRCS+= zutil.h __main.c assert.c bcd.c bswap.c environment.c getopt.c gets.c \ globals.c pager.c printf.c strdup.c strerror.c strtol.c random.c \ sbrk.c twiddle.c zalloc.c zalloc_malloc.c @@ -132,9 +133,22 @@ # decompression functionality from libz .PATH: ${.CURDIR}/../libz -CFLAGS+=-DHAVE_MEMCPY -SRCS+= adler32.c crc32.c infblock.c infcodes.c inffast.c inflate.c \ - inftrees.c infutil.c zutil.c +CFLAGS+=-DHAVE_MEMCPY -I${.CURDIR}/../libz +SRCS+= adler32.c crc32.c _infblock.c _infcodes.c _inffast.c _inflate.c \ + _inftrees.c _infutil.c _zutil.c _zutil.h + +# aargh +.for file in zutil.h +CLEANFILES+= _${file} _${file}.orig + +_${file}: ${file} ${file}.diff + patch -s -b .orig -o ${.TARGET} < ${.ALLSRC:M*.diff} ${.ALLSRC:M*.[ch]} +.endfor + +.for file in infblock.c infcodes.c inffast.c inflate.c inftrees.c infutil.c zutil.c +_${file}: ${file} + sed "s|zutil\.h|_zutil.h|" ${.ALLSRC} > ${.TARGET} +.endfor # io routines SRCS+= closeall.c dev.c ioctl.c nullfs.c stat.c \ ==== //depot/projects/ia64/lib/libstand/stand.h#7 (text+ko) ==== @@ -391,26 +391,19 @@ #define ntohs(x) __ntohs(x) #endif -#if 0 +void *Malloc(size_t, char *, int); +void *Calloc(size_t, size_t, char *, int); +void *Realloc(void *, size_t, char *, int); +void Free(void *, char *, int); -static inline void * -malloc_debug(size_t size, const char *file, int line) -{ - void *p; - printf("%s:%d malloc(%ld)", file, line, size); - p = malloc(size); - printf("=%p\n", p); - return p; -} - -static inline void -free_debug(void *p, const char *file, int line) -{ - printf("%s:%d free(%p)\n", file, line, p); - free(p); -} - -#define malloc(x) malloc_debug(x, __FILE__, __LINE__) -#define free(x) free_debug(x, __FILE__, __LINE__) - +#if 1 +#define malloc(x) Malloc(x, __FILE__, __LINE__) +#define calloc(x, y) Calloc(x, y, __FILE__, __LINE__) +#define free(x) Free(x, __FILE__, __LINE__) +#define realloc(x, y) Realloc(x, y, __FILE__, __LINE__) +#else +#define malloc(x) Malloc(x, NULL 0) +#define calloc(x, y) Calloc(x, y, NULL, 0) +#define free(x) Free(x, NULL, 0) +#define realloc(x, y) Realloc(x, y, NULL, 0) #endif ==== //depot/projects/ia64/lib/libstand/zalloc_defs.h#2 (text+ko) ==== @@ -96,6 +96,7 @@ #define MATYPE long double #define MALLOCALIGN ((sizeof(MATYPE) > sizeof(Guard)) ? sizeof(MATYPE) : sizeof(Guard)) #define GAMAGIC 0x55FF44FD +#define GAFREE 0x5F54F4DF #include "zalloc_protos.h" ==== //depot/projects/ia64/lib/libstand/zalloc_malloc.c#2 (text+ko) ==== @@ -60,14 +60,14 @@ #endif void * -malloc(size_t bytes) +Malloc(size_t bytes, char *file, int line) { Guard *res; #ifdef USEENDGUARD - bytes += MALLOCALIGN + 1; + bytes += 2 * MALLOCALIGN + 1; #else - bytes += MALLOCALIGN; + bytes += 2 * MALLOCALIGN; #endif while ((res = znalloc(&MallocPool, bytes)) == NULL) { @@ -94,21 +94,31 @@ } void -free(void *ptr) +Free(void *ptr, char *file, int line) { size_t bytes; if (ptr != NULL) { Guard *res = (void *)((char *)ptr - MALLOCALIGN); + if (file == NULL) + file = "unknown"; #ifdef USEGUARD + if (res->ga_Magic == GAFREE) { + printf("free: duplicate free @ %p from %s:%d\n", ptr, file, line); + return; + } if (res->ga_Magic != GAMAGIC) - panic("free: guard1 fail @ %p", ptr); - res->ga_Magic = -1; + panic("free: guard1 fail @ %p from %s:%p", ptr, file, line); + res->ga_Magic = GAFREE; #endif #ifdef USEENDGUARD + if (*((char *)res + res->ga_Bytes - 1) == -1) { + printf("free: duplicate2 free @ %p from %s:%d\n", ptr, file, line); + return; + } if (*((char *)res + res->ga_Bytes - 1) != -2) - panic("free: guard2 fail @ %p + %d", ptr, res->ga_Bytes - MALLOCALIGN); + panic("free: guard2 fail @ %p + %d from %s:%d", ptr, res->ga_Bytes - MALLOCALIGN, file, line); *((char *)res + res->ga_Bytes - 1) = -1; #endif @@ -122,12 +132,12 @@ void * -calloc(size_t n1, size_t n2) +Calloc(size_t n1, size_t n2, char *file, int line) { iaddr_t bytes = (iaddr_t)n1 * (iaddr_t)n2; void *res; - if ((res = malloc(bytes)) != NULL) { + if ((res = Malloc(bytes, file, line)) != NULL) { bzero(res, bytes); #ifdef DMALLOCDEBUG if (++MallocCount > MallocMax) @@ -144,19 +154,19 @@ */ void * -realloc(void *ptr, size_t size) +Realloc(void *ptr, size_t size, char *file, int line) { void *res; size_t old; - if ((res = malloc(size)) != NULL) { + if ((res = Malloc(size, file, line)) != NULL) { if (ptr) { old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN; if (old < size) bcopy(ptr, res, old); else bcopy(ptr, res, size); - free(ptr); + Free(ptr, file, line); } else { #ifdef DMALLOCDEBUG if (++MallocCount > MallocMax) @@ -174,12 +184,12 @@ } void * -reallocf(void *ptr, size_t size) +Reallocf(void *ptr, size_t size, char *file, int line) { void *res; - if ((res = realloc(ptr, size)) == NULL) - free(ptr); + if ((res = Realloc(ptr, size, file, line)) == NULL) + Free(ptr, file, line); return(res); } To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe p4-projects" in the body of the message