1 /* Stack allocation routines. This is intended for machines without support
2 for the `alloca' function.
4 Copyright (C) 1996 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 Library General Public License as published by
10 the Free Software Foundation; either version 2 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 Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library 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"
25 typedef struct tmp_stack tmp_stack;
28 static unsigned long max_total_allocation = 0;
29 static unsigned long current_total_allocation = 0;
31 static tmp_stack xxx = {&xxx, &xxx, 0};
32 static tmp_stack *current = &xxx;
34 /* Allocate a block of exactly <size> bytes. This should only be called
35 through the TMP_ALLOC macro, which takes care of rounding/alignment. */
42 if (size > (char *) current->end - (char *) current->alloc_point)
46 unsigned long chunk_size;
49 /* Allocate a chunk that makes the total current allocation somewhat
50 larger than the maximum allocation ever. If size is very large, we
51 allocate that much. */
53 now = current_total_allocation + size;
54 if (now > max_total_allocation)
56 /* We need more temporary memory than ever before. Increase
59 chunk_size = now - current_total_allocation + sizeof (tmp_stack);
60 current_total_allocation = now;
61 max_total_allocation = current_total_allocation;
65 chunk_size = max_total_allocation - current_total_allocation + sizeof (tmp_stack);
66 current_total_allocation = max_total_allocation;
69 chunk = malloc (chunk_size);
71 header->end = (char *) chunk + chunk_size;
72 header->alloc_point = (char *) chunk + sizeof (tmp_stack);
73 header->prev = current;
77 this = current->alloc_point;
78 current->alloc_point = (char *) this + size;
82 /* Typically called at function entry. <mark> is assigned so that __tmp_free
83 can later be used to reclaim all subsecuently allocated storage. */
88 mark->which_chunk = current;
89 mark->alloc_point = current->alloc_point;
92 /* Free everything allocated since <mark> was assigned by __tmp_mark */
97 while (mark->which_chunk != current)
103 current_total_allocation -= (((char *) (tmp->end) - (char *) tmp)
104 - sizeof (tmp_stack));
107 current->alloc_point = mark->alloc_point;