-/* $Header: malloc.c,v 1.0.1.1 88/01/24 03:53:23 root Exp $
+/* $Header: malloc.c,v 3.0.1.2 89/11/11 04:36:37 lwall Locked $
*
* $Log: malloc.c,v $
- * Revision 1.0.1.1 88/01/24 03:53:23 root
- * patch 2: made depend on perl.h.
+ * Revision 3.0.1.2 89/11/11 04:36:37 lwall
+ * patch2: malloc pointer corruption check made more portable
*
- * Revision 1.0 87/12/18 13:05:35 root
- * Initial revision
+ * Revision 3.0.1.1 89/10/26 23:15:05 lwall
+ * patch1: some declarations were missing from malloc.c
+ * patch1: sparc machines had alignment problems in malloc.c
+ *
+ * Revision 3.0 89/10/18 15:20:39 lwall
+ * 3.0 baseline
*
*/
#ifndef lint
static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
-#endif
-#include <stdio.h>
+#ifdef DEBUGGING
#define RCHECK
+#endif
/*
* malloc.c (Caltech) 2/21/82
* Chris Kingsley, kingsley@cit-20.
*/
#include "EXTERN.h"
-#include "handy.h"
-#include "search.h"
#include "perl.h"
+static findbucket(), morecore();
+
/* I don't much care whether these are defined in sys/types.h--LAW */
#define u_char unsigned char
#define u_int unsigned int
#define u_short unsigned short
-#define NULL 0
-
/*
* The overhead on a block is at least 4 bytes. When free, this space
* contains a pointer to the next free block, and the bottom two bits must
*/
union overhead {
union overhead *ov_next; /* when free */
+#if defined (mips) || defined (sparc)
+ double strut; /* alignment problems */
+#endif
struct {
u_char ovu_magic; /* magic number */
u_char ovu_index; /* bucket # */
};
#define MAGIC 0xff /* magic # on accounting info */
+#define OLDMAGIC 0x7f /* same after a free() */
#define RMAGIC 0x55555555 /* magic # on range info */
#ifdef RCHECK
#define RSLOP sizeof (u_int)
if ((p = (union overhead *)nextf[bucket]) == NULL)
return (NULL);
/* remove from linked list */
- if (*((int*)p) > 0x10000000)
+#ifdef RCHECK
+ if (*((int*)p) & (sizeof(union overhead) - 1))
+#ifndef I286
fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
- nextf[bucket] = nextf[bucket]->ov_next;
+#else
+ fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",*((int*)p),p);
+#endif
+#endif
+ nextf[bucket] = p->ov_next;
p->ov_magic = MAGIC;
p->ov_index= bucket;
#ifdef MSTATS
*/
static
morecore(bucket)
- register bucket;
+ register int bucket;
{
register union overhead *op;
register int rnu; /* 2^rnu bytes will be requested */
* make getpageize call?
*/
op = (union overhead *)sbrk(0);
+#ifndef I286
if ((int)op & 0x3ff)
- sbrk(1024 - ((int)op & 0x3ff));
+ (void)sbrk(1024 - ((int)op & 0x3ff));
+#else
+ /* The sbrk(0) call on the I286 always returns the next segment */
+#endif
+
+#ifndef I286
/* take 2k unless the block is bigger than that */
rnu = (bucket <= 8) ? 11 : bucket + 3;
+#else
+ /* take 16k unless the block is bigger than that
+ (80286s like large segments!) */
+ rnu = (bucket <= 11) ? 14 : bucket + 3;
+#endif
nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
if (rnu < bucket)
rnu = bucket;
* Round up to minimum allocation size boundary
* and deduct from block count to reflect.
*/
+#ifndef I286
if ((int)op & 7) {
op = (union overhead *)(((int)op + 8) &~ 7);
nblks--;
}
+#else
+ /* Again, this should always be ok on an 80286 */
+#endif
/*
* Add new memory allocated to that on
* free list for this hash bucket.
#ifdef debug
ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
#else
- if (op->ov_magic != MAGIC)
+ if (op->ov_magic != MAGIC) {
+ warn("%s free() ignored",
+ op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
return; /* sanity */
+ }
+ op->ov_magic = OLDMAGIC;
#endif
#ifdef RCHECK
ASSERT(op->ov_rmagic == RMAGIC);
* back. We have to search all the free lists for the block in order
* to determine its bucket: 1st we make one pass thru the lists
* checking only the first block in each; if that fails we search
- * ``realloc_srchlen'' blocks in each list for a match (the variable
+ * ``reall_srchlen'' blocks in each list for a match (the variable
* is extern so the caller can modify it). If that fails we just copy
* however many bytes was given to realloc() and hope it's not huge.
*/
-int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
+int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
char *
realloc(cp, nbytes)
* Search for the old block of memory on the
* free list. First, check the most common
* case (last element free'd), then (this failing)
- * the last ``realloc_srchlen'' items free'd.
+ * the last ``reall_srchlen'' items free'd.
* If all lookups fail, then assume the size of
* the memory block being realloc'd is the
* smallest possible.
*/
if ((i = findbucket(op, 1)) < 0 &&
- (i = findbucket(op, realloc_srchlen)) < 0)
+ (i = findbucket(op, reall_srchlen)) < 0)
i = 0;
}
onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
/* avoid the copy if same size block */
if (was_alloced &&
- nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
+ nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP) {
+#ifdef RCHECK
+ /*
+ * Record new allocated size of block and
+ * bound space with magic numbers.
+ */
+ if (op->ov_index <= 13) {
+ /*
+ * Convert amount of memory requested into
+ * closest block size stored in hash buckets
+ * which satisfies request. Account for
+ * space used per block for accounting.
+ */
+ nbytes += sizeof (union overhead) + RSLOP;
+ nbytes = (nbytes + 3) &~ 3;
+ op->ov_size = nbytes - 1;
+ *((u_int *)((caddr_t)op + nbytes - RSLOP)) = RMAGIC;
+ }
+#endif
return(cp);
+ }
if ((res = malloc(nbytes)) == NULL)
return (NULL);
if (cp != res) /* common optimization */
- bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
+ (void)bcopy(cp, res, (int)((nbytes < onb) ? nbytes : onb));
if (was_alloced)
free(cp);
return (res);
totused, totfree);
}
#endif
+#endif /* lint */