Austin Schuh | dace2a6 | 2020-08-18 10:56:48 -0700 | [diff] [blame] | 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 either: |
| 9 | |
| 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. |
| 13 | |
| 14 | or |
| 15 | |
| 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. |
| 19 | |
| 20 | or both in parallel, as here. |
| 21 | |
| 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. |
| 26 | |
| 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/. */ |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include "gmp-impl.h" |
| 33 | |
| 34 | |
| 35 | /* Each TMP_ALLOC uses __gmp_allocate_func to get a block of memory of the |
| 36 | size requested, plus a header at the start which is used to hold the |
| 37 | blocks on a linked list in the marker variable, ready for TMP_FREE to |
| 38 | release. |
| 39 | |
| 40 | Callers should try to do multiple allocs with one call, in the style of |
| 41 | TMP_ALLOC_LIMBS_2 if it's easy to arrange, since that will keep down the |
| 42 | number of separate malloc calls. |
| 43 | |
| 44 | Enhancements: |
| 45 | |
| 46 | Could inline both TMP_ALLOC and TMP_FREE, though TMP_ALLOC would need the |
| 47 | compiler to have "inline" since it returns a value. The calls to malloc |
| 48 | will be slow though, so it hardly seems worth worrying about one extra |
| 49 | level of function call. */ |
| 50 | |
| 51 | |
| 52 | #define HSIZ ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN) |
| 53 | |
| 54 | void * |
| 55 | __gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size) |
| 56 | { |
| 57 | char *p; |
| 58 | size_t total_size; |
| 59 | |
| 60 | #define P ((struct tmp_reentrant_t *) p) |
| 61 | |
| 62 | total_size = size + HSIZ; |
| 63 | p = __GMP_ALLOCATE_FUNC_TYPE (total_size, char); |
| 64 | P->size = total_size; |
| 65 | P->next = *markp; |
| 66 | *markp = P; |
| 67 | return p + HSIZ; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | __gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark) |
| 72 | { |
| 73 | struct tmp_reentrant_t *next; |
| 74 | |
| 75 | while (mark != NULL) |
| 76 | { |
| 77 | next = mark->next; |
| 78 | (*__gmp_free_func) ((char *) mark, mark->size); |
| 79 | mark = next; |
| 80 | } |
| 81 | } |