diff options
Diffstat (limited to 'lib/malloc')
-rw-r--r-- | lib/malloc/Makefile.in | 7 | ||||
-rw-r--r-- | lib/malloc/imalloc.h | 97 | ||||
-rw-r--r-- | lib/malloc/malloc.c | 451 | ||||
-rw-r--r-- | lib/malloc/mstats.h | 56 | ||||
-rw-r--r-- | lib/malloc/shmalloc.h | 2 | ||||
-rw-r--r-- | lib/malloc/stats.c | 67 | ||||
-rw-r--r-- | lib/malloc/stub.c | 18 | ||||
-rw-r--r-- | lib/malloc/table.c | 35 | ||||
-rw-r--r-- | lib/malloc/table.h | 10 | ||||
-rw-r--r-- | lib/malloc/trace.c | 14 | ||||
-rw-r--r-- | lib/malloc/watch.c | 150 | ||||
-rw-r--r-- | lib/malloc/watch.h | 39 |
12 files changed, 736 insertions, 210 deletions
diff --git a/lib/malloc/Makefile.in b/lib/malloc/Makefile.in index cbc6dbf..91549c9 100644 --- a/lib/malloc/Makefile.in +++ b/lib/malloc/Makefile.in @@ -36,7 +36,7 @@ MV = mv SHELL = @MAKE_SHELL@ -PROFILE_FLAGS = +PROFILE_FLAGS = @PROFILE_FLAGS@ CFLAGS = @CFLAGS@ LOCAL_CFLAGS = @LOCAL_CFLAGS@ @@ -69,7 +69,7 @@ MALLOC_SRC = @MALLOC_SRC@ MALLOC = @MALLOC@ ALLOCA = @ALLOCA@ -MALLOC_OBJS = malloc.o $(ALLOCA) trace.o stats.o table.o +MALLOC_OBJS = malloc.o $(ALLOCA) trace.o stats.o table.o watch.o STUB_OBJS = $(ALLOCA) stub.o .PHONY: malloc stubmalloc @@ -110,9 +110,11 @@ trace.o: ${BUILD_DIR}/config.h table.o: ${BUILD_DIR}/config.h malloc.o: ${srcdir}/imalloc.h ${srcdir}/mstats.h +malloc.o: ${srcdir}/table.h ${srcdir}/watch.h stats.o: ${srcdir}/imalloc.h ${srcdir}/mstats.h trace.o: ${srcdir}/imalloc.h table.o: ${srcdir}/imalloc.h ${srcdir}/table.h +watch.o: ${srcdir}/imalloc.h ${srcdir}/watch.h # Rules for deficient makes, like SunOS and Solaris stub.o: stub.c @@ -120,3 +122,4 @@ malloc.o: malloc.c table.o: table.c trace.o: trace.c stats.o: stats.c +watch.o: watch.c diff --git a/lib/malloc/imalloc.h b/lib/malloc/imalloc.h index ef08868..3920ce9 100644 --- a/lib/malloc/imalloc.h +++ b/lib/malloc/imalloc.h @@ -20,15 +20,18 @@ /* Must be included *after* config.h */ -#ifndef _IMALLOC_H_ +#ifndef _IMALLOC_H #define _IMALLOC_H #ifdef MALLOC_DEBUG #define MALLOC_STATS #define MALLOC_TRACE #define MALLOC_REGISTER +#define MALLOC_WATCH #endif +#define MALLOC_WRAPFUNCS + /* Generic pointer type. */ #ifndef PTR_T # if defined (__STDC__) @@ -64,4 +67,96 @@ # endif /* HAVE_BCOPY */ #endif /* !__GNUC__ */ +#if !defined (__P) +# if defined (__STDC__) || defined (__GNUC__) || defined (__cplusplus) || defined (PROTOTYPES) +# define __P(protos) protos +# else +# define __P(protos) () +# endif #endif + +/* Use Duff's device for good zeroing/copying performance. DO NOT call the + Duff's device macros with NBYTES == 0. */ + +#define MALLOC_BZERO(charp, nbytes) \ +do { \ + if ((nbytes) <= 32) { \ + size_t * mzp = (size_t *)(charp); \ + unsigned long mctmp = (nbytes)/sizeof(size_t); \ + long mcn; \ + if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp &= 7; } \ + switch (mctmp) { \ + case 0: for(;;) { *mzp++ = 0; \ + case 7: *mzp++ = 0; \ + case 6: *mzp++ = 0; \ + case 5: *mzp++ = 0; \ + case 4: *mzp++ = 0; \ + case 3: *mzp++ = 0; \ + case 2: *mzp++ = 0; \ + case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \ + } \ + else \ + memset ((charp), 0, (nbytes)); \ +} while(0) + +#define MALLOC_ZERO(charp, nbytes) \ +do { \ + size_t mzsz = (nbytes); \ + if (mzsz <= 9 * sizeof(mzsz) { \ + size_t *mz = (size_t *)(charp); \ + if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \ + *mz++ = 0; \ + if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \ + *mz++ = 0; \ + if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \ + *mz++ = 0; }}} \ + *mz++ = 0; \ + *mz++ = 0; \ + *mz = 0; \ + } else \ + memset ((charp), 0, mzsz); \ +} while (0) + +#define MALLOC_MEMSET(charp, xch, nbytes) \ +do { \ + if ((nbytes) <= 32) { \ + register char * mzp = (charp); \ + unsigned long mctmp = (nbytes); \ + register long mcn; \ + if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp &= 7; } \ + switch (mctmp) { \ + case 0: for(;;) { *mzp++ = xch; \ + case 7: *mzp++ = xch; \ + case 6: *mzp++ = xch; \ + case 5: *mzp++ = xch; \ + case 4: *mzp++ = xch; \ + case 3: *mzp++ = xch; \ + case 2: *mzp++ = xch; \ + case 1: *mzp++ = xch; if(mcn <= 0) break; mcn--; } \ + } \ + } else \ + memset ((charp), (xch), (nbytes)); \ +} while(0) + +#define MALLOC_MEMCPY(dest,src,nbytes) \ +do { \ + if ((nbytes) <= 32) { \ + size_t* mcsrc = (size_t*) src; \ + size_t* mcdst = (size_t*) dest; \ + unsigned long mctmp = (nbytes)/sizeof(size_t); \ + long mcn; \ + if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp &= 7; } \ + switch (mctmp) { \ + case 0: for(;;) { *mcdst++ = *mcsrc++; \ + case 7: *mcdst++ = *mcsrc++; \ + case 6: *mcdst++ = *mcsrc++; \ + case 5: *mcdst++ = *mcsrc++; \ + case 4: *mcdst++ = *mcsrc++; \ + case 3: *mcdst++ = *mcsrc++; \ + case 2: *mcdst++ = *mcsrc++; \ + case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \ + } else \ + memcpy ((dest), (src), (nbytes)) \ +} while(0) + +#endif /* _IMALLOC_H */ diff --git a/lib/malloc/malloc.c b/lib/malloc/malloc.c index 03b8f08..0b4c847 100644 --- a/lib/malloc/malloc.c +++ b/lib/malloc/malloc.c @@ -53,12 +53,12 @@ what you give them. Help stamp out software-hoarding! */ * to the second. */ -/* Define this to have free() write 0xcf into memory as it's freed, to - uncover callers that refer to freed memory. */ -/* SCO 3.2v4 getcwd and possibly other libc routines fail with MEMSCRAMBLE */ -#if !defined (NO_MEMSCRAMBLE) -# define MEMSCRAMBLE -#endif +/* Define MEMSCRAMBLE to have free() write 0xcf into memory as it's freed, to + uncover callers that refer to freed memory, and to have malloc() write 0xdf + into memory as it's allocated to avoid referring to previous contents. */ + +/* SCO 3.2v4 getcwd and possibly other libc routines fail with MEMSCRAMBLE; + handled by configure. */ #if defined (HAVE_CONFIG_H) # include <config.h> @@ -69,15 +69,6 @@ what you give them. Help stamp out software-hoarding! */ # include "stdc.h" #else # include <sys/types.h> - -# ifndef __P -# if defined (__STDC__) || defined (__GNUC__) || defined (__cplusplus) -# define __P(protos) protos -# else -# define __P(protos) () -# endif -# endif - #endif #if defined (HAVE_UNISTD_H) @@ -107,6 +98,9 @@ what you give them. Help stamp out software-hoarding! */ #ifdef MALLOC_REGISTER # include "table.h" #endif +#ifdef MALLOC_WATCH +# include "watch.h" +#endif /* System-specific omissions. */ #ifdef HPUX @@ -145,6 +139,14 @@ union mhead { #define mh_nbytes minfo.mi_nbytes #define mh_magic2 minfo.mi_magic2 +#define MOVERHEAD sizeof(union mhead) +#define MALIGN_MASK 7 /* one less than desired alignment */ + +typedef union _malloc_guard { + char s[4]; + u_bits32_t i; +} mguard_t; + /* Access free-list pointer of a block. It is stored at block + sizeof (char *). This is not a field in the minfo structure member of union mhead @@ -159,16 +161,15 @@ union mhead { and end of each allocated block, and make sure they are undisturbed whenever a free or a realloc occurs. */ -/* Written in each of the 4 bytes following the block's real space */ -#define MAGIC1 0x55 /* Written in the 2 bytes before the block's real space (-4 bytes) */ #define MAGIC2 0x5555 -#define MSLOP 4 /* 4 bytes extra for MAGIC1s */ +#define MSLOP 4 /* 4 bytes extra for u_bits32_t size */ /* How many bytes are actually allocated for a request of size N -- rounded up to nearest multiple of 8 after accounting for malloc overhead. */ -#define ALLOCATED_BYTES(n) (((n) + sizeof (union mhead) + MSLOP + 7) & ~7) +#define ALLOCATED_BYTES(n) \ + (((n) + MOVERHEAD + MSLOP + MALIGN_MASK) & ~MALIGN_MASK) #define ASSERT(p) \ do \ @@ -179,15 +180,18 @@ union mhead { /* Minimum and maximum bucket indices for block splitting (and to bound the search for a block to split). */ -#define SPLIT_MIN 3 -#define SPLIT_MID 11 /* XXX - was 9 */ -#define SPLIT_MAX 14 /* XXX - was 12 */ +#define SPLIT_MIN 2 /* XXX - was 3 */ +#define SPLIT_MID 11 +#define SPLIT_MAX 14 /* Minimum and maximum bucket indices for block coalescing. */ -#define COMBINE_MIN 6 -#define COMBINE_MAX (pagebucket - 1) +#define COMBINE_MIN 2 +#define COMBINE_MAX (pagebucket - 1) /* XXX */ -#define MIN_COMBINE_FREE 4 +#define LESSCORE_MIN 10 +#define LESSCORE_FRC 13 + +#define STARTBUCK 1 /* Flags for the internal functions. */ #define MALLOC_WRAPPER 0x01 /* wrapper function */ @@ -202,9 +206,16 @@ union mhead { #define ERR_ASSERT_FAILED 0x08 /* Evaluates to true if NB is appropriate for bucket NU. NB is adjusted - appropriately by the caller to account for malloc overhead. */ -#define IN_BUCKET(nb, nu) \ - ((nb) > (4 << (nu)) && ((nb) <= (8 << (nu)))) + appropriately by the caller to account for malloc overhead. This only + checks that the recorded size is not too big for the bucket. We + can't check whether or not it's in between NU and NU-1 because we + might have encountered a busy bucket when allocating and moved up to + the next size. */ +#define IN_BUCKET(nb, nu) ((nb) <= binsizes[(nu)]) + +/* Use this when we want to be sure that NB is in bucket NU. */ +#define RIGHT_BUCKET(nb, nu) \ + (((nb) > binsizes[(nu)-1]) && ((nb) <= binsizes[(nu)])) /* nextf[i] is free list of blocks of size 2**(i + 3) */ @@ -218,6 +229,19 @@ static int pagesz; /* system page size. */ static int pagebucket; /* bucket for requests a page in size */ static int maxbuck; /* highest bucket receiving allocation request. */ +static char *memtop; /* top of heap */ + +static unsigned long binsizes[NBUCKETS] = { + 8UL, 16UL, 32UL, 64UL, 128UL, 256UL, 512UL, 1024UL, 2048UL, 4096UL, + 8192UL, 16384UL, 32768UL, 65536UL, 131072UL, 262144UL, 524288UL, + 1048576UL, 2097152UL, 4194304UL, 8388608UL, 16777216UL, 33554432UL, + 67108864UL, 134217728UL, 268435456UL, 536870912UL, 1073741824UL, + 2147483648UL, 4294967296UL-1 +}; + +/* binsizes[x] == (1 << ((x) + 3)) */ +#define binsize(x) binsizes[(x)] + /* Declarations for internal functions */ static PTR_T internal_malloc __P((size_t, const char *, int, int)); static PTR_T internal_realloc __P((PTR_T, size_t, const char *, int, int)); @@ -238,10 +262,6 @@ static void botch __P((const char *, const char *, int)); #endif static void xbotch __P((PTR_T, int, const char *, const char *, int)); -#ifdef MALLOC_STATS -extern struct _malstats _mstats; -#endif /* MALLOC_STATS */ - #if !HAVE_DECL_SBRK extern char *sbrk (); #endif /* !HAVE_DECL_SBRK */ @@ -251,11 +271,23 @@ extern int interrupt_immediately; extern int signal_is_trapped __P((int)); #endif +#ifdef MALLOC_STATS +struct _malstats _mstats; +#endif /* MALLOC_STATS */ + /* Debugging variables available to applications. */ int malloc_flags = 0; /* future use */ int malloc_trace = 0; /* trace allocations and frees to stderr */ int malloc_register = 0; /* future use */ +#ifdef MALLOC_TRACE +char _malloc_trace_buckets[NBUCKETS]; + +/* These should really go into a header file. */ +extern void mtrace_alloc __P((const char *, PTR_T, size_t, const char *, int)); +extern void mtrace_free __P((PTR_T, int, const char *, int)); +#endif + #if !defined (botch) static void botch (s, file, line) @@ -286,53 +318,54 @@ xbotch (mem, e, s, file, line) botch(s, file, line); } -#if 0 /* Coalesce two adjacent free blocks off the free list for size NU - 1, - as long as there are at least MIN_COMBINE_FREE free blocks and we - can find two adjacent free blocks. nextf[NU -1] is assumed to not - be busy; the caller (morecore()) checks for this. */ + as long as we can find two adjacent free blocks. nextf[NU -1] is + assumed to not be busy; the caller (morecore()) checks for this. */ static void bcoalesce (nu) register int nu; { register union mhead *mp, *mp1, *mp2; - register int nfree, nbuck; + register int nbuck; unsigned long siz; nbuck = nu - 1; if (nextf[nbuck] == 0) return; - nfree = 1; - mp1 = nextf[nbuck]; + siz = binsize (nbuck); + + mp2 = mp1 = nextf[nbuck]; mp = CHAIN (mp1); - mp2 = (union mhead *)0; - while (CHAIN (mp)) + while (mp && mp != (union mhead *)((char *)mp1 + siz)) { mp2 = mp1; mp1 = mp; mp = CHAIN (mp); - nfree++; - /* We may not want to run all the way through the free list here; - if we do not, we need to check a threshold value here and break - if nfree exceeds it. */ } - if (nfree < MIN_COMBINE_FREE) + if (mp == 0) return; + /* OK, now we have mp1 pointing to the block we want to add to nextf[NU]. CHAIN(mp2) must equal mp1. Check that mp1 and mp are adjacent. */ - if (CHAIN(mp2) != mp1) + if (mp2 != mp1 && CHAIN(mp2) != mp1) xbotch ((PTR_T)0, 0, "bcoalesce: CHAIN(mp2) != mp1", (char *)NULL, 0); - siz = 1 << (nbuck + 3); + +#ifdef MALLOC_DEBUG if (CHAIN (mp1) != (union mhead *)((char *)mp1 + siz)) return; /* not adjacent */ +#endif #ifdef MALLOC_STATS _mstats.tbcoalesce++; + _mstats.ncoalesce[nbuck]++; #endif /* Since they are adjacent, remove them from the free list */ - CHAIN (mp2) = CHAIN (mp); + if (mp1 == nextf[nbuck]) + nextf[nbuck] = CHAIN (mp); + else + CHAIN (mp2) = CHAIN (mp); /* And add the combined two blocks to nextf[NU]. */ mp1->mh_alloc = ISFREE; @@ -340,7 +373,6 @@ bcoalesce (nu) CHAIN (mp1) = nextf[nu]; nextf[nu] = mp1; } -#endif /* Split a block at index > NU (but less than SPLIT_MAX) into a set of blocks of the correct size, and attach them to nextf[NU]. nextf[NU] @@ -387,8 +419,8 @@ bsplit (nu) #endif /* Figure out how many blocks we'll get. */ - siz = (1 << (nu + 3)); - nblks = (1 << (nbuck + 3)) / siz; + siz = binsize (nu); + nblks = binsize (nbuck) / siz; /* Remove the block from the chain of larger blocks. */ mp = nextf[nbuck]; @@ -434,6 +466,27 @@ unblock_signals (setp, osetp) # endif #endif } + +/* Return some memory to the system by reducing the break. This is only + called with NU > pagebucket, so we're always assured of giving back + more than one page of memory. */ +static void +lesscore (nu) /* give system back some memory */ + register int nu; /* size index we're discarding */ +{ + long siz; + + siz = binsize (nu); + /* Should check for errors here, I guess. */ + sbrk (-siz); + memtop -= siz; + +#ifdef MALLOC_STATS + _mstats.nsbrk++; + _mstats.tsbrk -= siz; + _mstats.nlesscore[nu]++; +#endif +} static void morecore (nu) /* ask system for more memory */ @@ -456,7 +509,7 @@ morecore (nu) /* ask system for more memory */ blocked_sigs = 1; } - siz = 1 << (nu + 3); /* size of desired block for nextf[nu] */ + siz = binsize (nu); /* size of desired block for nextf[nu] */ if (siz < 0) goto morecore_done; /* oops */ @@ -474,7 +527,6 @@ morecore (nu) /* ask system for more memory */ goto morecore_done; } -#if 0 /* Try to coalesce two adjacent blocks from the free list on nextf[nu - 1], if we can, and we're withing the range of the block coalescing limits. */ if (nu >= COMBINE_MIN && nu < COMBINE_MAX && busy[nu - 1] == 0 && nextf[nu - 1]) @@ -483,7 +535,6 @@ morecore (nu) /* ask system for more memory */ if (nextf[nu] != 0) goto morecore_done; } -#endif /* Take at least a page, and figure out how many blocks of the requested size we're getting. */ @@ -499,7 +550,7 @@ morecore (nu) /* ask system for more memory */ an amount. If it is, we can just request it. If not, we want the smallest integral multiple of pagesize that is larger than `siz' and will satisfy the request. */ - sbrk_amt = siz % pagesz; + sbrk_amt = siz & (pagesz - 1); if (sbrk_amt == 0) sbrk_amt = siz; else @@ -518,10 +569,12 @@ morecore (nu) /* ask system for more memory */ if ((long)mp == -1) goto morecore_done; + memtop += sbrk_amt; + /* shouldn't happen, but just in case -- require 8-byte alignment */ - if ((long)mp & 7) + if ((long)mp & MALIGN_MASK) { - mp = (union mhead *) (((long)mp + 7) & ~7); + mp = (union mhead *) (((long)mp + MALIGN_MASK) & ~MALIGN_MASK); nblks--; } @@ -542,28 +595,82 @@ morecore_done: unblock_signals (&set, &oset); } -#if defined (MEMSCRAMBLE) || !defined (NO_CALLOC) -static char * -zmemset (s, c, n) - char *s; - int c; - register int n; -{ - register char *sp; - - sp = s; - while (--n >= 0) - *sp++ = c; - return (s); -} -#endif /* MEMSCRAMBLE || !NO_CALLOC */ - static void malloc_debug_dummy () { write (1, "malloc_debug_dummy\n", 19); } +#define PREPOP_BIN 2 +#define PREPOP_SIZE 32 + +static int +pagealign () +{ + register int nunits; + register union mhead *mp; + long sbrk_needed; + char *curbrk; + + pagesz = getpagesize (); + if (pagesz < 1024) + pagesz = 1024; + + /* OK, how much do we need to allocate to make things page-aligned? + Some of this partial page will be wasted space, but we'll use as + much as we can. Once we figure out how much to advance the break + pointer, go ahead and do it. */ + memtop = curbrk = sbrk (0); + sbrk_needed = pagesz - ((long)curbrk & (pagesz - 1)); /* sbrk(0) % pagesz */ + if (sbrk_needed < 0) + sbrk_needed += pagesz; + + /* Now allocate the wasted space. */ + if (sbrk_needed) + { +#ifdef MALLOC_STATS + _mstats.nsbrk++; + _mstats.tsbrk += sbrk_needed; +#endif + curbrk = sbrk (sbrk_needed); + if ((long)curbrk == -1) + return -1; + memtop += sbrk_needed; + + /* Take the memory which would otherwise be wasted and populate the most + popular bin (2 == 32 bytes) with it. Add whatever we need to curbrk + to make things 32-byte aligned, compute how many 32-byte chunks we're + going to get, and set up the bin. */ + curbrk += sbrk_needed & (PREPOP_SIZE - 1); + sbrk_needed -= sbrk_needed & (PREPOP_SIZE - 1); + nunits = sbrk_needed / PREPOP_SIZE; + + if (nunits > 0) + { + mp = (union mhead *)curbrk; + + nextf[PREPOP_BIN] = mp; + while (1) + { + mp->mh_alloc = ISFREE; + mp->mh_index = PREPOP_BIN; + if (--nunits <= 0) break; + CHAIN(mp) = (union mhead *)((char *)mp + PREPOP_SIZE); + mp = (union mhead *)((char *)mp + PREPOP_SIZE); + } + CHAIN(mp) = 0; + } + } + + /* compute which bin corresponds to the page size. */ + for (nunits = 7; nunits < NBUCKETS; nunits++) + if (pagesz <= binsize(nunits)) + break; + pagebucket = nunits; + + return 0; +} + static PTR_T internal_malloc (n, file, line, flags) /* get a block */ size_t n; @@ -571,71 +678,27 @@ internal_malloc (n, file, line, flags) /* get a block */ int line, flags; { register union mhead *p; - register long nbytes; register int nunits; + register char *m, *z; + long nbytes; + mguard_t mg; - /* Get the system page size and align break pointer so everything will + /* Get the system page size and align break pointer so future sbrks will be page-aligned. The page size must be at least 1K -- anything smaller is increased. */ if (pagesz == 0) - { - register long sbrk_needed; - - pagesz = getpagesize (); - if (pagesz < 1024) - pagesz = 1024; - /* OK, how much do we need to allocate to make things page-aligned? - This partial page is wasted space. Once we figure out how much - to advance the break pointer, go ahead and do it. */ - sbrk_needed = pagesz - ((long)sbrk (0) & (pagesz - 1)); /* sbrk(0) % pagesz */ - if (sbrk_needed < 0) - sbrk_needed += pagesz; - /* Now allocate the wasted space. */ - if (sbrk_needed) - { -#ifdef MALLOC_STATS - _mstats.nsbrk++; - _mstats.tsbrk += sbrk_needed; -#endif - if ((long)sbrk (sbrk_needed) == -1) - return (NULL); - } - nunits = 0; - nbytes = 8; - while (pagesz > nbytes) - { - nbytes <<= 1; - nunits++; - } - pagebucket = nunits; - } + if (pagealign () < 0) + return ((PTR_T)NULL); /* Figure out how many bytes are required, rounding up to the nearest multiple of 8, then figure out which nextf[] area to use. Try to be smart about where to start searching -- if the number of bytes needed is greater than the page size, we can start at pagebucket. */ nbytes = ALLOCATED_BYTES(n); - nunits = 0; - if (nbytes <= (pagesz >> 1)) - { - register unsigned int shiftr; - - shiftr = (nbytes - 1) >> 2; /* == (nbytes - 1) / 4 */ - while (shiftr >>= 1) /* == (nbytes - 1) / {8,16,32,...} */ - nunits++; - } - else - { - register u_bits32_t amt; - - nunits = pagebucket; - amt = pagesz; - while (nbytes > amt) - { - amt <<= 1; - nunits++; - } - } + nunits = (nbytes <= (pagesz >> 1)) ? STARTBUCK : pagebucket; + for ( ; nunits < NBUCKETS; nunits++) + if (nbytes <= binsize(nunits)) + break; /* Silently reject too-large requests. */ if (nunits >= NBUCKETS) @@ -671,30 +734,35 @@ internal_malloc (n, file, line, flags) /* get a block */ /* If not for this check, we would gobble a clobbered free chain ptr and bomb out on the NEXT allocate of this size block */ if (p->mh_alloc != ISFREE || p->mh_index != nunits) - xbotch ((PTR_T)0, 0, "malloc: block on free list clobbered", file, line); + xbotch ((PTR_T)(p+1), 0, "malloc: block on free list clobbered", file, line); /* Fill in the info, and set up the magic numbers for range checking. */ p->mh_alloc = ISALLOC; p->mh_magic2 = MAGIC2; p->mh_nbytes = n; - { - register char *m = (char *) (p + 1) + n; - *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1; - } + /* End guard */ + mg.i = n; + z = mg.s; + m = (char *) (p + 1) + n; + *m++ = *z++, *m++ = *z++, *m++ = *z++, *m++ = *z++; #ifdef MEMSCRAMBLE - zmemset ((char *)(p + 1), 0xdf, n); /* scramble previous contents */ + if (n) + MALLOC_MEMSET ((char *)(p + 1), 0xdf, n); /* scramble previous contents */ #endif #ifdef MALLOC_STATS _mstats.nmalloc[nunits]++; _mstats.tmalloc[nunits]++; _mstats.nmal++; + _mstats.bytesreq += n; #endif /* MALLOC_STATS */ #ifdef MALLOC_TRACE if (malloc_trace && (flags & MALLOC_NOTRACE) == 0) mtrace_alloc ("malloc", p + 1, n, file, line); + else if (_malloc_trace_buckets[nunits]) + mtrace_alloc ("malloc", p + 1, n, file, line); #endif #ifdef MALLOC_REGISTER @@ -702,7 +770,12 @@ internal_malloc (n, file, line, flags) /* get a block */ mregister_alloc ("malloc", p + 1, n, file, line); #endif - return (char *) (p + 1); /* XXX - should be cast to PTR_T? */ +#ifdef MALLOC_WATCH + if (_malloc_nwatch > 0) + _malloc_ckwatch (p + 1, file, line, W_ALLOC, n); +#endif + + return (PTR_T) (p + 1); } static void @@ -712,10 +785,11 @@ internal_free (mem, file, line, flags) int line, flags; { register union mhead *p; - register char *ap; + register char *ap, *z; register int nunits; register unsigned int nbytes; int ubytes; /* caller-requested size */ + mguard_t mg; if ((ap = (char *)mem) == 0) return; @@ -753,18 +827,42 @@ internal_free (mem, file, line, flags) We sanity-check the value of mh_nbytes against the size of the blocks in the appropriate bucket before we use it. This can still cause problems and obscure errors if mh_nbytes is wrong but still within range; the - checks against MAGIC1 will probably fail then. Using MALLOC_REGISTER - will help here, since it saves the original number of bytes requested. */ + checks against the size recorded at the end of the chunk will probably + fail then. Using MALLOC_REGISTER will help here, since it saves the + original number of bytes requested. */ + if (IN_BUCKET(nbytes, nunits) == 0) xbotch (mem, ERR_UNDERFLOW, "free: underflow detected; mh_nbytes out of range", file, line); ap += p->mh_nbytes; - ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1); - ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1); + z = mg.s; + *z++ = *ap++, *z++ = *ap++, *z++ = *ap++, *z++ = *ap++; + if (mg.i != p->mh_nbytes) + xbotch (mem, ERR_ASSERT_FAILED, "free: start and end chunk sizes differ", file, line); + +#if 1 + if (nunits >= LESSCORE_MIN && ((char *)p + binsize(nunits) == memtop)) +#else + if (((char *)p + binsize(nunits) == memtop) && nunits >= LESSCORE_MIN) +#endif + { + /* If above LESSCORE_FRC, give back unconditionally. This should be set + high enough to be infrequently encountered. If between LESSCORE_MIN + and LESSCORE_FRC, call lesscore if the bucket is marked as busy (in + which case we would punt below and leak memory) or if there's already + a block on the free list. */ + if ((nunits >= LESSCORE_FRC) || busy[nunits] || nextf[nunits] != 0) + { + lesscore (nunits); + /* keeps the tracing and registering code in one place */ + goto free_return; + } + } #ifdef MEMSCRAMBLE - zmemset (mem, 0xcf, p->mh_nbytes); + if (p->mh_nbytes) + MALLOC_MEMSET (mem, 0xcf, p->mh_nbytes); #endif ASSERT (nunits < NBUCKETS); @@ -780,6 +878,8 @@ internal_free (mem, file, line, flags) nextf[nunits] = p; busy[nunits] = 0; +free_return: + #ifdef MALLOC_STATS _mstats.nmalloc[nunits]--; _mstats.nfre++; @@ -788,12 +888,19 @@ internal_free (mem, file, line, flags) #ifdef MALLOC_TRACE if (malloc_trace && (flags & MALLOC_NOTRACE) == 0) mtrace_free (mem, ubytes, file, line); + else if (_malloc_trace_buckets[nunits]) + mtrace_free (mem, ubytes, file, line); #endif #ifdef MALLOC_REGISTER if (malloc_register && (flags & MALLOC_NOREG) == 0) mregister_free (mem, ubytes, file, line); #endif + +#ifdef MALLOC_WATCH + if (_malloc_nwatch > 0) + _malloc_ckwatch (mem, file, line, W_FREE, ubytes); +#endif } static PTR_T @@ -807,7 +914,8 @@ internal_realloc (mem, n, file, line, flags) register u_bits32_t tocopy; register unsigned int nbytes; register int nunits; - register char *m; + register char *m, *z; + mguard_t mg; #ifdef MALLOC_STATS _mstats.nrealloc++; @@ -837,37 +945,56 @@ internal_realloc (mem, n, file, line, flags) We sanity-check the value of mh_nbytes against the size of the blocks in the appropriate bucket before we use it. This can still cause problems and obscure errors if mh_nbytes is wrong but still within range; the - checks against MAGIC1 will probably fail then. Using MALLOC_REGISTER - will help here, since it saves the original number of bytes requested. */ + checks against the size recorded at the end of the chunk will probably + fail then. Using MALLOC_REGISTER will help here, since it saves the + original number of bytes requested. */ if (IN_BUCKET(nbytes, nunits) == 0) xbotch (mem, ERR_UNDERFLOW, "realloc: underflow detected; mh_nbytes out of range", file, line); m = (char *)mem + (tocopy = p->mh_nbytes); - ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1); - ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1); + z = mg.s; + *z++ = *m++, *z++ = *m++, *z++ = *m++, *z++ = *m++; + if (mg.i != p->mh_nbytes) + xbotch (mem, ERR_ASSERT_FAILED, "realloc: start and end chunk sizes differ", file, line); + +#ifdef MALLOC_WATCH + if (_malloc_nwatch > 0) + _malloc_ckwatch (p + 1, file, line, W_REALLOC, n); +#endif +#ifdef MALLOC_STATS + _mstats.bytesreq += (n < tocopy) ? 0 : n - tocopy; +#endif /* See if desired size rounds to same power of 2 as actual size. */ nbytes = ALLOCATED_BYTES(n); /* If ok, use the same block, just marking its size as changed. */ - if (IN_BUCKET(nbytes, nunits)) + if (RIGHT_BUCKET(nbytes, nunits)) { - m = (char *)mem + tocopy; +#if 0 + m = (char *)mem + p->mh_nbytes; +#else + /* Compensate for increment above. */ + m -= 4; +#endif *m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0; - p->mh_nbytes = n; - m = (char *)mem + n; - *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; + m = (char *)mem + (p->mh_nbytes = n); + + mg.i = n; + z = mg.s; + *m++ = *z++, *m++ = *z++, *m++ = *z++, *m++ = *z++; + return mem; } + if (n < tocopy) + tocopy = n; + #ifdef MALLOC_STATS _mstats.nrcopy++; #endif - if (n < tocopy) - tocopy = n; - if ((m = internal_malloc (n, file, line, MALLOC_INTERNAL|MALLOC_NOTRACE|MALLOC_NOREG)) == 0) return 0; FASTCOPY (mem, m, tocopy); @@ -876,6 +1003,8 @@ internal_realloc (mem, n, file, line, flags) #ifdef MALLOC_TRACE if (malloc_trace && (flags & MALLOC_NOTRACE) == 0) mtrace_alloc ("realloc", m, n, file, line); + else if (_malloc_trace_buckets[nunits]) + mtrace_alloc ("realloc", m, n, file, line); #endif #ifdef MALLOC_REGISTER @@ -883,6 +1012,11 @@ internal_realloc (mem, n, file, line, flags) mregister_alloc ("realloc", m, n, file, line); #endif +#ifdef MALLOC_WATCH + if (_malloc_nwatch > 0) + _malloc_ckwatch (m, file, line, W_RESIZED, n); +#endif + return m; } @@ -946,7 +1080,7 @@ internal_calloc (n, s, file, line, flags) total = n * s; result = internal_malloc (total, file, line, flags|MALLOC_INTERNAL); if (result) - zmemset (result, 0, total); + memset (result, 0, total); return result; } @@ -961,7 +1095,6 @@ internal_cfree (p, file, line, flags) #endif /* !NO_CALLOC */ #ifdef MALLOC_STATS - int malloc_free_blocks (size) int size; @@ -977,7 +1110,7 @@ malloc_free_blocks (size) } #endif -#if defined (SHELL) +#if defined (MALLOC_WRAPFUNCS) PTR_T sh_malloc (bytes, file, line) size_t bytes; @@ -1045,9 +1178,9 @@ sh_valloc (size, file, line) { return internal_valloc (size, file, line, MALLOC_WRAPPER); } -#endif +#endif /* !NO_VALLOC */ -#endif +#endif /* MALLOC_WRAPFUNCS */ /* Externally-available functions that call their internal counterparts. */ diff --git a/lib/malloc/mstats.h b/lib/malloc/mstats.h index df51485..d237635 100644 --- a/lib/malloc/mstats.h +++ b/lib/malloc/mstats.h @@ -31,18 +31,28 @@ * NMALLOC[i] is the difference between the number of mallocs and frees * for a given block size. TMALLOC[i] is the total number of mallocs for * a given block size. NMORECORE[i] is the total number of calls to - * morecore(i). NMAL and NFRE are counts of the number of calls to malloc() - * and free(), respectively. NREALLOC is the total number of calls to - * realloc(); NRCOPY is the number of times realloc() had to allocate new - * memory and copy to it. NRECURSE is a count of the number of recursive - * calls to malloc() for the same bucket size, which can be caused by calls - * to malloc() from a signal handler. NSBRK is the number of calls to sbrk() - * (whether by morecore() or for alignment); TSBRK is the total number of - * bytes requested from the kernel with sbrk(). BYTESUSED is the total - * number of bytes consumed by blocks currently in use; BYTESFREE is the - * total number of bytes currently on all of the free lists. TBSPLIT is - * the number of times a larger block was split to satisfy a smaller request. - * NSPLIT[i] is the number of times a block of size I was split. + * morecore(i). NLESSCORE[i] is the total number of calls to lesscore(i). + * + * NMAL and NFRE are counts of the number of calls to malloc() and free(), + * respectively. NREALLOC is the total number of calls to realloc(); + * NRCOPY is the number of times realloc() had to allocate new memory and + * copy to it. NRECURSE is a count of the number of recursive calls to + * malloc() for the same bucket size, which can be caused by calls to + * malloc() from a signal handler. + * + * NSBRK is the number of calls to sbrk() (whether by morecore() or for + * alignment); TSBRK is the total number of bytes requested from the kernel + * with sbrk(). + * + * BYTESUSED is the total number of bytes consumed by blocks currently in + * use; BYTESFREE is the total number of bytes currently on all of the free + * lists. BYTESREQ is the total number of bytes requested by the caller + * via calls to malloc() and realloc(). + * + * TBSPLIT is the number of times a larger block was split to satisfy a + * smaller request. NSPLIT[i] is the number of times a block of size I was + * split. + * * TBCOALESCE is the number of times two adjacent smaller blocks off the free * list were combined to satisfy a larger request. */ @@ -50,6 +60,7 @@ struct _malstats { int nmalloc[NBUCKETS]; int tmalloc[NBUCKETS]; int nmorecore[NBUCKETS]; + int nlesscore[NBUCKETS]; int nmal; int nfre; int nrealloc; @@ -59,31 +70,38 @@ struct _malstats { bits32_t tsbrk; bits32_t bytesused; bits32_t bytesfree; + u_bits32_t bytesreq; int tbsplit; int nsplit[NBUCKETS]; int tbcoalesce; + int ncoalesce[NBUCKETS]; }; /* Return statistics describing allocation of blocks of size BLOCKSIZE. NFREE is the number of free blocks for this allocation size. NUSED is the number of blocks in use. NMAL is the number of requests for blocks of size BLOCKSIZE. NMORECORE is the number of times we had - to call MORECORE to repopulate the free list for this bucket. NSPLIT - is the number of times a block of this size was split to satisfy a - smaller request. */ + to call MORECORE to repopulate the free list for this bucket. + NLESSCORE is the number of times we gave memory back to the system + from this bucket. NSPLIT is the number of times a block of this size + was split to satisfy a smaller request. NCOALESCE is the number of + times two blocks of this size were combined to satisfy a larger + request. */ struct bucket_stats { u_bits32_t blocksize; int nfree; int nused; int nmal; int nmorecore; + int nlesscore; int nsplit; + int ncoalesce; }; -extern struct bucket_stats malloc_bucket_stats (); -extern struct _malstats malloc_stats (); -extern void print_malloc_stats (); -extern void trace_malloc_stats (); +extern struct bucket_stats malloc_bucket_stats __P((int)); +extern struct _malstats malloc_stats __P((void)); +extern void print_malloc_stats __P((char *)); +extern void trace_malloc_stats __P((char *, char *)); #endif /* MALLOC_STATS */ diff --git a/lib/malloc/shmalloc.h b/lib/malloc/shmalloc.h index 7143803..85cb5ce 100644 --- a/lib/malloc/shmalloc.h +++ b/lib/malloc/shmalloc.h @@ -62,6 +62,6 @@ extern int malloc_set_register __P((int)); /* stats.c */ extern void print_malloc_stats __P((char *)); extern void fprint_malloc_stats (); /* full prototype requires stdio.h */ -extern void trace_malloc_stats __P((char *)); +extern void trace_malloc_stats __P((char *, char *)); #endif diff --git a/lib/malloc/stats.c b/lib/malloc/stats.c index 48fba1b..52b23ae 100644 --- a/lib/malloc/stats.c +++ b/lib/malloc/stats.c @@ -25,9 +25,15 @@ #ifdef MALLOC_STATS #include <stdio.h> +#ifdef HAVE_UNISTD_H +# include <unistd.h> +#endif + #include "mstats.h" -struct _malstats _mstats; +extern int malloc_free_blocks __P((int)); + +extern struct _malstats _mstats; struct bucket_stats malloc_bucket_stats (size) @@ -40,7 +46,7 @@ malloc_bucket_stats (size) if (size < 0 || size >= NBUCKETS) { v.blocksize = 0; - v.nused = v.nmal = v.nmorecore = v.nsplit = 0; + v.nused = v.nmal = v.nmorecore = v.nlesscore = v.nsplit = 0; return v; } @@ -48,7 +54,9 @@ malloc_bucket_stats (size) v.nused = _mstats.nmalloc[size]; v.nmal = _mstats.tmalloc[size]; v.nmorecore = _mstats.nmorecore[size]; + v.nlesscore = _mstats.nlesscore[size]; v.nsplit = _mstats.nsplit[size]; + v.ncoalesce = _mstats.ncoalesce[size]; v.nfree = malloc_free_blocks (size); /* call back to malloc.c */ @@ -86,16 +94,18 @@ _print_malloc_stats (s, fp) unsigned long totused, totfree; struct bucket_stats v; - fprintf (fp, "Memory allocation statistics: %s\n\tsize\tfree\tin use\ttotal\tmorecore\tsplit\n", s ? s : ""); + fprintf (fp, "Memory allocation statistics: %s\n size\tfree\tin use\ttotal\tmorecore lesscore split\tcoalesce\n", s ? s : ""); for (i = totused = totfree = 0; i < NBUCKETS; i++) { v = malloc_bucket_stats (i); - fprintf (fp, "%12lu\t%4d\t%6d\t%5d\t%8d\t%5d\n", (unsigned long)v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nsplit); + if (v.nmal > 0) + fprintf (fp, "%8lu\t%4d\t%6d\t%5d\t%8d\t%d %5d %8d\n", (unsigned long)v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nlesscore, v.nsplit, v.ncoalesce); totfree += v.nfree * v.blocksize; totused += v.nused * v.blocksize; } fprintf (fp, "\nTotal bytes in use: %lu, total bytes free: %lu\n", totused, totfree); + fprintf (fp, "\nTotal bytes requested by application: %lu\n", _mstats.bytesreq); fprintf (fp, "Total mallocs: %d, total frees: %d, total reallocs: %d (%d copies)\n", _mstats.nmal, _mstats.nfre, _mstats.nrealloc, _mstats.nrcopy); fprintf (fp, "Total sbrks: %d, total bytes via sbrk: %d\n", @@ -120,24 +130,51 @@ fprint_malloc_stats (s, fp) } #define TRACEROOT "/var/tmp/maltrace/trace." -extern char *inttostr (); +static char mallbuf[1024]; void -trace_malloc_stats (s) - char *s; +trace_malloc_stats (s, fn) + char *s, *fn; { - char ibuf[32], *ip; - char fname[64]; - long p; + char defname[sizeof (TRACEROOT) + 64]; + char fname[1024]; + long l; FILE *fp; - p = getpid(); - ip = inttostr(p, ibuf, sizeof(ibuf)); - strcpy (fname, TRACEROOT); - strcat (fname, ip); - fp = fopen(fname, "w"); + l = (long)getpid (); + if (fn == 0) + { + sprintf (defname, "%s%ld", TRACEROOT, l); + fp = fopen(defname, "w"); + } + else + { + char *p, *q, *r; + char pidbuf[32]; + int sp; + + sprintf (pidbuf, "%ld", l); + if ((strlen (pidbuf) + strlen (fn) + 2) >= sizeof (fname)) + return; + for (sp = 0, p = fname, q = fn; *q; ) + { + if (sp == 0 && *q == '%' && q[1] == 'p') + { + sp = 1; + for (r = pidbuf; *r; ) + *p++ = *r++; + q += 2; + } + else + *p++ = *q++; + } + *p = '\0'; + fp = fopen (fname, "w"); + } + if (fp) { + setvbuf (fp, mallbuf, _IOFBF, sizeof (mallbuf)); _print_malloc_stats (s, fp); fflush(fp); fclose(fp); diff --git a/lib/malloc/stub.c b/lib/malloc/stub.c index d297c65..94693d8 100644 --- a/lib/malloc/stub.c +++ b/lib/malloc/stub.c @@ -1,3 +1,21 @@ +/* Copyright (C) 1993-2002 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any later + version. + + Bash is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License along + with Bash; see the file COPYING. If not, write to the Free Software + Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ + void bash_malloc_stub() { diff --git a/lib/malloc/table.c b/lib/malloc/table.c index d7c48c5..051b573 100644 --- a/lib/malloc/table.c +++ b/lib/malloc/table.c @@ -34,7 +34,9 @@ extern int malloc_register; #define FIND_EXIST 0x02 /* find existing entry */ static int table_count = 0; +static int table_allocated = 0; static mr_table_t mem_table[REG_TABLE_SIZE]; +static mr_table_t mem_overflow; /* * NOTE: taken from dmalloc (http://dmalloc.com) and modified. @@ -43,7 +45,7 @@ static unsigned int mt_hash (key) const PTR_T key; { - unsigned int a, b, c, len; + unsigned int a, b, c; unsigned long x; /* set up the internal state */ @@ -61,10 +63,10 @@ static unsigned int which_bucket (mem) PTR_T mem; { - return (mt_hash ((unsigned char *)mem) % REG_TABLE_SIZE); + return (mt_hash ((unsigned char *)mem) & (REG_TABLE_SIZE-1)); } #else -#define which_bucket(mem) (mt_hash ((unsigned char *)(mem)) % REG_TABLE_SIZE); +#define which_bucket(mem) (mt_hash ((unsigned char *)(mem)) & (REG_TABLE_SIZE-1)); #endif static mr_table_t * @@ -76,6 +78,9 @@ find_entry (mem, flags) register mr_table_t *tp; mr_table_t *endp, *lastp; + if (mem_overflow.mem == mem) + return (&mem_overflow); + bucket = which_bucket (mem); /* get initial hash */ tp = endp = mem_table + bucket; lastp = mem_table + REG_TABLE_SIZE; @@ -105,17 +110,26 @@ find_entry (mem, flags) /* oops. table is full. replace an existing free entry. */ do { + /* If there are no free entries, punt right away without searching. */ + if (table_allocated == REG_TABLE_SIZE) + break; + if (tp->flags & MT_FREE) { memset(tp, 0, sizeof (mr_table_t)); return (tp); } tp++; + + if (tp == lastp) + tp = mem_table; } while (tp != endp); - /* wow. entirely full. return NULL. */ - return ((mr_table_t *)NULL); + /* wow. entirely full. return mem_overflow dummy entry. */ + tp = &mem_overflow; + memset (tp, 0, sizeof (mr_table_t)); + return tp; } mr_table_t * @@ -158,7 +172,7 @@ mregister_alloc (tag, mem, size, file, line) if (tentry == 0) { /* oops. table is full. punt. */ - fprintf (stderr, "register_alloc: alloc table is full?\n"); + fprintf (stderr, "register_alloc: alloc table is full with FIND_ALLOC?\n"); return; } @@ -175,6 +189,9 @@ mregister_alloc (tag, mem, size, file, line) tentry->file = file; tentry->line = line; tentry->nalloc++; + + if (tentry != &mem_overflow) + table_allocated++; } void @@ -190,7 +207,9 @@ mregister_free (mem, size, file, line) if (tentry == 0) { /* oops. not found. */ +#if 0 fprintf (stderr, "register_free: %p not in allocation table?\n", mem); +#endif return; } if (tentry->flags & MT_FREE) @@ -204,6 +223,9 @@ mregister_free (mem, size, file, line) tentry->file = file; tentry->line = line; tentry->nfree++; + + if (tentry != &mem_overflow) + table_allocated--; } /* If we ever add more flags, this will require changes. */ @@ -250,6 +272,7 @@ void mregister_table_init () { memset (mem_table, 0, sizeof(mr_table_t) * REG_TABLE_SIZE); + memset (&mem_overflow, 0, sizeof (mr_table_t)); table_count = 0; } diff --git a/lib/malloc/table.h b/lib/malloc/table.h index f39beb8..9b2cddd 100644 --- a/lib/malloc/table.h +++ b/lib/malloc/table.h @@ -55,12 +55,12 @@ typedef struct mr_table { #define REG_TABLE_SIZE 8192 -extern mr_table_t *mr_table_entry (); -extern void mregister_alloc (); -extern void mregister_free (); +extern mr_table_t *mr_table_entry __P((PTR_T)); +extern void mregister_alloc __P((const char *, PTR_T, size_t, const char *, int)); +extern void mregister_free __P((PTR_T, int, const char *, int)); extern void mregister_describe_mem (); -extern void mregister_dump_table (); -extern void mregister_table_init (); +extern void mregister_dump_table __P((void)); +extern void mregister_table_init __P((void)); /* NOTE: HASH_MIX taken from dmalloc (http://dmalloc.com) */ diff --git a/lib/malloc/trace.c b/lib/malloc/trace.c index 418509e..ddd62f0 100644 --- a/lib/malloc/trace.c +++ b/lib/malloc/trace.c @@ -32,6 +32,7 @@ static int _mtrace_verbose = 0; #ifdef MALLOC_TRACE FILE *_mtrace_fp = NULL; +extern char _malloc_trace_buckets[]; void mtrace_alloc (tag, mem, size, file, line) @@ -72,7 +73,7 @@ mtrace_free (mem, size, file, line) #endif /* MALLOC_TRACE */ int -malloc_set_trace(n) +malloc_set_trace (n) int n; { int old; @@ -84,10 +85,19 @@ malloc_set_trace(n) } void -malloc_set_tracefp(fp) +malloc_set_tracefp (fp) FILE *fp; { #ifdef MALLOC_TRACE _mtrace_fp = fp ? fp : stderr; #endif } + +void +malloc_trace_bin (n) + int n; +{ +#ifdef MALLOC_TRACE + _malloc_trace_buckets[n] = 1; +#endif +} diff --git a/lib/malloc/watch.c b/lib/malloc/watch.c new file mode 100644 index 0000000..6594a3f --- /dev/null +++ b/lib/malloc/watch.c @@ -0,0 +1,150 @@ +/* watch.c - watchpoint functions for malloc */ + +/* Copyright (C) 2001 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2, or (at your option) any later + version. + + Bash is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License along + with Bash; see the file COPYING. If not, write to the Free Software + Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> + +#include "imalloc.h" + +#ifdef MALLOC_WATCH +#include "watch.h" + +#define WATCH_MAX 32 + +int _malloc_nwatch; +static PTR_T _malloc_watch_list[WATCH_MAX]; + +static void +watch_warn (addr, file, line, type, data) + PTR_T addr; + const char *file; + int line, type; + unsigned long data; +{ + char *tag; + + if (type == W_ALLOC) + tag = "allocated"; + else if (type == W_FREE) + tag = "freed"; + else if (type == W_REALLOC) + tag = "requesting resize"; + else if (type == W_RESIZED) + tag = "just resized"; + else + tag = "bug: unknown operation"; + + fprintf (stderr, "malloc: watch alert: %p %s ", addr, tag); + if (data != (unsigned long)-1) + fprintf (stderr, "(size %lu) ", data); + fprintf (stderr, "from '%s:%d'\n", file ? file : "unknown", line); +} + +void +_malloc_ckwatch (addr, file, line, type, data) + PTR_T addr; + const char *file; + int line, type; + unsigned long data; +{ + register int i; + + for (i = _malloc_nwatch - 1; i >= 0; i--) + { + if (_malloc_watch_list[i] == addr) + { + watch_warn (addr, file, line, type, data); + return; + } + } +} +#endif /* MALLOC_WATCH */ + +PTR_T +malloc_watch (addr) + PTR_T addr; +{ + register int i; + PTR_T ret; + + if (addr == 0) + return addr; + ret = (PTR_T)0; + +#ifdef MALLOC_WATCH + for (i = _malloc_nwatch - 1; i >= 0; i--) + { + if (_malloc_watch_list[i] == addr) + break; + } + if (i < 0) + { + if (_malloc_nwatch == WATCH_MAX) /* full, take out first */ + { + ret = _malloc_watch_list[0]; + _malloc_nwatch--; + for (i = 0; i < _malloc_nwatch; i++) + _malloc_watch_list[i] = _malloc_watch_list[i+1]; + } + _malloc_watch_list[_malloc_nwatch++] = addr; + } +#endif + + return ret; +} + +/* Remove a watchpoint set on ADDR. If ADDR is NULL, remove all + watchpoints. Returns ADDR if everything went OK, NULL if ADDR was + not being watched. */ +PTR_T +malloc_unwatch (addr) + PTR_T addr; +{ +#ifdef MALLOC_WATCH + register int i; + + if (addr == 0) + { + for (i = 0; i < _malloc_nwatch; i++) + _malloc_watch_list[i] = (PTR_T)0; + _malloc_nwatch = 0; + return ((PTR_T)0); + } + else + { + for (i = 0; i < _malloc_nwatch; i++) + { + if (_malloc_watch_list[i] == addr) + break; + } + if (i == _malloc_nwatch) + return ((PTR_T)0); /* not found */ + /* shuffle everything from i+1 to end down 1 */ + _malloc_nwatch--; + for ( ; i < _malloc_nwatch; i++) + _malloc_watch_list[i] = _malloc_watch_list[i+1]; + return addr; + } +#else + return ((PTR_T)0); +#endif +} diff --git a/lib/malloc/watch.h b/lib/malloc/watch.h new file mode 100644 index 0000000..d66d80e --- /dev/null +++ b/lib/malloc/watch.h @@ -0,0 +1,39 @@ +/* watch.h - definitions for tables for keeping track of allocated memory */ + +/* Copyright (C) 2001 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ + +#ifndef _MWATCH_H +#define _MWATCH_H + +#include "imalloc.h" + +#ifdef MALLOC_WATCH + +/* `Events' for watchpoints */ + +#define W_ALLOC 0x01 +#define W_FREE 0x02 +#define W_REALLOC 0x04 +#define W_RESIZED 0x08 + +extern int _malloc_nwatch; + +extern void _malloc_ckwatch __P((PTR_T, const char *, int, int, unsigned long)); + +#endif /* MALLOC_WATCH */ + +#endif /* _MWATCH_H */ |