[project @ 2000-08-04 23:28:04 by lewie]
[ghc.git] / ghc / rts / gmp / stack-alloc.c
1 /* Stack allocation routines. This is intended for machines without support
2 for the `alloca' function.
3
4 Copyright (C) 1996 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16 License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
22
23 #include "stack-alloc.h"
24
25 typedef struct tmp_stack tmp_stack;
26
27 void *malloc ();
28 static unsigned long max_total_allocation = 0;
29 static unsigned long current_total_allocation = 0;
30
31 static tmp_stack xxx = {&xxx, &xxx, 0};
32 static tmp_stack *current = &xxx;
33
34 /* Allocate a block of exactly <size> bytes. This should only be called
35 through the TMP_ALLOC macro, which takes care of rounding/alignment. */
36 void *
37 __tmp_alloc (size)
38 unsigned long size;
39 {
40 void *this;
41
42 if (size > (char *) current->end - (char *) current->alloc_point)
43 {
44 void *chunk;
45 tmp_stack *header;
46 unsigned long chunk_size;
47 unsigned long now;
48
49 /* Allocate a chunk that makes the total current allocation somewhat
50 larger than the maximum allocation ever. If size is very large, we
51 allocate that much. */
52
53 now = current_total_allocation + size;
54 if (now > max_total_allocation)
55 {
56 /* We need more temporary memory than ever before. Increase
57 for future needs. */
58 now = now * 3 / 2;
59 chunk_size = now - current_total_allocation + sizeof (tmp_stack);
60 current_total_allocation = now;
61 max_total_allocation = current_total_allocation;
62 }
63 else
64 {
65 chunk_size = max_total_allocation - current_total_allocation + sizeof (tmp_stack);
66 current_total_allocation = max_total_allocation;
67 }
68
69 chunk = malloc (chunk_size);
70 header = chunk;
71 header->end = (char *) chunk + chunk_size;
72 header->alloc_point = (char *) chunk + sizeof (tmp_stack);
73 header->prev = current;
74 current = header;
75 }
76
77 this = current->alloc_point;
78 current->alloc_point = (char *) this + size;
79 return this;
80 }
81
82 /* Typically called at function entry. <mark> is assigned so that __tmp_free
83 can later be used to reclaim all subsecuently allocated storage. */
84 void
85 __tmp_mark (mark)
86 tmp_marker *mark;
87 {
88 mark->which_chunk = current;
89 mark->alloc_point = current->alloc_point;
90 }
91
92 /* Free everything allocated since <mark> was assigned by __tmp_mark */
93 void
94 __tmp_free (mark)
95 tmp_marker *mark;
96 {
97 while (mark->which_chunk != current)
98 {
99 tmp_stack *tmp;
100
101 tmp = current;
102 current = tmp->prev;
103 current_total_allocation -= (((char *) (tmp->end) - (char *) tmp)
104 - sizeof (tmp_stack));
105 free (tmp);
106 }
107 current->alloc_point = mark->alloc_point;
108 }