Bring in an errno.9 manual page (based on NetBSD's).
[dragonfly.git] / contrib / gmp / tal-reent.c
blob32db9342c920e2fecfa4395bd65edf980dbe7d51
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 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.
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.
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/. */
20 #include <stdio.h>
21 #include "gmp.h"
22 #include "gmp-impl.h"
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.
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.
34 Enhancements:
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. */
42 #define HSIZ ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN)
44 void *
45 __gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size)
47 char *p;
48 size_t total_size;
50 #define P ((struct tmp_reentrant_t *) p)
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;
60 void
61 __gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark)
63 struct tmp_reentrant_t *next;
65 while (mark != NULL)
67 next = mark->next;
68 (*__gmp_free_func) ((char *) mark, mark->size);
69 mark = next;