tizen 2.3 release
[external/gmp.git] / tal-reent.c
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 the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11
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.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include <stdio.h>
23 #include "gmp.h"
24 #include "gmp-impl.h"
25
26
27 /* Each TMP_ALLOC uses __gmp_allocate_func to get a block of memory of the
28    size requested, plus a header at the start which is used to hold the
29    blocks on a linked list in the marker variable, ready for TMP_FREE to
30    release.
31
32    Callers should try to do multiple allocs with one call, in the style of
33    TMP_ALLOC_LIMBS_2 if it's easy to arrange, since that will keep down the
34    number of separate malloc calls.
35
36    Enhancements:
37
38    Could inline both TMP_ALLOC and TMP_FREE, though TMP_ALLOC would need the
39    compiler to have "inline" since it returns a value.  The calls to malloc
40    will be slow though, so it hardly seems worth worrying about one extra
41    level of function call.  */
42
43
44 #define HSIZ   ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN)
45
46 void *
47 __gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size)
48 {
49   char    *p;
50   size_t  total_size;
51
52 #define P   ((struct tmp_reentrant_t *) p)
53
54   total_size = size + HSIZ;
55   p = (*__gmp_allocate_func) (total_size);
56   P->size = total_size;
57   P->next = *markp;
58   *markp = P;
59   return p + HSIZ;
60 }
61
62 void
63 __gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark)
64 {
65   struct tmp_reentrant_t  *next;
66   
67   while (mark != NULL)
68     {
69       next = mark->next;
70       (*__gmp_free_func) ((char *) mark, mark->size);
71       mark = next;
72     }
73 }