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

     1  /* TMP_ALLOC routines using malloc in a reentrant fashion.
     2  
     3  Copyright 2000, 2001 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  #include <stdio.h>
    32  #include "gmp.h"
    33  #include "gmp-impl.h"
    34  
    35  
    36  /* Each TMP_ALLOC uses __gmp_allocate_func to get a block of memory of the
    37     size requested, plus a header at the start which is used to hold the
    38     blocks on a linked list in the marker variable, ready for TMP_FREE to
    39     release.
    40  
    41     Callers should try to do multiple allocs with one call, in the style of
    42     TMP_ALLOC_LIMBS_2 if it's easy to arrange, since that will keep down the
    43     number of separate malloc calls.
    44  
    45     Enhancements:
    46  
    47     Could inline both TMP_ALLOC and TMP_FREE, though TMP_ALLOC would need the
    48     compiler to have "inline" since it returns a value.  The calls to malloc
    49     will be slow though, so it hardly seems worth worrying about one extra
    50     level of function call.  */
    51  
    52  
    53  #define HSIZ   ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN)
    54  
    55  void *
    56  __gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size)
    57  {
    58    char    *p;
    59    size_t  total_size;
    60  
    61  #define P   ((struct tmp_reentrant_t *) p)
    62  
    63    total_size = size + HSIZ;
    64    p = (char *) (*__gmp_allocate_func) (total_size);
    65    P->size = total_size;
    66    P->next = *markp;
    67    *markp = P;
    68    return p + HSIZ;
    69  }
    70  
    71  void
    72  __gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark)
    73  {
    74    struct tmp_reentrant_t  *next;
    75  
    76    while (mark != NULL)
    77      {
    78        next = mark->next;
    79        (*__gmp_free_func) ((char *) mark, mark->size);
    80        mark = next;
    81      }
    82  }