Pristine Ack-5.5
[Ack-5.5.git] / modules / src / alloc / std_alloc.c
1 /* $Id: std_alloc.c,v 1.9 1994/06/24 11:06:50 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 /*      std_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         This is a counting version of st_alloc.
10 */
11
12 #if __STDC__
13 #include <stdlib.h>
14 #else
15 extern char *malloc();
16 #endif
17
18 #include        "alloc.h"
19
20 char *
21 std_alloc(phead, size, count, pcnt)
22         char **phead;
23         register unsigned int size;
24         int *pcnt;
25 {
26         register char *p;
27         register long *q;
28         char *retval;
29
30         if (*phead == 0)        {
31                 while (count >= 1 && (p = malloc(size * count)) == 0) {
32                         count >>= 1;
33                 }
34                 if (p == 0) {
35                         No_Mem();
36                 }
37                 *pcnt += count;
38                 ((_PALLOC_) p)->_A_next = 0;
39                 while (--count) {
40                         p += size;
41                         ((_PALLOC_) p)->_A_next = (_PALLOC_) (p - size);
42                 }
43                 *phead = p;
44         }
45         else p = *phead;
46         *phead = (char *) (((_PALLOC_) p)->_A_next);
47         retval = p;
48         q = (long *) p;
49         while (size >= 8*sizeof(long)) {
50                 *q++ = 0;
51                 *q++ = 0;
52                 *q++ = 0;
53                 *q++ = 0;
54                 *q++ = 0;
55                 *q++ = 0;
56                 *q++ = 0;
57                 *q++ = 0;
58                 size -= 8*sizeof(long);
59         }
60         while (size >= sizeof(long)) {
61                 *q++ = 0;
62                 size -= sizeof(long);
63         }
64         p = (char *) q;
65
66         while (size--) *p++ = 0;
67         return retval;
68 }