source: trunk/third/gmp/tal-reent.c @ 18191

Revision 18191, 2.2 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* TMP_ALLOC routines using malloc in a reentrant fashion.
2
3Copyright 2000, 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, 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
46void *
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
62void
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}
Note: See TracBrowser for help on using the repository browser.