1 /* Memory allocation routines.
2
3 Copyright 1991, 1993, 1994, 2000-2002, 2012 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 <stdlib.h> /* for malloc, realloc, free */
33
34 #include "gmp-impl.h"
35
36
37 void * (*__gmp_allocate_func) (size_t) = __gmp_default_allocate;
38 void * (*__gmp_reallocate_func) (void *, size_t, size_t) = __gmp_default_reallocate;
39 void (*__gmp_free_func) (void *, size_t) = __gmp_default_free;
40
41
42 /* Default allocation functions. In case of failure to allocate/reallocate
43 an error message is written to stderr and the program aborts. */
44
45 void *
__gmp_default_allocate(size_t size)46 __gmp_default_allocate (size_t size)
47 {
48 void *ret;
49 #ifdef DEBUG
50 size_t req_size = size;
51 size += 2 * GMP_LIMB_BYTES;
52 #endif
53 ret = malloc (size);
54 if (ret == 0)
55 {
56 fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
57 abort ();
58 }
59
60 #ifdef DEBUG
61 {
62 mp_ptr p = ret;
63 p++;
64 p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
65 if (req_size % GMP_LIMB_BYTES == 0)
66 p[req_size / GMP_LIMB_BYTES] = ~((0xdeadbeef << 31) + 0xdeafdeed);
67 ret = p;
68 }
69 #endif
70 return ret;
71 }
72
73 void *
__gmp_default_reallocate(void * oldptr,size_t old_size,size_t new_size)74 __gmp_default_reallocate (void *oldptr, size_t old_size, size_t new_size)
75 {
76 void *ret;
77
78 #ifdef DEBUG
79 size_t req_size = new_size;
80
81 if (old_size != 0)
82 {
83 mp_ptr p = oldptr;
84 if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
85 {
86 fprintf (stderr, "gmp: (realloc) data clobbered before allocation block\n");
87 abort ();
88 }
89 if (old_size % GMP_LIMB_BYTES == 0)
90 if (p[old_size / GMP_LIMB_BYTES] != ~((0xdeadbeef << 31) + 0xdeafdeed))
91 {
92 fprintf (stderr, "gmp: (realloc) data clobbered after allocation block\n");
93 abort ();
94 }
95 oldptr = p - 1;
96 }
97
98 new_size += 2 * GMP_LIMB_BYTES;
99 #endif
100
101 ret = realloc (oldptr, new_size);
102 if (ret == 0)
103 {
104 fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
105 abort ();
106 }
107
108 #ifdef DEBUG
109 {
110 mp_ptr p = ret;
111 p++;
112 p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
113 if (req_size % GMP_LIMB_BYTES == 0)
114 p[req_size / GMP_LIMB_BYTES] = ~((0xdeadbeef << 31) + 0xdeafdeed);
115 ret = p;
116 }
117 #endif
118 return ret;
119 }
120
121 void
__gmp_default_free(void * blk_ptr,size_t blk_size)122 __gmp_default_free (void *blk_ptr, size_t blk_size)
123 {
124 #ifdef DEBUG
125 {
126 mp_ptr p = blk_ptr;
127 if (blk_size != 0)
128 {
129 if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
130 {
131 fprintf (stderr, "gmp: (free) data clobbered before allocation block\n");
132 abort ();
133 }
134 if (blk_size % GMP_LIMB_BYTES == 0)
135 if (p[blk_size / GMP_LIMB_BYTES] != ~((0xdeadbeef << 31) + 0xdeafdeed))
136 {
137 fprintf (stderr, "gmp: (free) data clobbered after allocation block\n");
138 abort ();
139 }
140 }
141 blk_ptr = p - 1;
142 }
143 #endif
144 free (blk_ptr);
145 }
146