beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / memory.c
blob4475f37de37418d7e8b2ddbc39a04f042a912793
1 /* Memory allocation routines.
3 Copyright 1991, 1993, 1994, 2000-2002, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/. */
31 #include <stdio.h>
32 #include <stdlib.h> /* for malloc, realloc, free */
34 #include "gmp.h"
35 #include "gmp-impl.h"
38 void * (*__gmp_allocate_func) (size_t) = __gmp_default_allocate;
39 void * (*__gmp_reallocate_func) (void *, size_t, size_t) = __gmp_default_reallocate;
40 void (*__gmp_free_func) (void *, size_t) = __gmp_default_free;
43 /* Default allocation functions. In case of failure to allocate/reallocate
44 an error message is written to stderr and the program aborts. */
46 void *
47 __gmp_default_allocate (size_t size)
49 void *ret;
50 #ifdef DEBUG
51 size_t req_size = size;
52 size += 2 * GMP_LIMB_BYTES;
53 #endif
54 ret = malloc (size);
55 if (ret == 0)
57 fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
58 abort ();
61 #ifdef DEBUG
63 mp_ptr p = ret;
64 p++;
65 p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
66 if (req_size % GMP_LIMB_BYTES == 0)
67 p[req_size / GMP_LIMB_BYTES] = ~((0xdeadbeef << 31) + 0xdeafdeed);
68 ret = p;
70 #endif
71 return ret;
74 void *
75 __gmp_default_reallocate (void *oldptr, size_t old_size, size_t new_size)
77 void *ret;
79 #ifdef DEBUG
80 size_t req_size = new_size;
82 if (old_size != 0)
84 mp_ptr p = oldptr;
85 if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
87 fprintf (stderr, "gmp: (realloc) data clobbered before allocation block\n");
88 abort ();
90 if (old_size % GMP_LIMB_BYTES == 0)
91 if (p[old_size / GMP_LIMB_BYTES] != ~((0xdeadbeef << 31) + 0xdeafdeed))
93 fprintf (stderr, "gmp: (realloc) data clobbered after allocation block\n");
94 abort ();
96 oldptr = p - 1;
99 new_size += 2 * GMP_LIMB_BYTES;
100 #endif
102 ret = realloc (oldptr, new_size);
103 if (ret == 0)
105 fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
106 abort ();
109 #ifdef DEBUG
111 mp_ptr p = ret;
112 p++;
113 p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
114 if (req_size % GMP_LIMB_BYTES == 0)
115 p[req_size / GMP_LIMB_BYTES] = ~((0xdeadbeef << 31) + 0xdeafdeed);
116 ret = p;
118 #endif
119 return ret;
122 void
123 __gmp_default_free (void *blk_ptr, size_t blk_size)
125 #ifdef DEBUG
127 mp_ptr p = blk_ptr;
128 if (blk_size != 0)
130 if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
132 fprintf (stderr, "gmp: (free) data clobbered before allocation block\n");
133 abort ();
135 if (blk_size % GMP_LIMB_BYTES == 0)
136 if (p[blk_size / GMP_LIMB_BYTES] != ~((0xdeadbeef << 31) + 0xdeafdeed))
138 fprintf (stderr, "gmp: (free) data clobbered after allocation block\n");
139 abort ();
142 blk_ptr = p - 1;
144 #endif
145 free (blk_ptr);