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