Pristine Ack-5.5
[Ack-5.5.git] / modules / src / alloc / st_alloc.c
1 /* $Id: st_alloc.c,v 1.11 1994/06/24 11:06:47 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*      st_alloc - get a structure from a free list. If no structures left,
7                 create new ones.
8                 The counterpart, st_free, is a macro, defined in alloc.h
9 */
10
11 #if __STDC__
12 #include <stdlib.h>
13 #else
14 extern char *malloc();
15 #endif
16
17 #include        "alloc.h"
18
19 char *
20 st_alloc(phead, size, count)
21         char **phead;
22         register unsigned int size;
23 {
24         register char *p;
25         register long *q;
26         char *retval;
27
28         if (*phead == 0)        {
29                 while (count >= 1 && (p = malloc(size * count)) == 0) {
30                         count >>= 1;
31                 }
32                 if (p == 0) {
33                         No_Mem();
34                 }
35                 ((_PALLOC_) p)->_A_next = 0;
36                 while (--count) {
37                         p += size;
38                         ((_PALLOC_) p)->_A_next = (_PALLOC_) (p - size);
39                 }
40                 *phead = p;
41         }
42         else    p = *phead;
43         *phead = (char *) (((_PALLOC_)p)->_A_next);
44         retval = p;
45         q = (long *) p;
46         while (size >= 8*sizeof(long)) {
47                 *q++ = 0;
48                 *q++ = 0;
49                 *q++ = 0;
50                 *q++ = 0;
51                 *q++ = 0;
52                 *q++ = 0;
53                 *q++ = 0;
54                 *q++ = 0;
55                 size -= 8*sizeof(long);
56         }
57         while (size >= sizeof(long)) {
58                 *q++ = 0;
59                 size -= sizeof(long);
60         }
61         p = (char *) q;
62
63         while (size--) *p++ = 0;
64         return retval;
65 }