Blame tal-reent.c

Packit 15dc08
/* TMP_ALLOC routines using malloc in a reentrant fashion.
Packit 15dc08
Packit 15dc08
Copyright 2000, 2001 Free Software Foundation, Inc.
Packit 15dc08
Packit 15dc08
This file is part of the GNU MP Library.
Packit 15dc08
Packit 15dc08
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 15dc08
it under the terms of either:
Packit 15dc08
Packit 15dc08
  * the GNU Lesser General Public License as published by the Free
Packit 15dc08
    Software Foundation; either version 3 of the License, or (at your
Packit 15dc08
    option) any later version.
Packit 15dc08
Packit 15dc08
or
Packit 15dc08
Packit 15dc08
  * the GNU General Public License as published by the Free Software
Packit 15dc08
    Foundation; either version 2 of the License, or (at your option) any
Packit 15dc08
    later version.
Packit 15dc08
Packit 15dc08
or both in parallel, as here.
Packit 15dc08
Packit 15dc08
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 15dc08
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 15dc08
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 15dc08
for more details.
Packit 15dc08
Packit 15dc08
You should have received copies of the GNU General Public License and the
Packit 15dc08
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 15dc08
see https://www.gnu.org/licenses/.  */
Packit 15dc08
Packit 15dc08
#include <stdio.h>
Packit 15dc08
#include "gmp.h"
Packit 15dc08
#include "gmp-impl.h"
Packit 15dc08
Packit 15dc08
Packit 15dc08
/* Each TMP_ALLOC uses __gmp_allocate_func to get a block of memory of the
Packit 15dc08
   size requested, plus a header at the start which is used to hold the
Packit 15dc08
   blocks on a linked list in the marker variable, ready for TMP_FREE to
Packit 15dc08
   release.
Packit 15dc08
Packit 15dc08
   Callers should try to do multiple allocs with one call, in the style of
Packit 15dc08
   TMP_ALLOC_LIMBS_2 if it's easy to arrange, since that will keep down the
Packit 15dc08
   number of separate malloc calls.
Packit 15dc08
Packit 15dc08
   Enhancements:
Packit 15dc08
Packit 15dc08
   Could inline both TMP_ALLOC and TMP_FREE, though TMP_ALLOC would need the
Packit 15dc08
   compiler to have "inline" since it returns a value.  The calls to malloc
Packit 15dc08
   will be slow though, so it hardly seems worth worrying about one extra
Packit 15dc08
   level of function call.  */
Packit 15dc08
Packit 15dc08
Packit 15dc08
#define HSIZ   ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN)
Packit 15dc08
Packit 15dc08
void *
Packit 15dc08
__gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size)
Packit 15dc08
{
Packit 15dc08
  char    *p;
Packit 15dc08
  size_t  total_size;
Packit 15dc08
Packit 15dc08
#define P   ((struct tmp_reentrant_t *) p)
Packit 15dc08
Packit 15dc08
  total_size = size + HSIZ;
Packit 15dc08
  p = (char *) (*__gmp_allocate_func) (total_size);
Packit 15dc08
  P->size = total_size;
Packit 15dc08
  P->next = *markp;
Packit 15dc08
  *markp = P;
Packit 15dc08
  return p + HSIZ;
Packit 15dc08
}
Packit 15dc08
Packit 15dc08
void
Packit 15dc08
__gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark)
Packit 15dc08
{
Packit 15dc08
  struct tmp_reentrant_t  *next;
Packit 15dc08
Packit 15dc08
  while (mark != NULL)
Packit 15dc08
    {
Packit 15dc08
      next = mark->next;
Packit 15dc08
      (*__gmp_free_func) ((char *) mark, mark->size);
Packit 15dc08
      mark = next;
Packit 15dc08
    }
Packit 15dc08
}