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