Imported from ../bash-2.05a.tar.gz.

This commit is contained in:
Jari Aalto 2001-11-13 17:56:06 +00:00
commit f73dda092b
303 changed files with 37069 additions and 28812 deletions

View file

@ -60,8 +60,6 @@ CCFLAGS = ${PROFILE_FLAGS} ${INCLUDES} $(DEFS) $(LOCAL_DEFS) $(LOCAL_CFLAGS) \
$(CC) $(CCFLAGS) -c $<
MALLOC_SOURCE = malloc.c
GMALLOC_SOURCE = gmalloc.c
NMALLOC_SOURCE = nmalloc.c
STUB_SOURCE = stub.c
ALLOCA_SOURCE = alloca.c
@ -71,14 +69,10 @@ MALLOC_SRC = @MALLOC_SRC@
MALLOC = @MALLOC@
ALLOCA = @ALLOCA@
MALLOC_OBJS = malloc.o $(ALLOCA) stub.o
GMALLOC_OBJS = gmalloc.o $(ALLOCA) stub.o
NMALLOC_OBJS = nmalloc.o $(ALLOCA) stub.o
NMALLOC2_OBJS = nmalloc2.o $(ALLOCA) stub.o
NGMALLOC_OBJS = ngmalloc.o $(ALLOCA) stub.o
MALLOC_OBJS = malloc.o $(ALLOCA) trace.o stats.o table.o
STUB_OBJS = $(ALLOCA) stub.o
.PHONY: malloc gmalloc stubmalloc nmalloc ngmalloc nmalloc2
.PHONY: malloc stubmalloc
all: malloc
@ -87,31 +81,16 @@ malloc: ${MALLOC_OBJS}
${AR} ${ARFLAGS} libmalloc.a ${MALLOC_OBJS}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
nmalloc: ${NMALLOC_OBJS}
${RM} libmalloc.a
${AR} ${ARFLAGS} libmalloc.a ${NMALLOC_OBJS}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
nmalloc2: ${NMALLOC2_OBJS}
${RM} libmalloc.a
${AR} ${ARFLAGS} libmalloc.a ${NMALLOC2_OBJS}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
gmalloc: ${GMALLOC_OBJS}
${RM} libmalloc.a
${AR} ${ARFLAGS} libmalloc.a ${GMALLOC_OBJS}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
ngmalloc: ${NGMALLOC_OBJS}
${RM} libmalloc.a
${AR} ${ARFLAGS} libmalloc.a ${NGMALLOC_OBJS}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
stubmalloc: ${STUB_OBJS}
${RM} libmalloc.a
${AR} ${ARFLAGS} libmalloc.a ${STUB_OBJS}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
alloca: ${ALLOCA}
${RM} libmalloc.a
${AR} ${ARFLAGS} libmalloc.a ${ALLOCA}
-test -n "$(RANLIB)" && $(RANLIB) libmalloc.a
alloca.o: $(srcdir)/$(ALLOCA_SOURCE)
$(CC) $(CCFLAGS) -c $(srcdir)/$(ALLOCA_SOURCE)
@- if test "$(ALLOCA_OBJECT)" != alloca.o ; then \
@ -126,12 +105,18 @@ distclean realclean maintainer-clean: clean
alloca.o: $(BUILD_DIR)/config.h
malloc.o: $(BUILD_DIR)/config.h $(topdir)/bashtypes.h getpagesize.h
nmalloc.o: $(BUILD_DIR)/config.h $(topdir)/bashtypes.h getpagesize.h
nmalloc2.o: $(BUILD_DIR)/config.h $(topdir)/bashtypes.h getpagesize.h
xmalloc.o: $(BUILD_DIR)/config.h $(BASHINCDIR)/ansi_stdlib.h
gmalloc.o: $(BUILD_DIR)/config.h
trace.o: ${BUILD_DIR}/config.h
table.o: ${BUILD_DIR}/config.h
malloc.o: ${srcdir}/imalloc.h ${srcdir}/mstats.h
stats.o: ${srcdir}/imalloc.h ${srcdir}/mstats.h
trace.o: ${srcdir}/imalloc.h
table.o: ${srcdir}/imalloc.h ${srcdir}/table.h
# Rules for deficient makes, like SunOS and Solaris
stub.o: stub.c
malloc.o: malloc.c
gmalloc.o: gmalloc.c
table.o: table.c
trace.o: trace.c
stats.o: stats.c

View file

@ -28,6 +28,8 @@
/* If compiling with GCC 2, this file's not needed. */
#if !defined (__GNUC__) || __GNUC__ < 2
#include <bashtypes.h> /* for size_t */
/* If alloca is defined somewhere, this file is not needed. */
#ifndef alloca
@ -155,7 +157,7 @@ static header *last_alloca_header = NULL; /* -> last alloca header. */
pointer
alloca (size)
unsigned size;
size_t size;
{
auto char probe; /* Probes stack depth: */
register char *depth = ADDRESS_FUNCTION (probe);

File diff suppressed because it is too large Load diff

67
lib/malloc/imalloc.h Normal file
View file

@ -0,0 +1,67 @@
/* imalloc.h -- internal malloc definitions shared by source files. */
/* 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. */
/* Must be included *after* config.h */
#ifndef _IMALLOC_H_
#define _IMALLOC_H
#ifdef MALLOC_DEBUG
#define MALLOC_STATS
#define MALLOC_TRACE
#define MALLOC_REGISTER
#endif
/* Generic pointer type. */
#ifndef PTR_T
# if defined (__STDC__)
# define PTR_T void *
# else
# define PTR_T char *
# endif
#endif
#if !defined (NULL)
# define NULL 0
#endif
#if !defined (__STRING)
# if defined (HAVE_STRINGIZE)
# define __STRING(x) #x
# else
# define __STRING(x) "x"
# endif /* !HAVE_STRINGIZE */
#endif /* !__STRING */
#if __GNUC__ > 1
# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
#else /* !__GNUC__ */
# if !defined (HAVE_BCOPY)
# if !defined (HAVE_MEMMOVE)
# define FASTCOPY(s, d, n) memcpy (d, s, n)
# else
# define FASTCOPY(s, d, n) memmove (d, s, n)
# endif /* !HAVE_MEMMOVE */
# else /* HAVE_BCOPY */
# define FASTCOPY(s, d, n) bcopy (s, d, n)
# endif /* HAVE_BCOPY */
#endif /* !__GNUC__ */
#endif

View file

@ -45,7 +45,6 @@ what you give them. Help stamp out software-hoarding! */
* realloc knows how to return same block given, just changing its size,
* if the power of 2 is correct.
*/
#define MALLOC_STATS /* for the time being */
/*
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
@ -67,8 +66,18 @@ what you give them. Help stamp out software-hoarding! */
#if defined (SHELL)
# include "bashtypes.h"
# 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)
@ -84,40 +93,24 @@ what you give them. Help stamp out software-hoarding! */
# include <strings.h>
#endif
#if defined (MALLOC_STATS) || !defined (botch)
# include <stdio.h>
#endif /* MALLOC_STATS || !botch */
#include <stdio.h>
/* Define getpagesize () if the system does not. */
#ifndef HAVE_GETPAGESIZE
# include "getpagesize.h"
#endif
#if __GNUC__ > 1
# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
#else /* !__GNUC__ */
# if !defined (HAVE_BCOPY)
# if !defined (HAVE_MEMMOVE)
# define FASTCOPY(s, d, n) memcpy (d, s, n)
# else
# define FASTCOPY(s, d, n) memmove (d, s, n)
# endif /* !HAVE_MEMMOVE */
# else /* HAVE_BCOPY */
# define FASTCOPY(s, d, n) bcopy (s, d, n)
# endif /* HAVE_BCOPY */
#endif /* !__GNUC__ */
/* Generic pointer type. */
#ifndef PTR_T
# if defined (__STDC__)
# define PTR_T void *
# else
# define PTR_T char *
# endif
#include "imalloc.h"
#ifdef MALLOC_STATS
# include "mstats.h"
#endif
#ifdef MALLOC_REGISTER
# include "table.h"
#endif
#if !defined (NULL)
# define NULL 0
/* System-specific omissions. */
#ifdef HPUX
# define NO_VALLOC
#endif
#define NBUCKETS 30
@ -130,66 +123,6 @@ what you give them. Help stamp out software-hoarding! */
being the distance to the true
beginning of the block. */
#if !defined (SBRK_DECLARED)
extern char *sbrk ();
#endif /* !SBRK_DECLARED */
#ifdef MALLOC_STATS
/*
* 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.
* TBCOALESCE is the number of times two adjacent smaller blocks off the free
* list were combined to satisfy a larger request.
*/
struct _malstats {
int nmalloc[NBUCKETS];
int tmalloc[NBUCKETS];
int nmorecore[NBUCKETS];
int nmal;
int nfre;
int nrealloc;
int nrcopy;
int nrecurse;
int nsbrk;
bits32_t tsbrk;
bits32_t bytesused;
bits32_t bytesfree;
int tbsplit;
int nsplit[NBUCKETS];
int tbcoalesce;
};
static struct _malstats _mstats;
/* 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. */
struct bucket_stats {
u_bits32_t blocksize;
int nfree;
int nused;
int nmal;
int nmorecore;
int nsplit;
};
#endif /* MALLOC_STATS */
/* We have a flag indicating whether memory is allocated, an index in
nextf[], a size field, and a sentinel value to determine whether or
@ -198,13 +131,13 @@ struct bucket_stats {
enough room in the block for the new size. Range checking is always
done. */
union mhead {
bits64_t mh_align; /* 8 */
bits64_t mh_align; /* 8 */
struct {
char mi_alloc; /* ISALLOC or ISFREE */ /* 1 */
char mi_index; /* index in nextf[] */ /* 1 */
char mi_alloc; /* ISALLOC or ISFREE */ /* 1 */
char mi_index; /* index in nextf[] */ /* 1 */
/* Remainder are valid only when block is allocated */
u_bits32_t mi_nbytes; /* # of bytes allocated */ /* 4 */
u_bits16_t mi_magic2;/* should be == MAGIC2 */ /* 2 */
u_bits16_t mi_magic2; /* should be == MAGIC2 */ /* 2 */
u_bits32_t mi_nbytes; /* # of bytes allocated */ /* 4 */
} minfo;
};
#define mh_alloc minfo.mi_alloc
@ -222,38 +155,28 @@ union mhead {
#define CHAIN(a) \
(*(union mhead **) (sizeof (char *) + (char *) (a)))
#if defined (botch)
extern void botch ();
#else
static void
botch (s)
char *s;
{
fprintf (stderr, "\r\nmalloc: assertion botched: %s\r\n", s);
(void)fflush (stderr);
abort ();
}
#endif /* !botch */
#if !defined (__STRING)
# if defined (__STDC__)
# define __STRING(x) #x
# else
# define __STRING(x) "x"
# endif
#endif /* !__STRING */
/* To implement range checking, we write magic values in at the beginning
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 */
/* Written in the 2 bytes before the block's real space (-4 bytes) */
#define MAGIC2 0x5555
#define ASSERT(p) do { if (!(p)) botch(__STRING(p)); } while (0)
#define MSLOP 4 /* 4 bytes extra for MAGIC1s */
/* 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 ASSERT(p) \
do \
{ \
if (!(p)) xbotch((PTR_T)0, ERR_ASSERT_FAILED, __STRING(p), file, line); \
} \
while (0)
/* Minimum and maximum bucket indices for block splitting (and to bound
the search for a block to split). */
#define SPLIT_MIN 3
@ -266,6 +189,23 @@ botch (s)
#define MIN_COMBINE_FREE 4
/* Flags for the internal functions. */
#define MALLOC_WRAPPER 0x01 /* wrapper function */
#define MALLOC_INTERNAL 0x02 /* internal function calling another */
#define MALLOC_NOTRACE 0x04 /* don't trace this allocation or free */
#define MALLOC_NOREG 0x08 /* don't register this allocation or free */
/* Future use. */
#define ERR_DUPFREE 0x01
#define ERR_UNALLOC 0x02
#define ERR_UNDERFLOW 0x04
#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))))
/* nextf[i] is free list of blocks of size 2**(i + 3) */
static union mhead *nextf[NBUCKETS];
@ -278,11 +218,74 @@ static int pagesz; /* system page size. */
static int pagebucket; /* bucket for requests a page in size */
static int maxbuck; /* highest bucket receiving allocation request. */
/* 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));
static void internal_free __P((PTR_T, const char *, int, int));
static PTR_T internal_memalign __P((unsigned int, size_t, const char *, int, int));
#ifndef NO_CALLOC
static PTR_T internal_calloc __P((size_t, size_t, const char *, int, int));
static void internal_cfree __P((PTR_T, const char *, int, int));
#endif
#ifndef NO_VALLOC
static PTR_T internal_valloc __P((size_t, const char *, int, int));
#endif
#if defined (botch)
extern void botch ();
#else
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 */
#ifdef SHELL
extern int interrupt_immediately;
extern int signal_is_trapped ();
extern int signal_is_trapped __P((int));
#endif
/* 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 */
#if !defined (botch)
static void
botch (s, file, line)
{
fprintf (stderr, "malloc: failed assertion: %s\n", s);
(void)fflush (stderr);
abort ();
}
#endif
/* print the file and line number that caused the assertion failure and
call botch() to do whatever the application wants with the information */
static void
xbotch (mem, e, s, file, line)
PTR_T mem;
int e;
const char *s;
const char *file;
int line;
{
fprintf (stderr, "\r\nmalloc: %s:%d: assertion botched\r\n",
file ? file : "unknown", line);
#ifdef MALLOC_REGISTER
if (mem != NULL && malloc_register)
mregister_describe_mem (mem, stderr);
#endif
(void)fflush (stderr);
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
@ -319,7 +322,7 @@ bcoalesce (nu)
/* 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)
botch ("bcoalesce: CHAIN(mp2) != mp1");
xbotch ((PTR_T)0, 0, "bcoalesce: CHAIN(mp2) != mp1", (char *)NULL, 0);
siz = 1 << (nbuck + 3);
if (CHAIN (mp1) != (union mhead *)((char *)mp1 + siz))
return; /* not adjacent */
@ -464,11 +467,7 @@ morecore (nu) /* ask system for more memory */
/* Try to split a larger block here, if we're within the range of sizes
to split. */
#if 0
if (nu >= SPLIT_MIN && nu < SPLIT_MAX)
#else
if (nu >= SPLIT_MIN)
#endif
{
bsplit (nu);
if (nextf[nu] != 0)
@ -522,7 +521,7 @@ morecore (nu) /* ask system for more memory */
/* shouldn't happen, but just in case -- require 8-byte alignment */
if ((long)mp & 7)
{
mp = (union mhead *) (((long)mp + 8) & ~7);
mp = (union mhead *) (((long)mp + 7) & ~7);
nblks--;
}
@ -565,9 +564,11 @@ malloc_debug_dummy ()
write (1, "malloc_debug_dummy\n", 19);
}
PTR_T
malloc (n) /* get a block */
static PTR_T
internal_malloc (n, file, line, flags) /* get a block */
size_t n;
const char *file;
int line, flags;
{
register union mhead *p;
register long nbytes;
@ -610,10 +611,10 @@ malloc (n) /* get a block */
}
/* Figure out how many bytes are required, rounding up to the nearest
multiple of 4, then figure out which nextf[] area to use. Try to
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 = (n + sizeof *p + MSLOP + 3) & ~3;
nbytes = ALLOCATED_BYTES(n);
nunits = 0;
if (nbytes <= (pagesz >> 1))
{
@ -636,6 +637,10 @@ malloc (n) /* get a block */
}
}
/* Silently reject too-large requests. */
if (nunits >= NBUCKETS)
return ((PTR_T) NULL);
/* In case this is reentrant use of malloc from signal handler,
pick a block size that no other malloc level is currently
trying to allocate. That's the easiest harmless way not to
@ -666,12 +671,12 @@ malloc (n) /* 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)
botch ("malloc: block on free list clobbered");
xbotch ((PTR_T)0, 0, "malloc: block on free list clobbered", file, line);
/* Fill in the info, and if range checking, set up the magic numbers */
/* Fill in the info, and set up the magic numbers for range checking. */
p->mh_alloc = ISALLOC;
p->mh_nbytes = n;
p->mh_magic2 = MAGIC2;
p->mh_nbytes = n;
{
register char *m = (char *) (p + 1) + n;
@ -686,16 +691,31 @@ malloc (n) /* get a block */
_mstats.tmalloc[nunits]++;
_mstats.nmal++;
#endif /* MALLOC_STATS */
#ifdef MALLOC_TRACE
if (malloc_trace && (flags & MALLOC_NOTRACE) == 0)
mtrace_alloc ("malloc", p + 1, n, file, line);
#endif
#ifdef MALLOC_REGISTER
if (malloc_register && (flags & MALLOC_NOREG) == 0)
mregister_alloc ("malloc", p + 1, n, file, line);
#endif
return (char *) (p + 1); /* XXX - should be cast to PTR_T? */
}
void
free (mem)
static void
internal_free (mem, file, line, flags)
PTR_T mem;
const char *file;
int line, flags;
{
register union mhead *p;
register char *ap;
register int nunits;
register unsigned int nbytes;
int ubytes; /* caller-requested size */
if ((ap = (char *)mem) == 0)
return;
@ -708,15 +728,37 @@ free (mem)
p = (union mhead *) ap - 1;
}
#if defined (MALLOC_TRACE) || defined (MALLOC_REGISTER)
if (malloc_trace || malloc_register)
ubytes = p->mh_nbytes;
#endif
if (p->mh_alloc != ISALLOC)
{
if (p->mh_alloc == ISFREE)
botch ("free: called with already freed block argument");
xbotch (mem, ERR_DUPFREE,
"free: called with already freed block argument", file, line);
else
botch ("free: called with unallocated block argument");
xbotch (mem, ERR_UNALLOC,
"free: called with unallocated block argument", file, line);
}
ASSERT (p->mh_magic2 == MAGIC2);
nunits = p->mh_index;
nbytes = ALLOCATED_BYTES(p->mh_nbytes);
/* Since the sizeof(u_bits32_t) bytes before the memory handed to the user
are now used for the number of bytes allocated, a simple check of
mh_magic2 is no longer sufficient to catch things like p[-1] = 'x'.
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. */
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);
@ -725,15 +767,11 @@ free (mem)
zmemset (mem, 0xcf, p->mh_nbytes);
#endif
nunits = p->mh_index;
ASSERT (nunits < NBUCKETS);
p->mh_alloc = ISFREE;
#if 0
if (busy[nunits] == 1)
botch ("calling free %d while in malloc for %d", nunits, nunits);
#endif
return; /* this is bogus, but at least it won't corrupt the chains */
/* Protect against signal handlers calling malloc. */
busy[nunits] = 1;
@ -746,12 +784,24 @@ free (mem)
_mstats.nmalloc[nunits]--;
_mstats.nfre++;
#endif /* MALLOC_STATS */
#ifdef MALLOC_TRACE
if (malloc_trace && (flags & MALLOC_NOTRACE) == 0)
mtrace_free (mem, ubytes, file, line);
#endif
#ifdef MALLOC_REGISTER
if (malloc_register && (flags & MALLOC_NOREG) == 0)
mregister_free (mem, ubytes, file, line);
#endif
}
PTR_T
realloc (mem, n)
static PTR_T
internal_realloc (mem, n, file, line, flags)
PTR_T mem;
register size_t n;
const char *file;
int line, flags;
{
register union mhead *p;
register u_bits32_t tocopy;
@ -765,25 +815,43 @@ realloc (mem, n)
if (n == 0)
{
free (mem);
internal_free (mem, file, line, MALLOC_INTERNAL);
return (NULL);
}
if ((p = (union mhead *) mem) == 0)
return malloc (n);
return internal_malloc (n, file, line, MALLOC_INTERNAL);
p--;
nunits = p->mh_index;
ASSERT (p->mh_alloc == ISALLOC);
ASSERT (nunits < NBUCKETS);
if (p->mh_alloc != ISALLOC)
xbotch (mem, ERR_UNALLOC,
"realloc: called with unallocated block argument", file, line);
ASSERT (p->mh_magic2 == MAGIC2);
nbytes = ALLOCATED_BYTES(p->mh_nbytes);
/* Since the sizeof(u_bits32_t) bytes before the memory handed to the user
are now used for the number of bytes allocated, a simple check of
mh_magic2 is no longer sufficient to catch things like p[-1] = 'x'.
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. */
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);
/* See if desired size rounds to same power of 2 as actual size. */
nbytes = (n + sizeof *p + MSLOP + 7) & ~7;
nbytes = ALLOCATED_BYTES(n);
/* If ok, use the same block, just marking its size as changed. */
if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
if (IN_BUCKET(nbytes, nunits))
{
m = (char *)mem + tocopy;
*m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
@ -800,182 +868,240 @@ realloc (mem, n)
if (n < tocopy)
tocopy = n;
if ((m = malloc (n)) == 0)
if ((m = internal_malloc (n, file, line, MALLOC_INTERNAL|MALLOC_NOTRACE|MALLOC_NOREG)) == 0)
return 0;
FASTCOPY (mem, m, tocopy);
free (mem);
internal_free (mem, file, line, MALLOC_INTERNAL);
#ifdef MALLOC_TRACE
if (malloc_trace && (flags & MALLOC_NOTRACE) == 0)
mtrace_alloc ("realloc", m, n, file, line);
#endif
#ifdef MALLOC_REGISTER
if (malloc_register && (flags & MALLOC_NOREG) == 0)
mregister_alloc ("realloc", m, n, file, line);
#endif
return m;
}
static PTR_T
internal_memalign (alignment, size, file, line, flags)
unsigned int alignment;
size_t size;
const char *file;
int line, flags;
{
register char *ptr;
register char *aligned;
register union mhead *p;
ptr = internal_malloc (size + alignment, file, line, MALLOC_INTERNAL);
if (ptr == 0)
return 0;
/* If entire block has the desired alignment, just accept it. */
if (((long) ptr & (alignment - 1)) == 0)
return ptr;
/* Otherwise, get address of byte in the block that has that alignment. */
#if 0
aligned = (char *) (((long) ptr + alignment - 1) & -alignment);
#else
aligned = (char *) (((long) ptr + alignment - 1) & (~alignment + 1));
#endif
/* Store a suitable indication of how to free the block,
so that free can find the true beginning of it. */
p = (union mhead *) aligned - 1;
p->mh_nbytes = aligned - ptr;
p->mh_alloc = ISMEMALIGN;
return aligned;
}
#if !defined (NO_VALLOC)
/* This runs into trouble with getpagesize on HPUX, and Multimax machines.
Patching out seems cleaner than the ugly fix needed. */
static PTR_T
internal_valloc (size, file, line, flags)
size_t size;
const char *file;
int line, flags;
{
return internal_memalign (getpagesize (), size, file, line, flags|MALLOC_INTERNAL);
}
#endif /* !NO_VALLOC */
#ifndef NO_CALLOC
static PTR_T
internal_calloc (n, s, file, line, flags)
size_t n, s;
const char *file;
int line, flags;
{
size_t total;
PTR_T result;
total = n * s;
result = internal_malloc (total, file, line, flags|MALLOC_INTERNAL);
if (result)
zmemset (result, 0, total);
return result;
}
static void
internal_cfree (p, file, line, flags)
PTR_T p;
const char *file;
int line, flags;
{
internal_free (p, file, line, flags|MALLOC_INTERNAL);
}
#endif /* !NO_CALLOC */
#ifdef MALLOC_STATS
int
malloc_free_blocks (size)
int size;
{
int nfree;
register union mhead *p;
nfree = 0;
for (p = nextf[size]; p; p = CHAIN (p))
nfree++;
return nfree;
}
#endif
#if defined (SHELL)
PTR_T
sh_malloc (bytes, file, line)
size_t bytes;
const char *file;
int line;
{
return internal_malloc (bytes, file, line, MALLOC_WRAPPER);
}
PTR_T
sh_realloc (ptr, size, file, line)
PTR_T ptr;
size_t size;
const char *file;
int line;
{
return internal_realloc (ptr, size, file, line, MALLOC_WRAPPER);
}
void
sh_free (mem, file, line)
PTR_T mem;
const char *file;
int line;
{
internal_free (mem, file, line, MALLOC_WRAPPER);
}
PTR_T
sh_memalign (alignment, size, file, line)
unsigned int alignment;
size_t size;
const char *file;
int line;
{
return internal_memalign (alignment, size, file, line, MALLOC_WRAPPER);
}
#ifndef NO_CALLOC
PTR_T
sh_calloc (n, s, file, line)
size_t n, s;
const char *file;
int line;
{
return internal_calloc (n, s, file, line, MALLOC_WRAPPER);
}
void
sh_cfree (mem, file, line)
PTR_T mem;
const char *file;
int line;
{
internal_cfree (mem, file, line, MALLOC_WRAPPER);
}
#endif
#ifndef NO_VALLOC
PTR_T
sh_valloc (size, file, line)
size_t size;
const char *file;
int line;
{
return internal_valloc (size, file, line, MALLOC_WRAPPER);
}
#endif
#endif
/* Externally-available functions that call their internal counterparts. */
PTR_T
malloc (size)
size_t size;
{
return internal_malloc (size, (char *)NULL, 0, 0);
}
PTR_T
realloc (mem, nbytes)
PTR_T mem;
size_t nbytes;
{
return internal_realloc (mem, nbytes, (char *)NULL, 0, 0);
}
void
free (mem)
PTR_T mem;
{
internal_free (mem, (char *)NULL, 0, 0);
}
PTR_T
memalign (alignment, size)
unsigned int alignment;
size_t size;
{
register char *ptr;
register char *aligned;
register union mhead *p;
ptr = malloc (size + alignment);
if (ptr == 0)
return 0;
/* If entire block has the desired alignment, just accept it. */
if (((int) ptr & (alignment - 1)) == 0)
return ptr;
/* Otherwise, get address of byte in the block that has that alignment. */
aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
/* Store a suitable indication of how to free the block,
so that free can find the true beginning of it. */
p = (union mhead *) aligned - 1;
p->mh_nbytes = aligned - ptr;
p->mh_alloc = ISMEMALIGN;
return aligned;
return internal_memalign (alignment, size, (char *)NULL, 0, 0);
}
#if !defined (HPUX)
/* This runs into trouble with getpagesize on HPUX, and Multimax machines.
Patching out seems cleaner than the ugly fix needed. */
#ifndef NO_VALLOC
PTR_T
valloc (size)
size_t size;
{
return memalign (getpagesize (), size);
return internal_valloc (size, (char *)NULL, 0, 0);
}
#endif /* !HPUX */
#endif
#ifndef NO_CALLOC
PTR_T
calloc (n, s)
size_t n, s;
{
size_t total;
char *result;
total = n * s;
result = malloc (total);
if (result)
zmemset (result, 0, total);
return result;
return internal_calloc (n, s, (char *)NULL, 0, 0);
}
void
cfree (p)
PTR_T p;
cfree (mem)
PTR_T mem;
{
free (p);
internal_cfree (mem, (char *)NULL, 0, 0);
}
#endif /* !NO_CALLOC */
#ifdef MALLOC_STATS
struct bucket_stats
malloc_bucket_stats (size)
int size;
{
struct bucket_stats v;
register union mhead *p;
v.nfree = 0;
if (size < 0 || size >= NBUCKETS)
{
v.blocksize = 0;
v.nused = v.nmal = v.nmorecore = v.nsplit = 0;
return v;
}
v.blocksize = 1 << (size + 3);
v.nused = _mstats.nmalloc[size];
v.nmal = _mstats.tmalloc[size];
v.nmorecore = _mstats.nmorecore[size];
v.nsplit = _mstats.nsplit[size];
for (p = nextf[size]; p; p = CHAIN (p))
v.nfree++;
return v;
}
/* Return a copy of _MSTATS, with two additional fields filled in:
BYTESFREE is the total number of bytes on free lists. BYTESUSED
is the total number of bytes in use. These two fields are fairly
expensive to compute, so we do it only when asked to. */
struct _malstats
malloc_stats ()
{
struct _malstats result;
struct bucket_stats v;
register int i;
result = _mstats;
result.bytesused = result.bytesfree = 0;
for (i = 0; i < NBUCKETS; i++)
{
v = malloc_bucket_stats (i);
result.bytesfree += v.nfree * v.blocksize;
result.bytesused += v.nused * v.blocksize;
}
return (result);
}
static void
_print_malloc_stats (s, fp)
char *s;
FILE *fp;
{
register int i;
int totused, totfree;
struct bucket_stats v;
fprintf (fp, "Memory allocation statistics: %s\n\tsize\tfree\tin use\ttotal\tmorecore\tsplit\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", v.blocksize, v.nfree, v.nused, v.nmal, v.nmorecore, v.nsplit);
totfree += v.nfree * v.blocksize;
totused += v.nused * v.blocksize;
}
fprintf (fp, "\nTotal bytes in use: %d, total bytes free: %d\n",
totused, totfree);
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",
_mstats.nsbrk, _mstats.tsbrk);
fprintf (fp, "Total blocks split: %d, total block coalesces: %d\n",
_mstats.tbsplit, _mstats.tbcoalesce);
}
void
print_malloc_stats (s)
char *s;
{
_print_malloc_stats (s, stderr);
}
#define TRACEROOT "/var/tmp/maltrace/trace."
extern char *inttostr ();
void
trace_malloc_stats (s)
char *s;
{
char ibuf[32], *ip;
char fname[64];
int p;
FILE *fp;
p = (int)getpid();
ip = inttostr(p, ibuf, sizeof(ibuf));
strcpy (fname, TRACEROOT);
strcat (fname, ip);
fp = fopen(fname, "w");
if (fp)
{
_print_malloc_stats (s, fp);
fflush(fp);
fclose(fp);
}
}
#endif /* MALLOC_STATS */
#endif

90
lib/malloc/mstats.h Normal file
View file

@ -0,0 +1,90 @@
/* mstats.h - definitions for malloc statistics */
/* 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 _MSTATS_H
#define _MSTATS_H
#include "imalloc.h"
#ifdef MALLOC_STATS
#ifndef NBUCKETS
# define NBUCKETS 30
#endif
/*
* 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.
* TBCOALESCE is the number of times two adjacent smaller blocks off the free
* list were combined to satisfy a larger request.
*/
struct _malstats {
int nmalloc[NBUCKETS];
int tmalloc[NBUCKETS];
int nmorecore[NBUCKETS];
int nmal;
int nfre;
int nrealloc;
int nrcopy;
int nrecurse;
int nsbrk;
bits32_t tsbrk;
bits32_t bytesused;
bits32_t bytesfree;
int tbsplit;
int nsplit[NBUCKETS];
int tbcoalesce;
};
/* 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. */
struct bucket_stats {
u_bits32_t blocksize;
int nfree;
int nused;
int nmal;
int nmorecore;
int nsplit;
};
extern struct bucket_stats malloc_bucket_stats ();
extern struct _malstats malloc_stats ();
extern void print_malloc_stats ();
extern void trace_malloc_stats ();
#endif /* MALLOC_STATS */
#endif /* _MSTATS_H */

File diff suppressed because it is too large Load diff

View file

@ -1,759 +0,0 @@
/* dynamic memory allocation for GNU. */
/* Copyright (C) 1985, 1987 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.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
/*
* @(#)nmalloc.c 1 (Caltech) 2/21/82
*
* U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
*
* Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
*
* This is a very fast storage allocator. It allocates blocks of a small
* number of different sizes, and keeps free lists of each size. Blocks
* that don't exactly fit are passed up to the next larger size. In this
* implementation, the available sizes are (2^n)-4 (or -16) bytes long.
* This is designed for use in a program that uses vast quantities of
* memory, but bombs when it runs out. To make it a little better, it
* warns the user when he starts to get near the end.
*
* June 84, ACT: modified rcheck code to check the range given to malloc,
* rather than the range determined by the 2-power used.
*
* Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
* No longer Emacs-specific; can serve as all-purpose malloc for GNU.
* You should call malloc_init to reinitialize after loading dumped Emacs.
* Call malloc_stats to get info on memory stats if MSTATS turned on.
* realloc knows how to return same block given, just changing its size,
* if the power of 2 is correct.
*/
/*
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
* smallest allocatable block is 8 bytes. The overhead information will
* go in the first int of the block, and the returned pointer will point
* to the second.
*
#ifdef MSTATS
* nmalloc[i] is the difference between the number of mallocs and frees
* for a given block size.
#endif
*/
/* 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
#if defined (emacs) || defined (HAVE_CONFIG_H)
# include <config.h>
#endif /* emacs */
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
/* Determine which kind of system this is. */
#if defined (SHELL)
# include "bashtypes.h"
#else
# include <sys/types.h>
#endif
#include <signal.h>
/* Define getpagesize () if the system does not. */
#ifndef HAVE_GETPAGESIZE
# include "getpagesize.h"
#endif
#if defined (HAVE_RESOURCE)
# include <sys/time.h>
# include <sys/resource.h>
#endif /* HAVE_RESOURCE */
/* Check for the needed symbols. If they aren't present, this
system's <sys/resource.h> isn't very useful to us. */
#if !defined (RLIMIT_DATA)
# undef HAVE_RESOURCE
#endif
#if __GNUC__ > 1
# define FASTCOPY(s, d, n) __builtin_memcpy (d, s, n)
#else /* !__GNUC__ */
# if !defined (HAVE_BCOPY)
# if !defined (HAVE_MEMMOVE)
# define FASTCOPY(s, d, n) memcpy (d, s, n)
# else
# define FASTCOPY(s, d, n) memmove (d, s, n)
# endif /* !HAVE_MEMMOVE */
# else /* HAVE_BCOPY */
# define FASTCOPY(s, d, n) bcopy (s, d, n)
# endif /* HAVE_BCOPY */
#endif /* !__GNUC__ */
#if !defined (NULL)
# define NULL 0
#endif
#define start_of_data() &etext
#define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
#define ISFREE ((char) 0x54) /* magic byte that implies free block */
/* this is for error checking only */
#define ISMEMALIGN ((char) 0xd6) /* Stored before the value returned by
memalign, with the rest of the word
being the distance to the true
beginning of the block. */
extern char etext;
#if !defined (SBRK_DECLARED)
extern char *sbrk ();
#endif /* !SBRK_DECLARED */
/* These two are for user programs to look at, when they are interested. */
unsigned int malloc_sbrk_used; /* amount of data space used now */
unsigned int malloc_sbrk_unused; /* amount more we can have */
/* start of data space; can be changed by calling init_malloc */
static char *data_space_start;
static void get_lim_data ();
#ifdef MSTATS
static int nmalloc[30];
static int nmal, nfre;
#endif /* MSTATS */
/* If range checking is not turned on, all we have is a flag indicating
whether memory is allocated, an index in nextf[], and a size field; to
realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
on whether the former can hold the exact size (given the value of
'index'). If range checking is on, we always need to know how much space
is allocated, so the 'size' field is never used. */
struct mhead {
char mh_alloc; /* ISALLOC or ISFREE */
char mh_index; /* index in nextf[] */
/* Remainder are valid only when block is allocated */
unsigned short mh_size; /* size, if < 0x10000 */
#ifdef RCHECK
unsigned int mh_nbytes; /* number of bytes allocated */
int mh_magic4; /* should be == MAGIC4 */
#endif /* RCHECK */
};
/* Access free-list pointer of a block.
It is stored at block + 4.
This is not a field in the mhead structure
because we want sizeof (struct mhead)
to describe the overhead for when the block is in use,
and we do not want the free-list pointer to count in that. */
#define CHAIN(a) \
(*(struct mhead **) (sizeof (char *) + (char *) (a)))
#ifdef RCHECK
# include <stdio.h>
# if !defined (botch)
# define botch(x) abort ()
# else
extern void botch();
# endif /* botch */
# if !defined (__STRING)
# if defined (__STDC__)
# define __STRING(x) #x
# else
# define __STRING(x) "x"
# endif
# endif
/* To implement range checking, we write magic values in at the beginning
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 4 bytes before the block's real space */
# define MAGIC4 0x55555555
# define ASSERT(p) if (!(p)) botch(__STRING(p)); else
# define EXTRA 4 /* 4 bytes extra for MAGIC1s */
#else /* !RCHECK */
# define ASSERT(p)
# define EXTRA 0
#endif /* RCHECK */
/* nextf[i] is free list of blocks of size 2**(i + 3) */
static struct mhead *nextf[30];
/* busy[i] is nonzero while allocation of block size i is in progress. */
static char busy[30];
/* Number of bytes of writable memory we can expect to be able to get */
static unsigned int lim_data;
/* Level number of warnings already issued.
0 -- no warnings issued.
1 -- 75% warning already issued.
2 -- 85% warning already issued.
*/
static int warnlevel;
/* Function to call to issue a warning;
0 means don't issue them. */
static void (*warnfunction) ();
/* nonzero once initial bunch of free blocks made */
static int gotpool;
char *_malloc_base;
static void getpool ();
/* Cause reinitialization based on job parameters;
also declare where the end of pure storage is. */
void
malloc_init (start, warnfun)
char *start;
void (*warnfun) ();
{
if (start)
data_space_start = start;
lim_data = 0;
warnlevel = 0;
warnfunction = warnfun;
}
/* Return the maximum size to which MEM can be realloc'd
without actually requiring copying. */
int
malloc_usable_size (mem)
char *mem;
{
int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
return blocksize - sizeof (struct mhead) - EXTRA;
}
static void
morecore (nu) /* ask system for more memory */
register int nu; /* size index to get more of */
{
register char *cp;
register int nblks;
register unsigned int siz;
/* Block all signals in case we are executed from a signal handler. */
#if defined (HAVE_BSD_SIGNALS)
int oldmask;
oldmask = sigsetmask (-1);
#else
# if defined (HAVE_POSIX_SIGNALS)
sigset_t set, oset;
sigfillset (&set);
sigemptyset (&oset);
sigprocmask (SIG_BLOCK, &set, &oset);
# endif /* HAVE_POSIX_SIGNALS */
#endif /* HAVE_BSD_SIGNALS */
if (!data_space_start)
{
data_space_start = start_of_data ();
}
if (lim_data == 0)
get_lim_data ();
/* On initial startup, get two blocks of each size up to 1k bytes */
if (!gotpool)
{ getpool (); getpool (); gotpool = 1; }
/* Find current end of memory and issue warning if getting near max */
cp = sbrk (0);
siz = cp - data_space_start;
malloc_sbrk_used = siz;
malloc_sbrk_unused = lim_data - siz;
if (warnfunction)
switch (warnlevel)
{
case 0:
if (siz > (lim_data / 4) * 3)
{
warnlevel++;
(*warnfunction) ("Warning: past 75% of memory limit");
}
break;
case 1:
if (siz > (lim_data / 20) * 17)
{
warnlevel++;
(*warnfunction) ("Warning: past 85% of memory limit");
}
break;
case 2:
if (siz > (lim_data / 20) * 19)
{
warnlevel++;
(*warnfunction) ("Warning: past 95% of memory limit");
}
break;
}
if ((int) cp & 0x3ff) /* land on 1K boundaries */
sbrk (1024 - ((int) cp & 0x3ff));
/* Take at least 2k, and figure out how many blocks of the desired size
we're about to get */
nblks = 1;
if ((siz = nu) < 8)
nblks = 1 << ((siz = 8) - nu);
if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
return; /* no more room! */
if ((int) cp & 7)
{ /* shouldn't happen, but just in case */
cp = (char *) (((int) cp + 8) & ~7);
nblks--;
}
/* save new header and link the nblks blocks together */
nextf[nu] = (struct mhead *) cp;
siz = 1 << (nu + 3);
while (1)
{
((struct mhead *) cp) -> mh_alloc = ISFREE;
((struct mhead *) cp) -> mh_index = nu;
if (--nblks <= 0) break;
CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
cp += siz;
}
CHAIN ((struct mhead *) cp) = 0;
#if defined (HAVE_BSD_SIGNALS)
sigsetmask (oldmask);
#else
# if defined (HAVE_POSIX_SIGNALS)
sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
# endif
#endif /* HAVE_BSD_SIGNALS */
}
static void
getpool ()
{
register int nu;
register char *cp = sbrk (0);
if ((int) cp & 0x3ff) /* land on 1K boundaries */
sbrk (1024 - ((int) cp & 0x3ff));
/* Record address of start of space allocated by malloc. */
if (_malloc_base == 0)
_malloc_base = cp;
/* Get 2k of storage */
cp = sbrk (04000);
if (cp == (char *) -1)
return;
/* Divide it into an initial 8-word block
plus one block of size 2**nu for nu = 3 ... 10. */
CHAIN (cp) = nextf[0];
nextf[0] = (struct mhead *) cp;
((struct mhead *) cp) -> mh_alloc = ISFREE;
((struct mhead *) cp) -> mh_index = 0;
cp += 8;
for (nu = 0; nu < 7; nu++)
{
CHAIN (cp) = nextf[nu];
nextf[nu] = (struct mhead *) cp;
((struct mhead *) cp) -> mh_alloc = ISFREE;
((struct mhead *) cp) -> mh_index = nu;
cp += 8 << nu;
}
}
#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 */
char *
malloc (n) /* get a block */
unsigned int n;
{
register struct mhead *p;
register unsigned int nbytes;
register int nunits = 0;
/* Figure out how many bytes are required, rounding up to the nearest
multiple of 4, then figure out which nextf[] area to use */
nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
{
register unsigned int shiftr = (nbytes - 1) >> 2;
while (shiftr >>= 1)
nunits++;
}
/* In case this is reentrant use of malloc from signal handler,
pick a block size that no other malloc level is currently
trying to allocate. That's the easiest harmless way not to
interfere with the other level of execution. */
while (busy[nunits]) nunits++;
busy[nunits] = 1;
/* If there are no blocks of the appropriate size, go get some */
/* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
if (nextf[nunits] == 0)
morecore (nunits);
/* Get one block off the list, and set the new list head */
if ((p = nextf[nunits]) == 0)
{
busy[nunits] = 0;
return 0;
}
nextf[nunits] = CHAIN (p);
busy[nunits] = 0;
/* Check for free block clobbered */
/* 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)
#ifdef RCHECK
botch ("block on free list clobbered");
#else /* not RCHECK */
abort ();
#endif /* not RCHECK */
/* Fill in the info, and if range checking, set up the magic numbers */
p -> mh_alloc = ISALLOC;
#ifdef RCHECK
p -> mh_nbytes = n;
p -> mh_magic4 = MAGIC4;
{
register char *m = (char *) (p + 1) + n;
*m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
}
#else /* not RCHECK */
p -> mh_size = n;
#endif /* not RCHECK */
#ifdef MEMSCRAMBLE
zmemset ((char *)(p + 1), 0xdf, n); /* scramble previous contents */
#endif
#ifdef MSTATS
nmalloc[nunits]++;
nmal++;
#endif /* MSTATS */
return (char *) (p + 1);
}
void
free (mem)
char *mem;
{
register struct mhead *p;
{
register char *ap = mem;
if (ap == 0)
return;
p = (struct mhead *) ap - 1;
if (p -> mh_alloc == ISMEMALIGN)
{
#ifdef RCHECK
ap -= p->mh_nbytes;
#else
ap -= p->mh_size; /* XXX */
#endif
p = (struct mhead *) ap - 1;
}
#ifndef RCHECK
if (p -> mh_alloc != ISALLOC)
abort ();
#else /* RCHECK */
if (p -> mh_alloc != ISALLOC)
{
if (p -> mh_alloc == ISFREE)
botch ("free: Called with already freed block argument\n");
else
botch ("free: Called with unallocated block argument\n");
}
ASSERT (p -> mh_magic4 == MAGIC4);
ap += p -> mh_nbytes;
ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
#endif /* RCHECK */
}
#ifdef MEMSCRAMBLE
{
register int n;
#ifdef RCHECK
n = p->mh_nbytes;
#else /* not RCHECK */
n = p->mh_size;
#endif /* not RCHECK */
zmemset (mem, 0xcf, n);
}
#endif
{
register int nunits = p -> mh_index;
ASSERT (nunits <= 29);
p -> mh_alloc = ISFREE;
/* Protect against signal handlers calling malloc. */
busy[nunits] = 1;
/* Put this block on the free list. */
CHAIN (p) = nextf[nunits];
nextf[nunits] = p;
busy[nunits] = 0;
#ifdef MSTATS
nmalloc[nunits]--;
nfre++;
#endif /* MSTATS */
}
}
char *
realloc (mem, n)
char *mem;
register unsigned int n;
{
register struct mhead *p;
register unsigned int tocopy;
register unsigned int nbytes;
register int nunits;
if ((p = (struct mhead *) mem) == 0)
return malloc (n);
p--;
nunits = p -> mh_index;
ASSERT (p -> mh_alloc == ISALLOC);
#ifdef RCHECK
ASSERT (p -> mh_magic4 == MAGIC4);
{
register char *m = mem + (tocopy = p -> mh_nbytes);
ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
}
#else /* not RCHECK */
if (p -> mh_index >= 13)
tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
else
tocopy = p -> mh_size;
#endif /* not RCHECK */
/* See if desired size rounds to same power of 2 as actual size. */
nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
/* If ok, use the same block, just marking its size as changed. */
if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
{
#ifdef RCHECK
register char *m = mem + tocopy;
*m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
p-> mh_nbytes = n;
m = mem + n;
*m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
#else /* not RCHECK */
p -> mh_size = n;
#endif /* not RCHECK */
return mem;
}
if (n < tocopy)
tocopy = n;
{
register char *new;
if ((new = malloc (n)) == 0)
return 0;
FASTCOPY (mem, new, tocopy);
free (mem);
return new;
}
}
char *
memalign (alignment, size)
unsigned int alignment, size;
{
register char *ptr;
register char *aligned;
register struct mhead *p;
ptr = malloc (size + alignment);
if (ptr == 0)
return 0;
/* If entire block has the desired alignment, just accept it. */
if (((int) ptr & (alignment - 1)) == 0)
return ptr;
/* Otherwise, get address of byte in the block that has that alignment. */
aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
/* Store a suitable indication of how to free the block,
so that free can find the true beginning of it. */
p = (struct mhead *) aligned - 1;
p -> mh_size = aligned - ptr;
p -> mh_alloc = ISMEMALIGN;
return aligned;
}
#if !defined (HPUX)
/* This runs into trouble with getpagesize on HPUX, and Multimax machines.
Patching out seems cleaner than the ugly fix needed. */
#if defined (__STDC__)
void *
#else
char *
#endif
valloc (size)
size_t size;
{
return memalign (getpagesize (), size);
}
#endif /* !HPUX */
#ifndef NO_CALLOC
char *
calloc (n, s)
size_t n, s;
{
size_t total;
char *result;
total = n * s;
result = malloc (total);
if (result)
zmemset (result, 0, total);
return result;
}
void
cfree (p)
char *p;
{
free (p);
}
#endif /* !NO_CALLOC */
#ifdef MSTATS
/* Return statistics describing allocation of blocks of size 2**n. */
struct mstats_value
{
int blocksize;
int nfree;
int nused;
};
struct mstats_value
malloc_stats (size)
int size;
{
struct mstats_value v;
register int i;
register struct mhead *p;
v.nfree = 0;
if (size < 0 || size >= 30)
{
v.blocksize = 0;
v.nused = 0;
return v;
}
v.blocksize = 1 << (size + 3);
v.nused = nmalloc[size];
for (p = nextf[size]; p; p = CHAIN (p))
v.nfree++;
return v;
}
#endif /* MSTATS */
/*
* This function returns the total number of bytes that the process
* will be allowed to allocate via the sbrk(2) system call. On
* BSD systems this is the total space allocatable to stack and
* data. On USG systems this is the data space only.
*/
#if !defined (HAVE_RESOURCE)
extern long ulimit ();
static void
get_lim_data ()
{
lim_data = ulimit (3, 0);
lim_data -= (long) data_space_start;
}
#else /* HAVE_RESOURCE */
static void
get_lim_data ()
{
struct rlimit XXrlimit;
getrlimit (RLIMIT_DATA, &XXrlimit);
#ifdef RLIM_INFINITY
lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
#else
lim_data = XXrlimit.rlim_cur; /* soft limit */
#endif
}
#endif /* HAVE_RESOURCE */

67
lib/malloc/shmalloc.h Normal file
View file

@ -0,0 +1,67 @@
/* Functions (currently) for use by the shell to do malloc debugging and
tracking. */
/* 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 of the License, 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 _SH_MALLOC_H
#define _SH_MALLOC_H
#ifndef __P
# if defined (__STDC__) || defined (__GNUC__) || defined (__cplusplus)
# define __P(protos) protos
# else
# define __P(protos) ()
# endif
#endif
/* Generic pointer type. */
#ifndef PTR_T
#if defined (__STDC__)
# define PTR_T void *
#else
# define PTR_T char *
#endif
#endif /* PTR_T */
extern PTR_T sh_malloc __P((size_t, const char *, int));
extern PTR_T sh_realloc __P((PTR_T, size_t, const char *, int));
extern void sh_free __P((PTR_T, const char *, int));
extern PTR_T sh_memalign __P((unsigned int, size_t, const char *, int));
extern PTR_T sh_calloc __P((size_t, size_t, const char *, int));
extern void sh_cfree __P((PTR_T, const char *, int));
extern PTR_T sh_valloc __P((size_t, const char *, int));
/* trace.c */
extern int malloc_set_trace __P((int));
extern void malloc_set_tracefp (); /* full prototype requires stdio.h */
/* table.c */
extern void mregister_dump_table __P((void));
extern void mregister_table_init __P((void));
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 *));
#endif

147
lib/malloc/stats.c Normal file
View file

@ -0,0 +1,147 @@
/* stats.c - malloc statistics */
/* 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. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "imalloc.h"
#ifdef MALLOC_STATS
#include <stdio.h>
#include "mstats.h"
struct _malstats _mstats;
struct bucket_stats
malloc_bucket_stats (size)
int size;
{
struct bucket_stats v;
v.nfree = 0;
if (size < 0 || size >= NBUCKETS)
{
v.blocksize = 0;
v.nused = v.nmal = v.nmorecore = v.nsplit = 0;
return v;
}
v.blocksize = 1 << (size + 3);
v.nused = _mstats.nmalloc[size];
v.nmal = _mstats.tmalloc[size];
v.nmorecore = _mstats.nmorecore[size];
v.nsplit = _mstats.nsplit[size];
v.nfree = malloc_free_blocks (size); /* call back to malloc.c */
return v;
}
/* Return a copy of _MSTATS, with two additional fields filled in:
BYTESFREE is the total number of bytes on free lists. BYTESUSED
is the total number of bytes in use. These two fields are fairly
expensive to compute, so we do it only when asked to. */
struct _malstats
malloc_stats ()
{
struct _malstats result;
struct bucket_stats v;
register int i;
result = _mstats;
result.bytesused = result.bytesfree = 0;
for (i = 0; i < NBUCKETS; i++)
{
v = malloc_bucket_stats (i);
result.bytesfree += v.nfree * v.blocksize;
result.bytesused += v.nused * v.blocksize;
}
return (result);
}
static void
_print_malloc_stats (s, fp)
char *s;
FILE *fp;
{
register int i;
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 : "");
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);
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, "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",
_mstats.nsbrk, _mstats.tsbrk);
fprintf (fp, "Total blocks split: %d, total block coalesces: %d\n",
_mstats.tbsplit, _mstats.tbcoalesce);
}
void
print_malloc_stats (s)
char *s;
{
_print_malloc_stats (s, stderr);
}
void
fprint_malloc_stats (s, fp)
char *s;
FILE *fp;
{
_print_malloc_stats (s, fp);
}
#define TRACEROOT "/var/tmp/maltrace/trace."
extern char *inttostr ();
void
trace_malloc_stats (s)
char *s;
{
char ibuf[32], *ip;
char fname[64];
long p;
FILE *fp;
p = getpid();
ip = inttostr(p, ibuf, sizeof(ibuf));
strcpy (fname, TRACEROOT);
strcat (fname, ip);
fp = fopen(fname, "w");
if (fp)
{
_print_malloc_stats (s, fp);
fflush(fp);
fclose(fp);
}
}
#endif /* MALLOC_STATS */

267
lib/malloc/table.c Normal file
View file

@ -0,0 +1,267 @@
/* table.c - bookkeeping functions for allocated memory */
/* 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"
#include "table.h"
extern int malloc_register;
#ifdef MALLOC_REGISTER
#define FIND_ALLOC 0x01 /* allocate new entry or find existing */
#define FIND_EXIST 0x02 /* find existing entry */
static int table_count = 0;
static mr_table_t mem_table[REG_TABLE_SIZE];
/*
* NOTE: taken from dmalloc (http://dmalloc.com) and modified.
*/
static unsigned int
mt_hash (key)
const PTR_T key;
{
unsigned int a, b, c, len;
unsigned long x;
/* set up the internal state */
a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
x = (unsigned long)key; /* truncation is OK */
b = x >> 8;
c = x >> 3; /* XXX - was >> 4 */
HASH_MIX(a, b, c);
return c;
}
#if 0
static unsigned int
which_bucket (mem)
PTR_T mem;
{
return (mt_hash ((unsigned char *)mem) % REG_TABLE_SIZE);
}
#else
#define which_bucket(mem) (mt_hash ((unsigned char *)(mem)) % REG_TABLE_SIZE);
#endif
static mr_table_t *
find_entry (mem, flags)
PTR_T mem;
int flags;
{
unsigned int bucket;
register mr_table_t *tp;
mr_table_t *endp, *lastp;
bucket = which_bucket (mem); /* get initial hash */
tp = endp = mem_table + bucket;
lastp = mem_table + REG_TABLE_SIZE;
while (1)
{
if (tp->mem == mem)
return (tp);
if (tp->mem == 0 && (flags & FIND_ALLOC))
{
table_count++;
return (tp);
}
tp++;
if (tp == lastp) /* wrap around */
tp = mem_table;
if (tp == endp && (flags & FIND_EXIST))
return ((mr_table_t *)NULL);
if (tp == endp && (flags & FIND_ALLOC))
break;
}
/* oops. table is full. replace an existing free entry. */
do
{
if (tp->flags & MT_FREE)
{
memset(tp, 0, sizeof (mr_table_t));
return (tp);
}
tp++;
}
while (tp != endp);
/* wow. entirely full. return NULL. */
return ((mr_table_t *)NULL);
}
mr_table_t *
mr_table_entry (mem)
PTR_T mem;
{
return (find_entry (mem, FIND_EXIST));
}
void
mregister_describe_mem (mem, fp)
PTR_T mem;
FILE *fp;
{
mr_table_t *entry;
entry = find_entry (mem, FIND_EXIST);
if (entry == 0)
return;
fprintf (fp, "malloc: %p: %s: last %s from %s:%d\n",
mem,
(entry->flags & MT_ALLOC) ? "allocated" : "free",
(entry->flags & MT_ALLOC) ? "allocated" : "freed",
entry->file ? entry->file : "unknown",
entry->line);
}
void
mregister_alloc (tag, mem, size, file, line)
const char *tag;
PTR_T mem;
size_t size;
const char *file;
int line;
{
mr_table_t *tentry;
tentry = find_entry (mem, FIND_ALLOC);
if (tentry == 0)
{
/* oops. table is full. punt. */
fprintf (stderr, "register_alloc: alloc table is full?\n");
return;
}
if (tentry->flags & MT_ALLOC)
{
/* oops. bad bookkeeping. ignore for now */
fprintf (stderr, "register_alloc: %p already in table as allocated?\n", mem);
}
tentry->mem = mem;
tentry->size = size;
tentry->func = tag;
tentry->flags = MT_ALLOC;
tentry->file = file;
tentry->line = line;
tentry->nalloc++;
}
void
mregister_free (mem, size, file, line)
PTR_T mem;
int size;
const char *file;
int line;
{
mr_table_t *tentry;
tentry = find_entry (mem, FIND_EXIST);
if (tentry == 0)
{
/* oops. not found. */
fprintf (stderr, "register_free: %p not in allocation table?\n", mem);
return;
}
if (tentry->flags & MT_FREE)
{
/* oops. bad bookkeeping. ignore for now */
fprintf (stderr, "register_free: %p already in table as free?\n", mem);
}
tentry->flags = MT_FREE;
tentry->func = "free";
tentry->file = file;
tentry->line = line;
tentry->nfree++;
}
/* If we ever add more flags, this will require changes. */
static char *
_entry_flags(x)
int x;
{
if (x & MT_FREE)
return "free";
else if (x & MT_ALLOC)
return "allocated";
else
return "undetermined?";
}
static void
_register_dump_table(fp)
FILE *fp;
{
register int i;
mr_table_t entry;
for (i = 0; i < REG_TABLE_SIZE; i++)
{
entry = mem_table[i];
if (entry.mem)
fprintf (fp, "[%d] %p:%d:%s:%s:%s:%d:%d:%d\n", i,
entry.mem, entry.size,
_entry_flags(entry.flags),
entry.func ? entry.func : "unknown",
entry.file ? entry.file : "unknown",
entry.line,
entry.nalloc, entry.nfree);
}
}
void
mregister_dump_table()
{
_register_dump_table (stderr);
}
void
mregister_table_init ()
{
memset (mem_table, 0, sizeof(mr_table_t) * REG_TABLE_SIZE);
table_count = 0;
}
#endif /* MALLOC_REGISTER */
int
malloc_set_register(n)
int n;
{
int old;
old = malloc_register;
malloc_register = n;
return old;
}

103
lib/malloc/table.h Normal file
View file

@ -0,0 +1,103 @@
/* table.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 _MTABLE_H
#define _MTABLE_H
#include "imalloc.h"
#ifdef MALLOC_REGISTER
/* values for flags byte. */
#define MT_ALLOC 0x01
#define MT_FREE 0x02
/*
* Memory table entry.
*
* MEM is the address of the allocated pointer.
* SIZE is the requested allocation size.
* FLAGS includes either MT_ALLOC (MEM is allocated) or MT_FREE (MEM is
* not allocated). Other flags later.
* FUNC is set to the name of the function doing the allocation (from the
* `tag' argument to register_alloc().
* FILE and LINE are the filename and line number of the last allocation
* and free (depending on STATUS) of MEM.
* NALLOC and NFREE are incremented on each allocation that returns MEM or
* each free of MEM, respectively (way to keep track of memory reuse
* and how well the free lists are working).
*
*/
typedef struct mr_table {
PTR_T mem;
size_t size;
char flags;
const char *func;
const char *file;
int line;
int nalloc, nfree;
} mr_table_t;
#define REG_TABLE_SIZE 8192
extern mr_table_t *mr_table_entry ();
extern void mregister_alloc ();
extern void mregister_free ();
extern void mregister_describe_mem ();
extern void mregister_dump_table ();
extern void mregister_table_init ();
/* NOTE: HASH_MIX taken from dmalloc (http://dmalloc.com) */
/*
* void HASH_MIX
*
* DESCRIPTION:
*
* Mix 3 32-bit values reversibly. For every delta with one or two
* bits set, and the deltas of all three high bits or all three low
* bits, whether the original value of a,b,c is almost all zero or is
* uniformly distributed.
*
* If HASH_MIX() is run forward or backward, at least 32 bits in a,b,c
* have at least 1/4 probability of changing. If mix() is run
* forward, every bit of c will change between 1/3 and 2/3 of the
* time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
*
* HASH_MIX() takes 36 machine instructions, but only 18 cycles on a
* superscalar machine (like a Pentium or a Sparc). No faster mixer
* seems to work, that's the result of my brute-force search. There
* were about 2^68 hashes to choose from. I only tested about a
* billion of those.
*/
#define HASH_MIX(a, b, c) \
do { \
a -= b; a -= c; a ^= (c >> 13); \
b -= c; b -= a; b ^= (a << 8); \
c -= a; c -= b; c ^= (b >> 13); \
a -= b; a -= c; a ^= (c >> 12); \
b -= c; b -= a; b ^= (a << 16); \
c -= a; c -= b; c ^= (b >> 5); \
a -= b; a -= c; a ^= (c >> 3); \
b -= c; b -= a; b ^= (a << 10); \
c -= a; c -= b; c ^= (b >> 15); \
} while(0)
#endif /* MALLOC_REGISTER */
#endif /* _MTABLE_H */

93
lib/malloc/trace.c Normal file
View file

@ -0,0 +1,93 @@
/* trace.c - tracing 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"
extern int malloc_trace;
static int _mtrace_verbose = 0;
#ifdef MALLOC_TRACE
FILE *_mtrace_fp = NULL;
void
mtrace_alloc (tag, mem, size, file, line)
const char *tag;
PTR_T mem;
size_t size;
const char *file;
int line;
{
if (_mtrace_fp == NULL)
_mtrace_fp = stderr;
if (_mtrace_verbose)
fprintf (_mtrace_fp, "alloc: %s: %p (%d bytes) from '%s:%d'\n",
tag, mem, size, file ? file : "unknown", line);
else
fprintf (_mtrace_fp, "alloc:%p:%d:%s:%d\n",
mem, size, file ? file : "unknown", line);
}
void
mtrace_free (mem, size, file, line)
PTR_T mem;
int size;
const char *file;
int line;
{
if (_mtrace_fp == NULL)
_mtrace_fp = stderr;
if (_mtrace_verbose)
fprintf (_mtrace_fp, "free: %p (%d bytes) from '%s:%d'\n",
mem, size, file ? file : "unknown", line);
else
fprintf (_mtrace_fp, "free:%p:%d:%s:%d\n",
mem, size, file ? file : "unknown", line);
}
#endif /* MALLOC_TRACE */
int
malloc_set_trace(n)
int n;
{
int old;
old = malloc_trace;
malloc_trace = n;
_mtrace_verbose = (n > 1);
return old;
}
void
malloc_set_tracefp(fp)
FILE *fp;
{
#ifdef MALLOC_TRACE
_mtrace_fp = fp ? fp : stderr;
#endif
}

43
lib/malloc/xleaktrace Executable file
View file

@ -0,0 +1,43 @@
#! /usr/bin/awk -f
#
# xleaktrace - print unfreed memory using input generated by compact malloc
# tracing (malloc_set_trace(1))
#
# NOTE: we ignore `realloc' tags because they're just extra information
#
# Chet Ramey
# chet@po.cwru.edu
#
BEGIN {
FS=":";
}
$1 == "alloc" {
alloc[$2] = 1;
size[$2] = $3;
file[$2] = $4;
line[$2] = $5;
# printf "allocated: %s %d %d %s %d\n", $2, alloc[$2], size[$2], file[$2], line[$2];
}
$1 == "free" {
if ($2 in alloc) {
alloc[$2] = 0;
# printf "freed: %s %d\n", $2, alloc[$2];
} else
printf "freeing unallocated pointer: %s\n", $2;
}
END {
printf "unfreed memory\n";
for (ptr in alloc) {
if (alloc[ptr] == 1) {
printf "%s (%d) from %s:%d\n", ptr, size[ptr], file[ptr], line[ptr];
}
}
}

View file

@ -31,7 +31,16 @@
# include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
static void memory_error_and_abort ();
/* Generic pointer type. */
#ifndef PTR_T
#if defined (__STDC__)
# define PTR_T void *
#else
# define PTR_T char *
#endif
#endif /* PTR_T */
/* **************************************************************** */
/* */
@ -39,35 +48,6 @@ static void memory_error_and_abort ();
/* */
/* **************************************************************** */
/* Return a pointer to free()able block of memory large enough
to hold BYTES number of bytes. If the memory cannot be allocated,
print an error message and abort. */
char *
xmalloc (bytes)
int bytes;
{
char *temp;
temp = (char *)malloc (bytes);
if (temp == 0)
memory_error_and_abort ("xmalloc");
return (temp);
}
char *
xrealloc (pointer, bytes)
char *pointer;
int bytes;
{
char *temp;
temp = pointer ? (char *)realloc (pointer, bytes) : (char *)malloc (bytes);
if (temp == 0)
memory_error_and_abort ("xrealloc");
return (temp);
}
static void
memory_error_and_abort (fname)
char *fname;
@ -76,11 +56,38 @@ memory_error_and_abort (fname)
exit (2);
}
/* Use this as the function to call when adding unwind protects so we
don't need to know what free() returns. */
/* Return a pointer to free()able block of memory large enough
to hold BYTES number of bytes. If the memory cannot be allocated,
print an error message and abort. */
PTR_T
xmalloc (bytes)
size_t bytes;
{
PTR_T temp;
temp = malloc (bytes);
if (temp == 0)
memory_error_and_abort ("xmalloc");
return (temp);
}
PTR_T
xrealloc (pointer, bytes)
PTR_T pointer;
size_t bytes;
{
PTR_T temp;
temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
if (temp == 0)
memory_error_and_abort ("xrealloc");
return (temp);
}
void
xfree (string)
char *string;
PTR_T string;
{
if (string)
free (string);