FIX BUILD (with GHC 6.2.x): System.Directory.Internals is no more
[ghc-hetmet.git] / 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, 1997, 1999, 2000 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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU Lesser 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 #define __need_size_t
26 #include <stddef.h>
27 #undef __need_size_t
28
29 /* gmp-impl.h and stack-alloc.h conflict when not USE_STACK_ALLOC, so these
30    declarations are copied here */
31 #if __STDC__
32 extern void *   (*__gmp_allocate_func) (size_t);
33 extern void     (*__gmp_free_func) (void *, size_t);
34 #else
35 extern void *   (*__gmp_allocate_func) ();
36 extern void     (*__gmp_free_func) ();
37 #endif
38
39 typedef struct tmp_stack tmp_stack;
40
41 static unsigned long max_total_allocation = 0;
42 static unsigned long current_total_allocation = 0;
43
44 static tmp_stack xxx = {&xxx, &xxx, 0};
45 static tmp_stack *current = &xxx;
46
47 /* The rounded size of the header of each allocation block.  */
48 #define HSIZ ((sizeof (tmp_stack) + __TMP_ALIGN - 1) & -__TMP_ALIGN)
49
50 /* Allocate a block of exactly <size> bytes.  This should only be called
51    through the TMP_ALLOC macro, which takes care of rounding/alignment.  */
52 void *
53 #if __STDC__
54 __gmp_tmp_alloc (unsigned long size)
55 #else
56 __gmp_tmp_alloc (size)
57      unsigned long size;
58 #endif
59 {
60   void *that;
61
62   if (size > (char *) current->end - (char *) current->alloc_point)
63     {
64       void *chunk;
65       tmp_stack *header;
66       unsigned long chunk_size;
67       unsigned long now;
68
69       /* Allocate a chunk that makes the total current allocation somewhat
70          larger than the maximum allocation ever.  If size is very large, we
71          allocate that much.  */
72
73       now = current_total_allocation + size;
74       if (now > max_total_allocation)
75         {
76           /* We need more temporary memory than ever before.  Increase
77              for future needs.  */
78           now = now * 3 / 2;
79           chunk_size = now - current_total_allocation + HSIZ;
80           current_total_allocation = now;
81           max_total_allocation = current_total_allocation;
82         }
83       else
84         {
85           chunk_size = max_total_allocation - current_total_allocation + HSIZ;
86           current_total_allocation = max_total_allocation;
87         }
88
89       chunk = (*__gmp_allocate_func) (chunk_size);
90       header = (tmp_stack *) chunk;
91       header->end = (char *) chunk + chunk_size;
92       header->alloc_point = (char *) chunk + HSIZ;
93       header->prev = current;
94       current = header;
95     }
96
97   that = current->alloc_point;
98   current->alloc_point = (char *) that + size;
99   return that;
100 }
101
102 /* Typically called at function entry.  <mark> is assigned so that
103    __gmp_tmp_free can later be used to reclaim all subsequently allocated
104    storage.  */
105 void
106 #if __STDC__
107 __gmp_tmp_mark (tmp_marker *mark)
108 #else
109 __gmp_tmp_mark (mark)
110      tmp_marker *mark;
111 #endif
112 {
113   mark->which_chunk = current;
114   mark->alloc_point = current->alloc_point;
115 }
116
117 /* Free everything allocated since <mark> was assigned by __gmp_tmp_mark */
118 void
119 #if __STDC__
120 __gmp_tmp_free (tmp_marker *mark)
121 #else
122 __gmp_tmp_free (mark)
123      tmp_marker *mark;
124 #endif
125 {
126   while (mark->which_chunk != current)
127     {
128       tmp_stack *tmp;
129
130       tmp = current;
131       current = tmp->prev;
132       current_total_allocation -= (((char *) (tmp->end) - (char *) tmp) - HSIZ);
133       (*__gmp_free_func) (tmp, (char *) tmp->end - (char *) tmp);
134     }
135   current->alloc_point = mark->alloc_point;
136 }