diff options
author | Kevin Ryde <user42@zip.com.au> | 2001-06-19 22:35:50 +0200 |
---|---|---|
committer | Kevin Ryde <user42@zip.com.au> | 2001-06-19 22:35:50 +0200 |
commit | 82528f46ece4b8ef8cf1a1fe370349ea6301b2c0 (patch) | |
tree | 4ae6d9d059e40f8c8370f1270b7d07d73fdebb75 /tal-reent.c | |
parent | d43f043363445d80bb7032144f4a1dbac351a1e3 (diff) | |
download | gmp-82528f46ece4b8ef8cf1a1fe370349ea6301b2c0.tar.gz |
* tal-reent.c: New file.
By the way, "tal" stands for temporary alloc. Pretty dumb name
really, but tmp-reent.c might look like its a temporary file, not a
file for temporary allocs!
Diffstat (limited to 'tal-reent.c')
-rw-r--r-- | tal-reent.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tal-reent.c b/tal-reent.c new file mode 100644 index 000000000..003f264ae --- /dev/null +++ b/tal-reent.c @@ -0,0 +1,73 @@ +/* TMP_ALLOC routines using malloc in a reentrant fashion. + +Copyright 2000, 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include <stdio.h> +#include "gmp.h" +#include "gmp-impl.h" + + +/* Each TMP_ALLOC uses __gmp_allocate_func to get a block of memory of the + size requested, plus a header at the start which is used to hold the + blocks on a linked list in the marker variable, ready for TMP_FREE to + release. + + Callers should try to do multiple allocs with one call, in the style of + TMP_ALLOC_LIMBS_2 if it's easy to arrange, since that will keep down the + number of separate malloc calls. + + Enhancements: + + Could inline both TMP_ALLOC and TMP_FREE, though TMP_ALLOC would need the + compiler to have "inline" since it returns a value. The calls to malloc + will be slow though, so it hardly seems worth worrying about one extra + level of function call. */ + + +#define HSIZ ROUND_UP_MULTIPLE (sizeof (struct tmp_reentrant_t), __TMP_ALIGN) + +void * +__gmp_tmp_reentrant_alloc (struct tmp_reentrant_t **markp, size_t size) +{ + char *p; + size_t total_size; + +#define P ((struct tmp_reentrant_t *) p) + + total_size = size + HSIZ; + p = (*__gmp_allocate_func) (total_size); + P->size = total_size; + P->next = *markp; + *markp = P; + return p + HSIZ; +} + +void +__gmp_tmp_reentrant_free (struct tmp_reentrant_t *mark) +{ + struct tmp_reentrant_t *next; + + while (mark != NULL) + { + next = mark->next; + (*__gmp_free_func) ((char *) mark, mark->size); + mark = next; + } +} |