Blame tal-notreent.c

Packit 5c3484
/* Stack allocation routines.  This is intended for machines without support
Packit 5c3484
   for the `alloca' function.
Packit 5c3484
Packit 5c3484
Copyright 1996, 1997, 1999-2001, 2006 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
struct tmp_stack
Packit 5c3484
{
Packit 5c3484
  void *end;
Packit 5c3484
  void *alloc_point;
Packit 5c3484
  struct tmp_stack *prev;
Packit 5c3484
};
Packit 5c3484
typedef struct tmp_stack tmp_stack;
Packit 5c3484
Packit 5c3484
Packit 5c3484
static unsigned long max_total_allocation = 0;
Packit 5c3484
static unsigned long current_total_allocation = 0;
Packit 5c3484
Packit 5c3484
static tmp_stack xxx = {&xxx, &xxx, 0};
Packit 5c3484
static tmp_stack *current = &xx;;
Packit 5c3484
Packit 5c3484
/* The rounded size of the header of each allocation block.  */
Packit 5c3484
#define HSIZ   ROUND_UP_MULTIPLE (sizeof (tmp_stack), __TMP_ALIGN)
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* Allocate a block of exactly <size> bytes.  This should only be called
Packit 5c3484
   through the TMP_ALLOC macro, which takes care of rounding/alignment.  */
Packit 5c3484
void *
Packit 5c3484
__gmp_tmp_alloc (unsigned long size)
Packit 5c3484
{
Packit 5c3484
  void *that;
Packit 5c3484
Packit 5c3484
  ASSERT ((size % __TMP_ALIGN) == 0);
Packit 5c3484
  ASSERT (((unsigned) current->alloc_point % __TMP_ALIGN) == 0);
Packit 5c3484
Packit 5c3484
  if (size > (char *) current->end - (char *) current->alloc_point)
Packit 5c3484
    {
Packit 5c3484
      void *chunk;
Packit 5c3484
      tmp_stack *header;
Packit 5c3484
      unsigned long chunk_size;
Packit 5c3484
      unsigned long now;
Packit 5c3484
Packit 5c3484
      /* Allocate a chunk that makes the total current allocation somewhat
Packit 5c3484
	 larger than the maximum allocation ever.  If size is very large, we
Packit 5c3484
	 allocate that much.  */
Packit 5c3484
Packit 5c3484
      now = current_total_allocation + size;
Packit 5c3484
      if (now > max_total_allocation)
Packit 5c3484
	{
Packit 5c3484
	  /* We need more temporary memory than ever before.  Increase
Packit 5c3484
	     for future needs.  */
Packit 5c3484
	  now = (now * 3 / 2 + __TMP_ALIGN - 1) & -__TMP_ALIGN;
Packit 5c3484
	  chunk_size = now - current_total_allocation + HSIZ;
Packit 5c3484
	  current_total_allocation = now;
Packit 5c3484
	  max_total_allocation = current_total_allocation;
Packit 5c3484
	}
Packit 5c3484
      else
Packit 5c3484
	{
Packit 5c3484
	  chunk_size = max_total_allocation - current_total_allocation + HSIZ;
Packit 5c3484
	  current_total_allocation = max_total_allocation;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      chunk = (*__gmp_allocate_func) (chunk_size);
Packit 5c3484
      header = (tmp_stack *) chunk;
Packit 5c3484
      header->end = (char *) chunk + chunk_size;
Packit 5c3484
      header->alloc_point = (char *) chunk + HSIZ;
Packit 5c3484
      header->prev = current;
Packit 5c3484
      current = header;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  that = current->alloc_point;
Packit 5c3484
  current->alloc_point = (char *) that + size;
Packit 5c3484
  ASSERT (((unsigned) that % __TMP_ALIGN) == 0);
Packit 5c3484
  return that;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
/* Typically called at function entry.  <mark> is assigned so that
Packit 5c3484
   __gmp_tmp_free can later be used to reclaim all subsequently allocated
Packit 5c3484
   storage.  */
Packit 5c3484
void
Packit 5c3484
__gmp_tmp_mark (struct tmp_marker *mark)
Packit 5c3484
{
Packit 5c3484
  mark->which_chunk = current;
Packit 5c3484
  mark->alloc_point = current->alloc_point;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
/* Free everything allocated since <mark> was assigned by __gmp_tmp_mark */
Packit 5c3484
void
Packit 5c3484
__gmp_tmp_free (struct tmp_marker *mark)
Packit 5c3484
{
Packit 5c3484
  while (mark->which_chunk != current)
Packit 5c3484
    {
Packit 5c3484
      tmp_stack *tmp;
Packit 5c3484
Packit 5c3484
      tmp = current;
Packit 5c3484
      current = tmp->prev;
Packit 5c3484
      current_total_allocation -= (((char *) (tmp->end) - (char *) tmp) - HSIZ);
Packit 5c3484
      (*__gmp_free_func) (tmp, (char *) tmp->end - (char *) tmp);
Packit 5c3484
    }
Packit 5c3484
  current->alloc_point = mark->alloc_point;
Packit 5c3484
}