-/* $Header: malloc.c,v 2.0 88/06/05 00:09:16 root Exp $
+/* $RCSfile: malloc.c,v $$Revision: 4.0.1.2 $$Date: 91/06/07 11:20:45 $
*
* $Log: malloc.c,v $
- * Revision 2.0 88/06/05 00:09:16 root
- * Baseline version 2.0.
+ * Revision 4.0.1.2 91/06/07 11:20:45 lwall
+ * patch4: many, many itty-bitty portability fixes
+ *
+ * Revision 4.0.1.1 91/04/11 17:48:31 lwall
+ * patch1: Configure now figures out malloc ptr type
+ *
+ * Revision 4.0 91/03/20 01:28:52 lwall
+ * 4.0 baseline.
*
*/
#ifndef lint
static char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83";
-#endif
+#ifdef DEBUGGING
#define RCHECK
+#endif
/*
* malloc.c (Caltech) 2/21/82
* Chris Kingsley, kingsley@cit-20.
#include "EXTERN.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
*/
union overhead {
union overhead *ov_next; /* when free */
+#if ALIGNBYTES > 4
+ double strut; /* alignment problems */
+#endif
struct {
u_char ovu_magic; /* magic number */
u_char ovu_index; /* bucket # */
#define ASSERT(p)
#endif
-char *
+MALLOCPTRTYPE *
malloc(nbytes)
register unsigned nbytes;
{
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
p->ov_rmagic = RMAGIC;
*((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
#endif
- return ((char *)(p + 1));
+ return ((MALLOCPTRTYPE *)(p + 1));
}
/*
*/
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.
}
}
-free(cp)
- char *cp;
+void
+free(mp)
+ MALLOCPTRTYPE *mp;
{
register int size;
register union overhead *op;
+ char *cp = (char*)mp;
if (cp == NULL)
return;
ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
#else
if (op->ov_magic != MAGIC) {
- fprintf(stderr,"%s free() ignored\n",
+ warn("%s free() ignored",
op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
return; /* sanity */
}
*/
int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
-char *
-realloc(cp, nbytes)
- char *cp;
+MALLOCPTRTYPE *
+realloc(mp, nbytes)
+ MALLOCPTRTYPE *mp;
unsigned nbytes;
{
register u_int onb;
char *res;
register int i;
int was_alloced = 0;
+ char *cp = (char*)mp;
if (cp == NULL)
return (malloc(nbytes));
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)
- return(cp);
- if ((res = malloc(nbytes)) == NULL)
+ 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((MALLOCPTRTYPE*)cp);
+ }
+ if ((res = (char*)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);
+ return ((MALLOCPTRTYPE*)res);
}
/*
totused, totfree);
}
#endif
+#endif /* lint */