blob: 083fa842feb385f53af13040d1da4f85a1cd3709 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* Stack allocation routines. This is intended for machines without support
2 for the `alloca' function.
3
4Copyright 1996, 1997, 1999-2001, 2006 Free Software Foundation, Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of either:
10
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.
14
15or
16
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.
20
21or both in parallel, as here.
22
23The GNU MP Library is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26for more details.
27
28You should have received copies of the GNU General Public License and the
29GNU Lesser General Public License along with the GNU MP Library. If not,
30see https://www.gnu.org/licenses/. */
31
32#include "gmp-impl.h"
33
34
35struct tmp_stack
36{
37 void *end;
38 void *alloc_point;
39 struct tmp_stack *prev;
40};
41typedef struct tmp_stack tmp_stack;
42
43
44static unsigned long max_total_allocation = 0;
45static unsigned long current_total_allocation = 0;
46
47static tmp_stack xxx = {&xxx, &xxx, 0};
48static tmp_stack *current = &xxx;
49
50/* The rounded size of the header of each allocation block. */
51#define HSIZ ROUND_UP_MULTIPLE (sizeof (tmp_stack), __TMP_ALIGN)
52
53
54/* Allocate a block of exactly <size> bytes. This should only be called
55 through the TMP_ALLOC macro, which takes care of rounding/alignment. */
56void *
57__gmp_tmp_alloc (unsigned long size)
58{
59 void *that;
60
61 ASSERT ((size % __TMP_ALIGN) == 0);
62 ASSERT (((unsigned) current->alloc_point % __TMP_ALIGN) == 0);
63
64 if (size > (char *) current->end - (char *) current->alloc_point)
65 {
66 void *chunk;
67 tmp_stack *header;
68 unsigned long chunk_size;
69 unsigned long now;
70
71 /* Allocate a chunk that makes the total current allocation somewhat
72 larger than the maximum allocation ever. If size is very large, we
73 allocate that much. */
74
75 now = current_total_allocation + size;
76 if (now > max_total_allocation)
77 {
78 /* We need more temporary memory than ever before. Increase
79 for future needs. */
80 now = (now * 3 / 2 + __TMP_ALIGN - 1) & -__TMP_ALIGN;
81 chunk_size = now - current_total_allocation + HSIZ;
82 current_total_allocation = now;
83 max_total_allocation = current_total_allocation;
84 }
85 else
86 {
87 chunk_size = max_total_allocation - current_total_allocation + HSIZ;
88 current_total_allocation = max_total_allocation;
89 }
90
91 chunk = (*__gmp_allocate_func) (chunk_size);
92 header = (tmp_stack *) chunk;
93 header->end = (char *) chunk + chunk_size;
94 header->alloc_point = (char *) chunk + HSIZ;
95 header->prev = current;
96 current = header;
97 }
98
99 that = current->alloc_point;
100 current->alloc_point = (char *) that + size;
101 ASSERT (((unsigned) that % __TMP_ALIGN) == 0);
102 return that;
103}
104
105/* Typically called at function entry. <mark> is assigned so that
106 __gmp_tmp_free can later be used to reclaim all subsequently allocated
107 storage. */
108void
109__gmp_tmp_mark (struct tmp_marker *mark)
110{
111 mark->which_chunk = current;
112 mark->alloc_point = current->alloc_point;
113}
114
115/* Free everything allocated since <mark> was assigned by __gmp_tmp_mark */
116void
117__gmp_tmp_free (struct tmp_marker *mark)
118{
119 while (mark->which_chunk != current)
120 {
121 tmp_stack *tmp;
122
123 tmp = current;
124 current = tmp->prev;
125 current_total_allocation -= (((char *) (tmp->end) - (char *) tmp) - HSIZ);
126 (*__gmp_free_func) (tmp, (char *) tmp->end - (char *) tmp);
127 }
128 current->alloc_point = mark->alloc_point;
129}