Line | Count | Source (jump to first uncovered line) |
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 * |
46 | | __gmp_default_allocate (size_t size) |
47 | 0 | { |
48 | 0 | void *ret; |
49 | | #ifdef DEBUG |
50 | | size_t req_size = size; |
51 | | size += 2 * GMP_LIMB_BYTES; |
52 | | #endif |
53 | 0 | ret = malloc (size); |
54 | 0 | if (ret == 0) |
55 | 0 | { |
56 | 0 | fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size); |
57 | 0 | abort (); |
58 | 0 | } |
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 | 0 | return ret; |
71 | 0 | } |
72 | | |
73 | | void * |
74 | | __gmp_default_reallocate (void *oldptr, size_t old_size, size_t new_size) |
75 | 0 | { |
76 | 0 | 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 | 0 | ret = realloc (oldptr, new_size); |
102 | 0 | if (ret == 0) |
103 | 0 | { |
104 | 0 | fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size); |
105 | 0 | abort (); |
106 | 0 | } |
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 | 0 | return ret; |
119 | 0 | } |
120 | | |
121 | | void |
122 | | __gmp_default_free (void *blk_ptr, size_t blk_size) |
123 | 0 | { |
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 | 0 | free (blk_ptr); |
145 | 0 | } |