3 /* Copyright (c) 1994 Stanford University
7 This software is provided under the terms described in
8 the "suif_copyright.h" include file. */
10 #include <suif_copyright.h>
12 /* snoot tree management */
16 static struct arena first
[] =
18 { 0, 0, 0, &first
[0], 0 },
19 { 0, 0, 0, &first
[1], 0 },
22 Arena permanent
= &first
[0]; /* permanent storage */
23 Arena transient
= &first
[1]; /* transient storage; released at function end */
24 static Arena freearenas
; /* list of free arenas */
26 /* allocate - allocate n bytes in arena **p, adding a new arena if necessary */
27 char *allocate(int n
, Arena
*p
)
31 while (ap
->avail
+ n
> ap
->limit
)
33 if (ap
->next
!= NULL
) /* move to next arena */
36 ap
->avail
= (char *)ap
+ roundup(sizeof *ap
, sizeof (double));
38 else if ((ap
->next
= freearenas
) != NULL
)
40 freearenas
= freearenas
->next
;
42 ap
->avail
= (char *)ap
+ roundup(sizeof *ap
, sizeof (double));
43 ap
->first
= (*p
)->first
;
46 else /* allocate a new arena */
48 int m
= n
+ MEMINCR
* 1024 + roundup(sizeof *ap
, sizeof (double));
49 ap
->next
= (Arena
)malloc(m
);
50 assert(ap
->next
!= NULL
);
51 if ((char *)ap
->next
== ap
->limit
) /* extend previous arena? */
53 ap
->limit
= (char *)ap
->next
+ m
;
55 else /* link to a new arena */
58 ap
->limit
= (char *)ap
+ m
;
59 ap
->avail
= (char *)ap
+ roundup(sizeof *ap
, sizeof (double));
61 ap
->first
= (*p
)->first
;
70 /* deallocate - release all space in arena *p, except the first arena;
72 void deallocate(Arena
*p
)
74 Arena first
= (*p
)->first
;
76 (*p
)->next
= freearenas
;
77 freearenas
= first
->next
;