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
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
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/. */
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. */
58 __gmp_tmp_alloc (unsigned long size
)
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
)
69 unsigned long chunk_size
;
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
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
;
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
;
100 that
= current
->alloc_point
;
101 current
->alloc_point
= (char *) that
+ size
;
102 ASSERT (((unsigned) that
% __TMP_ALIGN
) == 0);
106 /* Typically called at function entry. <mark> is assigned so that
107 __gmp_tmp_free can later be used to reclaim all subsequently allocated
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 */
118 __gmp_tmp_free (struct tmp_marker
*mark
)
120 while (mark
->which_chunk
!= current
)
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
;