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