beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / tal-notreent.c
blobed162e552b3b6bacc13bc33e0303dbdbae68dd19
1 /* Stack allocation routines. This is intended for machines without support
2 for the `alloca' function.
4 Copyright 1996, 1997, 1999-2001, 2006 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 either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
36 struct tmp_stack
38 void *end;
39 void *alloc_point;
40 struct tmp_stack *prev;
42 typedef struct tmp_stack tmp_stack;
45 static unsigned long max_total_allocation = 0;
46 static unsigned long current_total_allocation = 0;
48 static tmp_stack xxx = {&xxx, &xxx, 0};
49 static tmp_stack *current = &xxx;
51 /* The rounded size of the header of each allocation block. */
52 #define HSIZ ROUND_UP_MULTIPLE (sizeof (tmp_stack), __TMP_ALIGN)
55 /* Allocate a block of exactly <size> bytes. This should only be called
56 through the TMP_ALLOC macro, which takes care of rounding/alignment. */
57 void *
58 __gmp_tmp_alloc (unsigned long size)
60 void *that;
62 ASSERT ((size % __TMP_ALIGN) == 0);
63 ASSERT (((unsigned) current->alloc_point % __TMP_ALIGN) == 0);
65 if (size > (char *) current->end - (char *) current->alloc_point)
67 void *chunk;
68 tmp_stack *header;
69 unsigned long chunk_size;
70 unsigned long now;
72 /* Allocate a chunk that makes the total current allocation somewhat
73 larger than the maximum allocation ever. If size is very large, we
74 allocate that much. */
76 now = current_total_allocation + size;
77 if (now > max_total_allocation)
79 /* We need more temporary memory than ever before. Increase
80 for future needs. */
81 now = (now * 3 / 2 + __TMP_ALIGN - 1) & -__TMP_ALIGN;
82 chunk_size = now - current_total_allocation + HSIZ;
83 current_total_allocation = now;
84 max_total_allocation = current_total_allocation;
86 else
88 chunk_size = max_total_allocation - current_total_allocation + HSIZ;
89 current_total_allocation = max_total_allocation;
92 chunk = (*__gmp_allocate_func) (chunk_size);
93 header = (tmp_stack *) chunk;
94 header->end = (char *) chunk + chunk_size;
95 header->alloc_point = (char *) chunk + HSIZ;
96 header->prev = current;
97 current = header;
100 that = current->alloc_point;
101 current->alloc_point = (char *) that + size;
102 ASSERT (((unsigned) that % __TMP_ALIGN) == 0);
103 return that;
106 /* Typically called at function entry. <mark> is assigned so that
107 __gmp_tmp_free can later be used to reclaim all subsequently allocated
108 storage. */
109 void
110 __gmp_tmp_mark (struct tmp_marker *mark)
112 mark->which_chunk = current;
113 mark->alloc_point = current->alloc_point;
116 /* Free everything allocated since <mark> was assigned by __gmp_tmp_mark */
117 void
118 __gmp_tmp_free (struct tmp_marker *mark)
120 while (mark->which_chunk != current)
122 tmp_stack *tmp;
124 tmp = current;
125 current = tmp->prev;
126 current_total_allocation -= (((char *) (tmp->end) - (char *) tmp) - HSIZ);
127 (*__gmp_free_func) (tmp, (char *) tmp->end - (char *) tmp);
129 current->alloc_point = mark->alloc_point;