beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / tal-reent.c
blob92f516b5ff3d08b8290aab8ca636dc0152f459b3
1 /* TMP_ALLOC routines using malloc in a reentrant fashion.
3 Copyright 2000, 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/. */
31 #include <stdio.h>
32 #include "gmp.h"
33 #include "gmp-impl.h"
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.
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.
45 Enhancements:
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. */
53 #define HSIZ ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN)
55 void *
56 __gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size)
58 char *p;
59 size_t total_size;
61 #define P ((struct tmp_reentrant_t *) p)
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;
71 void
72 __gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark)
74 struct tmp_reentrant_t *next;
76 while (mark != NULL)
78 next = mark->next;
79 (*__gmp_free_func) ((char *) mark, mark->size);
80 mark = next;