1
/****************************************************************
2
Copyright 1990, 1994 by AT&T, Lucent Technologies and Bellcore.
4
Permission to use, copy, modify, and distribute this software
5
and its documentation for any purpose and without fee is hereby
6
granted, provided that the above copyright notice appear in all
7
copies and that both that the copyright notice and this
8
permission notice and warranty disclaimer appear in supporting
9
documentation, and that the names of AT&T, Bell Laboratories,
10
Lucent or Bellcore or any of their entities not be used in
11
advertising or publicity pertaining to distribution of the
12
software without specific, written prior permission.
14
AT&T, Lucent and Bellcore disclaim all warranties with regard to
15
this software, including all implied warranties of
16
merchantability and fitness. In no event shall AT&T, Lucent or
17
Bellcore be liable for any special, indirect or consequential
18
damages or any damages whatsoever resulting from loss of use,
19
data or profits, whether in an action of contract, negligence or
20
other tortious action, arising out of or in connection with the
21
use or performance of this software.
22
****************************************************************/
26
#define MINBLK (2*sizeof(struct mem) + 16)
27
#define F _malloc_free_
29
#include "string.h" /* for memcpy */
33
#define Unsigned unsigned
37
#define Unsigned size_t
51
register Unsigned size;
53
malloc(register Unsigned size)
56
register mem *p, *q, *r, *s;
57
unsigned register k, m;
58
extern Char *sbrk(Int);
63
for (p = F, q = 0; p; r = p, p = p->next) {
64
if ((k = p->len) >= size && (!q || m > k)) {
71
if (q->len - size >= MINBLK) { /* split block */
72
p = (mem *) (((char *) (q+1)) + size);
74
p->len = q->len - size - sizeof(mem);
82
top = (Char *)(((long)sbrk(0) + 7) & ~7);
83
if (F && (char *)(F+1) + F->len == top) {
89
top1 = (char *)(q+1) + size;
90
if (sbrk((int)(top1-top+SBGULP)) == (Char *) -1)
93
r->len = SBGULP - sizeof(mem);
98
return (Char *) (q+1);
113
q = (mem *) ((char *)f - sizeof(mem));
114
qn = (char *)f + q->len;
115
for (p = F, r = (mem *) &F; ; r = p, p = p->next) {
116
if (qn == (Char *) p) {
117
q->len += p->len + sizeof(mem);
120
pn = p ? ((char *) (p+1)) + p->len : 0;
121
if (pn == (Char *) q) {
122
p->len += sizeof(mem) + q->len;
128
if (pn < (char *) q) {
142
realloc(Char *f, Unsigned size)
149
if (!f) return malloc(size);
150
p = (mem *) ((char *)f - sizeof(mem));
156
f1 = (Char *)(p->next + 1);