/src/gmp-6.2.1/tal-reent.c
Line  | Count  | Source  | 
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  | 4.59k  | #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  | 2.29k  | { | 
57  | 2.29k  |   char    *p;  | 
58  | 2.29k  |   size_t  total_size;  | 
59  |  |  | 
60  | 6.88k  | #define P   ((struct tmp_reentrant_t *) p)  | 
61  |  |  | 
62  | 2.29k  |   total_size = size + HSIZ;  | 
63  | 2.29k  |   p = __GMP_ALLOCATE_FUNC_TYPE (total_size, char);  | 
64  | 2.29k  |   P->size = total_size;  | 
65  | 2.29k  |   P->next = *markp;  | 
66  | 2.29k  |   *markp = P;  | 
67  | 2.29k  |   return p + HSIZ;  | 
68  | 2.29k  | }  | 
69  |  |  | 
70  |  | void  | 
71  |  | __gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark)  | 
72  | 1.14k  | { | 
73  | 1.14k  |   struct tmp_reentrant_t  *next;  | 
74  |  |  | 
75  | 3.44k  |   while (mark != NULL)  | 
76  | 2.29k  |     { | 
77  | 2.29k  |       next = mark->next;  | 
78  | 2.29k  |       (*__gmp_free_func) ((char *) mark, mark->size);  | 
79  | 2.29k  |       mark = next;  | 
80  | 2.29k  |     }  | 
81  | 1.14k  | }  |