1994-01-05 02:53:40 +00:00
|
|
|
/****************************************************************
|
1997-04-13 01:13:52 +00:00
|
|
|
Copyright 1990, 1994 by AT&T, Lucent Technologies and Bellcore.
|
1994-01-05 02:53:40 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software
|
|
|
|
and its documentation for any purpose and without fee is hereby
|
|
|
|
granted, provided that the above copyright notice appear in all
|
|
|
|
copies and that both that the copyright notice and this
|
|
|
|
permission notice and warranty disclaimer appear in supporting
|
1997-04-13 01:13:52 +00:00
|
|
|
documentation, and that the names of AT&T, Bell Laboratories,
|
|
|
|
Lucent or Bellcore or any of their entities not be used in
|
|
|
|
advertising or publicity pertaining to distribution of the
|
|
|
|
software without specific, written prior permission.
|
1994-01-05 02:53:40 +00:00
|
|
|
|
1997-04-13 01:13:52 +00:00
|
|
|
AT&T, Lucent and Bellcore disclaim all warranties with regard to
|
|
|
|
this software, including all implied warranties of
|
|
|
|
merchantability and fitness. In no event shall AT&T, Lucent or
|
|
|
|
Bellcore be liable for any special, indirect or consequential
|
|
|
|
damages or any damages whatsoever resulting from loss of use,
|
|
|
|
data or profits, whether in an action of contract, negligence or
|
|
|
|
other tortious action, arising out of or in connection with the
|
|
|
|
use or performance of this software.
|
1994-01-05 02:53:40 +00:00
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#ifndef CRAY
|
|
|
|
#define STACKMIN 512
|
|
|
|
#define MINBLK (2*sizeof(struct mem) + 16)
|
1995-09-28 20:36:16 +00:00
|
|
|
#define F _malloc_free_
|
1994-01-05 02:53:40 +00:00
|
|
|
#define SBGULP 8192
|
1995-09-28 20:36:16 +00:00
|
|
|
#include "string.h" /* for memcpy */
|
1994-01-05 02:53:40 +00:00
|
|
|
|
1995-09-28 20:36:16 +00:00
|
|
|
#ifdef KR_headers
|
|
|
|
#define Char char
|
|
|
|
#define Unsigned unsigned
|
|
|
|
#define Int /*int*/
|
|
|
|
#else
|
|
|
|
#define Char void
|
|
|
|
#define Unsigned size_t
|
|
|
|
#define Int int
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct mem {
|
1994-01-05 02:53:40 +00:00
|
|
|
struct mem *next;
|
1995-09-28 20:36:16 +00:00
|
|
|
Unsigned len;
|
|
|
|
} mem;
|
1994-01-05 02:53:40 +00:00
|
|
|
|
1995-09-28 20:36:16 +00:00
|
|
|
mem *F;
|
1994-01-05 02:53:40 +00:00
|
|
|
|
1995-09-28 20:36:16 +00:00
|
|
|
Char *
|
|
|
|
#ifdef KR_headers
|
1994-01-05 02:53:40 +00:00
|
|
|
malloc(size)
|
1995-09-28 20:36:16 +00:00
|
|
|
register Unsigned size;
|
|
|
|
#else
|
|
|
|
malloc(register Unsigned size)
|
|
|
|
#endif
|
1994-01-05 02:53:40 +00:00
|
|
|
{
|
1995-09-28 20:36:16 +00:00
|
|
|
register mem *p, *q, *r, *s;
|
1994-01-05 02:53:40 +00:00
|
|
|
unsigned register k, m;
|
1995-09-28 20:36:16 +00:00
|
|
|
extern Char *sbrk(Int);
|
1994-01-05 02:53:40 +00:00
|
|
|
char *top, *top1;
|
|
|
|
|
|
|
|
size = (size+7) & ~7;
|
1995-09-28 20:36:16 +00:00
|
|
|
r = (mem *) &F;
|
1994-01-05 02:53:40 +00:00
|
|
|
for (p = F, q = 0; p; r = p, p = p->next) {
|
1995-09-28 20:36:16 +00:00
|
|
|
if ((k = p->len) >= size && (!q || m > k)) {
|
|
|
|
m = k;
|
|
|
|
q = p;
|
|
|
|
s = r;
|
|
|
|
}
|
1994-01-05 02:53:40 +00:00
|
|
|
}
|
|
|
|
if (q) {
|
|
|
|
if (q->len - size >= MINBLK) { /* split block */
|
1995-09-28 20:36:16 +00:00
|
|
|
p = (mem *) (((char *) (q+1)) + size);
|
1994-01-05 02:53:40 +00:00
|
|
|
p->next = q->next;
|
1995-09-28 20:36:16 +00:00
|
|
|
p->len = q->len - size - sizeof(mem);
|
1994-01-05 02:53:40 +00:00
|
|
|
s->next = p;
|
|
|
|
q->len = size;
|
|
|
|
}
|
1995-09-28 20:36:16 +00:00
|
|
|
else
|
|
|
|
s->next = q->next;
|
1994-01-05 02:53:40 +00:00
|
|
|
}
|
|
|
|
else {
|
1995-09-28 20:36:16 +00:00
|
|
|
top = (Char *)(((long)sbrk(0) + 7) & ~7);
|
|
|
|
if (F && (char *)(F+1) + F->len == top) {
|
|
|
|
q = F;
|
|
|
|
F = F->next;
|
1994-01-05 02:53:40 +00:00
|
|
|
}
|
1995-09-28 20:36:16 +00:00
|
|
|
else
|
|
|
|
q = (mem *) top;
|
|
|
|
top1 = (char *)(q+1) + size;
|
|
|
|
if (sbrk((int)(top1-top+SBGULP)) == (Char *) -1)
|
|
|
|
return 0;
|
|
|
|
r = (mem *)top1;
|
|
|
|
r->len = SBGULP - sizeof(mem);
|
|
|
|
r->next = F;
|
|
|
|
F = r;
|
1994-01-05 02:53:40 +00:00
|
|
|
q->len = size;
|
|
|
|
}
|
1995-09-28 20:36:16 +00:00
|
|
|
return (Char *) (q+1);
|
1994-01-05 02:53:40 +00:00
|
|
|
}
|
|
|
|
|
1995-09-28 20:36:16 +00:00
|
|
|
void
|
|
|
|
#ifdef KR_headers
|
1994-01-05 02:53:40 +00:00
|
|
|
free(f)
|
1995-09-28 20:36:16 +00:00
|
|
|
Char *f;
|
|
|
|
#else
|
|
|
|
free(Char *f)
|
|
|
|
#endif
|
1994-01-05 02:53:40 +00:00
|
|
|
{
|
1995-09-28 20:36:16 +00:00
|
|
|
mem *p, *q, *r;
|
1994-01-05 02:53:40 +00:00
|
|
|
char *pn, *qn;
|
|
|
|
|
|
|
|
if (!f) return;
|
1995-09-28 20:36:16 +00:00
|
|
|
q = (mem *) ((char *)f - sizeof(mem));
|
|
|
|
qn = (char *)f + q->len;
|
|
|
|
for (p = F, r = (mem *) &F; ; r = p, p = p->next) {
|
|
|
|
if (qn == (Char *) p) {
|
|
|
|
q->len += p->len + sizeof(mem);
|
1994-01-05 02:53:40 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
pn = p ? ((char *) (p+1)) + p->len : 0;
|
1995-09-28 20:36:16 +00:00
|
|
|
if (pn == (Char *) q) {
|
|
|
|
p->len += sizeof(mem) + q->len;
|
1994-01-05 02:53:40 +00:00
|
|
|
q->len = 0;
|
|
|
|
q->next = p;
|
|
|
|
r->next = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pn < (char *) q) {
|
|
|
|
r->next = q;
|
|
|
|
q->next = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-09-28 20:36:16 +00:00
|
|
|
Char *
|
|
|
|
#ifdef KR_headers
|
1994-01-05 02:53:40 +00:00
|
|
|
realloc(f, size)
|
1995-09-28 20:36:16 +00:00
|
|
|
Char *f;
|
|
|
|
Unsigned size;
|
|
|
|
#else
|
|
|
|
realloc(Char *f, Unsigned size)
|
|
|
|
#endif
|
1994-01-05 02:53:40 +00:00
|
|
|
{
|
1995-09-28 20:36:16 +00:00
|
|
|
mem *p;
|
|
|
|
Char *q, *f1;
|
|
|
|
Unsigned s1;
|
1994-01-05 02:53:40 +00:00
|
|
|
|
|
|
|
if (!f) return malloc(size);
|
1995-09-28 20:36:16 +00:00
|
|
|
p = (mem *) ((char *)f - sizeof(mem));
|
1994-01-05 02:53:40 +00:00
|
|
|
s1 = p->len;
|
|
|
|
free(f);
|
1995-09-28 20:36:16 +00:00
|
|
|
if (s1 > size)
|
|
|
|
s1 = size + 7 & ~7;
|
1994-01-05 02:53:40 +00:00
|
|
|
if (!p->len) {
|
1995-09-28 20:36:16 +00:00
|
|
|
f1 = (Char *)(p->next + 1);
|
1994-01-05 02:53:40 +00:00
|
|
|
memcpy(f1, f, s1);
|
|
|
|
f = f1;
|
|
|
|
}
|
|
|
|
q = malloc(size);
|
|
|
|
if (q && q != f)
|
|
|
|
memcpy(q, f, s1);
|
|
|
|
return q;
|
|
|
|
}
|
1999-02-03 17:23:49 +00:00
|
|
|
|
|
|
|
/* The following (calloc) should really be in a separate file, */
|
|
|
|
/* but defining it here sometimes avoids confusion on systems */
|
|
|
|
/* that do not provide calloc in its own file. */
|
|
|
|
|
|
|
|
Char *
|
|
|
|
#ifdef KR_headers
|
|
|
|
calloc(n, m) Unsigned m, n;
|
|
|
|
#else
|
|
|
|
calloc(Unsigned n, Unsigned m)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
Char *rv = malloc(n *= m);
|
|
|
|
if (n && rv)
|
|
|
|
memset(rv, 0, n);
|
|
|
|
return rv;
|
|
|
|
}
|
1994-01-05 02:53:40 +00:00
|
|
|
#endif
|