1 /* Stack allocation routines. This is intended for machines without support
2 for the `alloca' function.
4 Copyright (C) 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
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.
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.
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. */
23 #include "stack-alloc.h"
29 /* gmp-impl.h and stack-alloc.h conflict when not USE_STACK_ALLOC, so these
30 declarations are copied here */
32 extern void * (*__gmp_allocate_func) (size_t);
33 extern void (*__gmp_free_func) (void *, size_t);
35 extern void * (*__gmp_allocate_func) ();
36 extern void (*__gmp_free_func) ();
39 typedef struct tmp_stack tmp_stack;
41 static unsigned long max_total_allocation = 0;
42 static unsigned long current_total_allocation = 0;
44 static tmp_stack xxx = {&xxx, &xxx, 0};
45 static tmp_stack *current = &xxx;
47 /* The rounded size of the header of each allocation block. */
48 #define HSIZ ((sizeof (tmp_stack) + __TMP_ALIGN - 1) & -__TMP_ALIGN)
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. */
54 __gmp_tmp_alloc (unsigned long size)
56 __gmp_tmp_alloc (size)
62 if (size > (char *) current->end - (char *) current->alloc_point)
66 unsigned long chunk_size;
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. */
73 now = current_total_allocation + size;
74 if (now > max_total_allocation)
76 /* We need more temporary memory than ever before. Increase
79 chunk_size = now - current_total_allocation + HSIZ;
80 current_total_allocation = now;
81 max_total_allocation = current_total_allocation;
85 chunk_size = max_total_allocation - current_total_allocation + HSIZ;
86 current_total_allocation = max_total_allocation;
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;
97 that = current->alloc_point;
98 current->alloc_point = (char *) that + size;
102 /* Typically called at function entry. <mark> is assigned so that
103 __gmp_tmp_free can later be used to reclaim all subsequently allocated
107 __gmp_tmp_mark (tmp_marker *mark)
109 __gmp_tmp_mark (mark)
113 mark->which_chunk = current;
114 mark->alloc_point = current->alloc_point;
117 /* Free everything allocated since <mark> was assigned by __gmp_tmp_mark */
120 __gmp_tmp_free (tmp_marker *mark)
122 __gmp_tmp_free (mark)
126 while (mark->which_chunk != current)
132 current_total_allocation -= (((char *) (tmp->end) - (char *) tmp) - HSIZ);
133 (*__gmp_free_func) (tmp, (char *) tmp->end - (char *) tmp);
135 current->alloc_point = mark->alloc_point;