github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tal-notreent.c (about)

     1  /* Stack allocation routines.  This is intended for machines without support
     2     for the `alloca' function.
     3  
     4  Copyright 1996, 1997, 1999-2001, 2006 Free Software Foundation, Inc.
     5  
     6  This file is part of the GNU MP Library.
     7  
     8  The GNU MP Library is free software; you can redistribute it and/or modify
     9  it 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  
    15  or
    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  
    21  or both in parallel, as here.
    22  
    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
    26  for more details.
    27  
    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/.  */
    31  
    32  #include "gmp.h"
    33  #include "gmp-impl.h"
    34  
    35  
    36  struct tmp_stack
    37  {
    38    void *end;
    39    void *alloc_point;
    40    struct tmp_stack *prev;
    41  };
    42  typedef struct tmp_stack tmp_stack;
    43  
    44  
    45  static unsigned long max_total_allocation = 0;
    46  static unsigned long current_total_allocation = 0;
    47  
    48  static tmp_stack xxx = {&xxx, &xxx, 0};
    49  static tmp_stack *current = &xxx;
    50  
    51  /* The rounded size of the header of each allocation block.  */
    52  #define HSIZ   ROUND_UP_MULTIPLE (sizeof (tmp_stack), __TMP_ALIGN)
    53  
    54  
    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.  */
    57  void *
    58  __gmp_tmp_alloc (unsigned long size)
    59  {
    60    void *that;
    61  
    62    ASSERT ((size % __TMP_ALIGN) == 0);
    63    ASSERT (((unsigned) current->alloc_point % __TMP_ALIGN) == 0);
    64  
    65    if (size > (char *) current->end - (char *) current->alloc_point)
    66      {
    67        void *chunk;
    68        tmp_stack *header;
    69        unsigned long chunk_size;
    70        unsigned long now;
    71  
    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.  */
    75  
    76        now = current_total_allocation + size;
    77        if (now > max_total_allocation)
    78  	{
    79  	  /* We need more temporary memory than ever before.  Increase
    80  	     for future needs.  */
    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;
    85  	}
    86        else
    87  	{
    88  	  chunk_size = max_total_allocation - current_total_allocation + HSIZ;
    89  	  current_total_allocation = max_total_allocation;
    90  	}
    91  
    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;
    97        current = header;
    98      }
    99  
   100    that = current->alloc_point;
   101    current->alloc_point = (char *) that + size;
   102    ASSERT (((unsigned) that % __TMP_ALIGN) == 0);
   103    return that;
   104  }
   105  
   106  /* Typically called at function entry.  <mark> is assigned so that
   107     __gmp_tmp_free can later be used to reclaim all subsequently allocated
   108     storage.  */
   109  void
   110  __gmp_tmp_mark (struct tmp_marker *mark)
   111  {
   112    mark->which_chunk = current;
   113    mark->alloc_point = current->alloc_point;
   114  }
   115  
   116  /* Free everything allocated since <mark> was assigned by __gmp_tmp_mark */
   117  void
   118  __gmp_tmp_free (struct tmp_marker *mark)
   119  {
   120    while (mark->which_chunk != current)
   121      {
   122        tmp_stack *tmp;
   123  
   124        tmp = current;
   125        current = tmp->prev;
   126        current_total_allocation -= (((char *) (tmp->end) - (char *) tmp) - HSIZ);
   127        (*__gmp_free_func) (tmp, (char *) tmp->end - (char *) tmp);
   128      }
   129    current->alloc_point = mark->alloc_point;
   130  }