Debian Codegraph @

melder/melder_alloc.cpp

raw symbols SCIP
1 /* melder_alloc.cpp
2 *
3 * Copyright (C) 1992-2007,2009,2011,2012,2014-2020,2022-2025 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
20 #include <assert.h>
21
22 static stdcxx . . $ std/::atomiccxx . . $ std/atomic# <int64cxx . . $ int64#> totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c)., totalNumberOfDeallocationscxx . . $ totalNumberOfDeallocations. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c)., totalAllocationSizecxx . . $ totalAllocationSize. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c).,
23 totalNumberOfMovingReallocscxx . . $ totalNumberOfMovingReallocs. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c)., totalNumberOfReallocsInSitucxx . . $ totalNumberOfReallocsInSitu. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c).;
24
25 /*
26 * The rainy-day fund.
27 *
28 * Typically, memory allocation for data is entirely checked by using the normal versions of the allocation routines,
29 * which will call Melder_error if they are out of memory.
30 * When data allocation is indeed out of memory,
31 * the application will present an error message to the user saying that the data could not be created.
32 *
33 * By contrast, it is not practical to check the allocation of user interface elements,
34 * because the application cannot perform correctly if an interface element is missing or incorrect.
35 * For such situations, the application will typically use the _f versions of the allocation routines,
36 * which, if out of memory, will free a "rainy-day fund" and retry.
37 * In this way, the interface element can usually still be allocated;
38 * the application will present an error message telling the user to save her work and quit the application.
39 * If the user doesn't do that, the application will crash upon the next failing allocation of a _f routine.
40 */
41
42 #define theRainyDayFund_SIZEcxx . . $ `melder/melder_alloc.cpp:42:9`! 3'000'000
43 static char *theRainyDayFundcxx . . $ theRainyDayFund. = nullptr;
44
45 void Melder_alloc_initcxx . . $ Melder_alloc_init(49f6e7a06ebc5aa8). () {
46 theRainyDayFundcxx . . $ theRainyDayFund. = (char *) malloccxx . . $ malloc(9f008b2a9698428b). (theRainyDayFund_SIZEcxx . . $ `melder/melder_alloc.cpp:42:9`!); // called at application initialization, so cannot fail
47 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`! (theRainyDayFund);
48 }
49
50 /*
51 The following functions take int64 arguments even on 32-bit machines.
52 This is because it is easy for the user to request objects that do not fit in memory
53 on 32-bit machines, in which case an appropriate error message is required.
54 */
55
56 void * _Melder_malloccxx . . $ _Melder_malloc(1de1fe46563a6a2a). (int64cxx . . $ int64# size) {
57 if (size <= 0)
58 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (size), U" bytes.");
59 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
60 if (size > MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`!)
61 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (size), U" bytes.");
62 if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`!)
63 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?");
64 void *result = malloccxx . . $ malloc(9f008b2a9698428b). ((size_t) size); // guarded cast
65 if (! result)
66 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Out of memory: there is not enough room for another ", Melder_bigInteger (size), U" bytes.");
67 if (Melder_debugcxx . . $ Melder_debug. == 34)
68 Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Melder_malloc\t", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (result), U"\t", Melder_bigIntegercxx . . $ Melder_bigInteger(7b348ebc2004441d). (size), U"\t1");
69 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
70 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). size;
71 return result;
72 }
73
74 void * _Melder_malloc_fcxx . . $ _Melder_malloc_f(1de1fe46563a6a2a). (int64cxx . . $ int64# size) {
75 if (size <= 0)
76 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_malloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes.");
77 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
78 if (size > MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`!)
79 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_malloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes.");
80 if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`!)
81 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_malloc_f:) Cannot allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?");
82 void *result = malloccxx . . $ malloc(9f008b2a9698428b). ((size_t) size);
83 if (! result) {
84 if (theRainyDayFundcxx . . $ theRainyDayFund.) {
85 freecxx . . $ free(bd21765a0afc8e3c). (theRainyDayFundcxx . . $ theRainyDayFund.);
86 theRainyDayFundcxx . . $ theRainyDayFund. = nullptr;
87 }
88 result = malloccxx . . $ malloc(9f008b2a9698428b). ((size_t) size);
89 if (result)
90 Melder_flushErrorcxx . . $ Melder_flushError(6298c93241566110). (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash.");
91 else
92 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Out of memory: there is not enough room for another ", Melder_bigInteger (size), U" bytes.");
93 }
94 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
95 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). size;
96 return result;
97 }
98
99 void _Melder_freecxx . . $ _Melder_free(66f733d91e158b15). (void **ptr) noexcept {
100 if (! *ptr)
101 return;
102 if (Melder_debugcxx . . $ Melder_debug. == 34)
103 Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Melder_free\t", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (*ptr), U"\t?\t?");
104 freecxx . . $ free(bd21765a0afc8e3c). (*ptr);
105 *ptr = nullptr;
106 totalNumberOfDeallocationscxx . . $ totalNumberOfDeallocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
107 }
108
109 void * Melder_realloccxx . . $ Melder_realloc(d42fd80046fa8ace). (void *ptr, int64cxx . . $ int64# size) {
110 if (size <= 0)
111 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (size), U" bytes.");
112 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
113 if (size > MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`!)
114 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (size), U" bytes.");
115 if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`!)
116 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?");
117 void *result = realloccxx . . $ realloc(42b2a9dd8f084bf2). (ptr, (size_t) size); // will not show in the statistics...
118 if (! result)
119 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes.");
120 if (! ptr) { // is it like malloc?
121 if (Melder_debugcxx . . $ Melder_debug. == 34)
122 Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Melder_realloc\t", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (result), U"\t", Melder_bigIntegercxx . . $ Melder_bigInteger(7b348ebc2004441d). (size), U"\t1");
123 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
124 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). size;
125 } else if (result != ptr) { // did realloc do a malloc-and-free?
126 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
127 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). size;
128 totalNumberOfDeallocationscxx . . $ totalNumberOfDeallocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
129 totalNumberOfMovingReallocscxx . . $ totalNumberOfMovingReallocs. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
130 } else {
131 totalNumberOfReallocsInSitucxx . . $ totalNumberOfReallocsInSitu. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
132 }
133 return result;
134 }
135
136 void * Melder_realloc_fcxx . . $ Melder_realloc_f(d42fd80046fa8ace). (void *ptr, int64cxx . . $ int64# size) {
137 if (size <= 0)
138 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_realloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes.");
139 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
140 if (size > MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`!)
141 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_realloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes.");
142 if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`!)
143 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_realloc_f:) Cannot allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?");
144 void *result = realloccxx . . $ realloc(42b2a9dd8f084bf2). (ptr, (size_t) size); // will not show in the statistics...
145 if (! result) {
146 if (theRainyDayFundcxx . . $ theRainyDayFund.) {
147 freecxx . . $ free(bd21765a0afc8e3c). (theRainyDayFundcxx . . $ theRainyDayFund.);
148 theRainyDayFundcxx . . $ theRainyDayFund. = nullptr;
149 }
150 result = realloccxx . . $ realloc(42b2a9dd8f084bf2). (ptr, (size_t) size);
151 if (result)
152 Melder_flushErrorcxx . . $ Melder_flushError(6298c93241566110). (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash.");
153 else
154 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes.");
155 }
156 if (! ptr) { // is it like malloc?
157 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
158 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). size;
159 } else if (result != ptr) { // did realloc do a malloc-and-free?
160 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
161 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). size;
162 totalNumberOfDeallocationscxx . . $ totalNumberOfDeallocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
163 totalNumberOfMovingReallocscxx . . $ totalNumberOfMovingReallocs. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
164 } else {
165 totalNumberOfReallocsInSitucxx . . $ totalNumberOfReallocsInSitu. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
166 }
167 return result;
168 }
169
170 void * _Melder_calloccxx . . $ _Melder_calloc(d58a73cf1dc24cbb). (int64cxx . . $ int64# nelem, int64cxx . . $ int64# elsize) {
171 if (nelem <= 0)
172 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (nelem), U" elements.");
173 if (elsize <= 0)
174 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes.");
175 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
176 if ((uint64cxx . . $ uint64#) nelem > MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`! / (uint64cxx . . $ uint64#) elsize) // guarded casts to unsigned
177 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ",
178 Melder_bigInteger (elsize), U" bytes each.");
179 if (sizeof (size_t) < 8 && (uint64cxx . . $ uint64#) nelem > MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`! / (uint64cxx . . $ uint64#) elsize)
180 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ",
181 Melder_bigInteger (elsize), U" bytes each. Use a 64-bit edition of Praat instead?");
182 void *result = calloccxx . . $ calloc(95acc9b79bb49189). ((size_t) nelem, (size_t) elsize);
183 if (! result)
184 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Out of memory: there is not enough room for ", Melder_bigInteger (nelem), U" more elements whose sizes are ", elsize, U" bytes each.");
185 if (Melder_debugcxx . . $ Melder_debug. == 34)
186 Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Melder_calloc\t", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (result), U"\t", Melder_bigIntegercxx . . $ Melder_bigInteger(7b348ebc2004441d). (nelem), U"\t", Melder_bigIntegercxx . . $ Melder_bigInteger(7b348ebc2004441d). (elsize));
187 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
188 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). nelem * elsize;
189 return result;
190 }
191
192 void * _Melder_calloc_fcxx . . $ _Melder_calloc_f(d58a73cf1dc24cbb). (int64cxx . . $ int64# nelem, int64cxx . . $ int64# elsize) {
193 if (nelem <= 0)
194 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_calloc_f:) Can never allocate ", Melder_bigInteger (nelem), U" elements.");
195 if (elsize <= 0)
196 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_calloc_f:) Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes.");
197 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
198 if ((uint64cxx . . $ uint64#) nelem > MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`! / (uint64cxx . . $ uint64#) elsize) // guarded casts to unsigned
199 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Can never allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ",
200 Melder_bigInteger (elsize), U" bytes each.");
201 if (sizeof (size_t) < 8 && (uint64cxx . . $ uint64#) nelem > MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`! / (uint64cxx . . $ uint64#) elsize)
202 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Cannot allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ",
203 Melder_bigInteger (elsize), U" bytes each. Use a 64-bit edition of Praat instead?");
204 void *result = calloccxx . . $ calloc(95acc9b79bb49189). ((size_t) nelem, (size_t) elsize);
205 if (! result) {
206 if (theRainyDayFundcxx . . $ theRainyDayFund.) {
207 freecxx . . $ free(bd21765a0afc8e3c). (theRainyDayFundcxx . . $ theRainyDayFund.);
208 theRainyDayFundcxx . . $ theRainyDayFund. = nullptr;
209 }
210 result = calloccxx . . $ calloc(95acc9b79bb49189). ((size_t) nelem, (size_t) elsize);
211 if (result)
212 Melder_flushErrorcxx . . $ Melder_flushError(6298c93241566110). (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash.");
213 else
214 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"Out of memory: there is not enough room for ", Melder_bigInteger (nelem),
215 U" more elements whose sizes are ", Melder_bigInteger (elsize), U" bytes each.");
216 }
217 totalNumberOfAllocationscxx . . $ totalNumberOfAllocations. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
218 totalAllocationSizecxx . . $ totalAllocationSize. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). nelem * elsize;
219 return result;
220 }
221
222 autostring32cxx . . $ autostring32# Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (conststring32cxx . . $ conststring32# string /* cattable */) {
223 if (! string)
224 return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).();
225 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
226 const integercxx . . $ integer# numberOfCharactersToCopy = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string);
227 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numberOfCharactersToCopy >= 0);
228 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numberOfCharactersToCopy < MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`!); // there should be no way to get longer strings into Praat
229 const int64cxx . . $ int64# requestedBufferSize = (int64cxx . . $ int64#) numberOfCharactersToCopy + 1;
230 if (requestedBufferSize > (int64cxx . . $ int64#) MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`! / (int64cxx . . $ int64#) sizeof (char32cxx . . $ char32#))
231 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (requestedBufferSize), U" characters.");
232 if (sizeof (size_t) < 8 && requestedBufferSize > (int64cxx . . $ int64#) MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`! / (int64cxx . . $ int64#) sizeof (char32cxx . . $ char32#))
233 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot allocate ", Melder_bigInteger (requestedBufferSize),
234 U" characters. Use a 64-bit edition of Praat instead?");
235 autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (numberOfCharactersToCopy, false); // guarded conversion
236 str32cpycxx . . $ str32cpy(6ab934322573b179). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), string);
237 if (Melder_debugcxx . . $ Melder_debug. == 34)
238 Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Melder_dup\t", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()), U"\t",
239 Melder_bigIntegercxx . . $ Melder_bigInteger(7b348ebc2004441d). (requestedBufferSize), U"\t", sizeof (char32cxx . . $ char32#));
240 return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).;
241 }
242
243 autostring32cxx . . $ autostring32# Melder_ndupcxx . . $ Melder_ndup(c02f217f21fae7fe). (conststring32cxx . . $ conststring32# string /* cattable */, const integercxx . . $ integer# numberOfCharactersToCopy) {
244 if (! string)
245 return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).();
246 const int64cxx . . $ int64# requestedBufferSize = (int64cxx . . $ int64#) numberOfCharactersToCopy + 1;
247 if (sizeof (size_t) < 8 && requestedBufferSize > SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`! / sizeof (char32cxx . . $ char32#))
248 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Can never allocate ", Melder_bigInteger (requestedBufferSize),
249 U" characters. Use a 64-bit edition of Praat instead?");
250 autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (numberOfCharactersToCopy, false); // guarded conversion
251 str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), string, numberOfCharactersToCopy);
252 result [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).numberOfCharactersToCopy] = U'\0';
253 if (Melder_debugcxx . . $ Melder_debug. == 34)
254 Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Melder_ndup\t", Melder_pointercxx . . $ Melder_pointer(a9959086b14cc90d). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()), U"\t",
255 Melder_bigIntegercxx . . $ Melder_bigInteger(7b348ebc2004441d). (requestedBufferSize), U"\t", sizeof (char32cxx . . $ char32#));
256 return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).;
257 }
258
259 autostring32cxx . . $ autostring32# Melder_dup_fcxx . . $ Melder_dup_f(87bf156b1f548402). (conststring32cxx . . $ conststring32# string /* cattable */) {
260 if (! string)
261 return autostring32cxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).();
262 static_assert (MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! < SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!);
263 const integercxx . . $ integer# numberOfCharactersToCopy = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (string);
264 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numberOfCharactersToCopy >= 0);
265 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numberOfCharactersToCopy < MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`!); // there should be no way to get longer strings into Praat
266 const int64cxx . . $ int64# requestedBufferSize = (int64cxx . . $ int64#) numberOfCharactersToCopy + 1;
267 if (requestedBufferSize > (int64cxx . . $ int64#) MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`! / (int64cxx . . $ int64#) sizeof (char32cxx . . $ char32#))
268 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_dup_f:) Can never allocate ", Melder_bigInteger (requestedBufferSize), U" characters.");
269 if (sizeof (size_t) < 8 && requestedBufferSize > (int64cxx . . $ int64#) MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`! / (int64cxx . . $ int64#) sizeof (char32cxx . . $ char32#))
270 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"(Melder_dup_f:) Cannot allocate ", Melder_bigInteger (requestedBufferSize),
271 U" characters. Use a 64-bit edition of Praat instead?");
272 autostring32cxx . . $ autostring32# resultcxx . . $ _autostring#_autostring(f153f132b2bc41c2). (numberOfCharactersToCopy, true);
273 str32cpycxx . . $ str32cpy(6ab934322573b179). (result.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), string);
274 return resultcxx . . $ _autostring#_autostring(39021ecceedcc7d2).;
275 }
276
277 int64cxx . . $ int64# Melder_allocationCountcxx . . $ Melder_allocationCount(da6335ded91d79c2). () {
278 return totalNumberOfAllocationscxx . . $ totalNumberOfAllocations.;
279 }
280
281 int64cxx . . $ int64# Melder_deallocationCountcxx . . $ Melder_deallocationCount(da6335ded91d79c2). () {
282 return totalNumberOfDeallocationscxx . . $ totalNumberOfDeallocations.;
283 }
284
285 int64cxx . . $ int64# Melder_allocationSizecxx . . $ Melder_allocationSize(da6335ded91d79c2). () {
286 return totalAllocationSizecxx . . $ totalAllocationSize.;
287 }
288
289 int64cxx . . $ int64# Melder_reallocationsInSituCountcxx . . $ Melder_reallocationsInSituCount(da6335ded91d79c2). () {
290 return totalNumberOfReallocsInSitucxx . . $ totalNumberOfReallocsInSitu.;
291 }
292
293 int64cxx . . $ int64# Melder_movingReallocationsCountcxx . . $ Melder_movingReallocationsCount(da6335ded91d79c2). () {
294 return totalNumberOfMovingReallocscxx . . $ totalNumberOfMovingReallocs.;
295 }
296
297 #pragma mark - Generic memory functions for vectors and matrices
298
299 namespace MelderArraycxx . . $ MelderArray/ { // reopen
300 stdcxx . . $ std/::atomiccxx . . $ std/atomic# <int64cxx . . $ int64#> allocationCountcxx . . $ MelderArray/allocationCount. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c)., deallocationCountcxx . . $ MelderArray/deallocationCount. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c).;
301 stdcxx . . $ std/::atomiccxx . . $ std/atomic# <int64cxx . . $ int64#> cellAllocationCountcxx . . $ MelderArray/cellAllocationCount. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c)., cellDeallocationCountcxx . . $ MelderArray/cellDeallocationCount. = 0cxx . . $ std/atomic#atomic(f1ee9d71b26a60c).;
302 }
303
304 int64cxx . . $ int64# MelderArray_allocationCountcxx . . $ MelderArray_allocationCount(da6335ded91d79c2). () { return MelderArraycxx . . $ MelderArray/ :: allocationCountcxx . . $ MelderArray/allocationCount.; }
305 int64cxx . . $ int64# MelderArray_deallocationCountcxx . . $ MelderArray_deallocationCount(da6335ded91d79c2). () { return MelderArraycxx . . $ MelderArray/ :: deallocationCountcxx . . $ MelderArray/deallocationCount.; }
306 int64cxx . . $ int64# MelderArray_cellAllocationCountcxx . . $ MelderArray_cellAllocationCount(da6335ded91d79c2). () { return MelderArraycxx . . $ MelderArray/ :: cellAllocationCountcxx . . $ MelderArray/cellAllocationCount.; }
307 int64cxx . . $ int64# MelderArray_cellDeallocationCountcxx . . $ MelderArray_cellDeallocationCount(da6335ded91d79c2). () { return MelderArraycxx . . $ MelderArray/ :: cellDeallocationCountcxx . . $ MelderArray/cellDeallocationCount.; }
308
309 bytecxx . . $ byte# * MelderArraycxx . . $ MelderArray/:: _alloc_genericcxx . . $ MelderArray/_alloc_generic(2491babf9587f48b). (integercxx . . $ integer# cellSize, integercxx . . $ integer# numberOfCells, kInitializationTypecxx . . $ MelderArray/kInitializationType# initializationType) {
310 try {
311 if (numberOfCells <= 0)
312 return nullptr; // not an error
313 bytecxx . . $ byte# *result = ( initializationType == kInitializationTypecxx . . $ MelderArray/kInitializationType# :: ZEROcxx . . $ MelderArray/kInitializationType#ZERO. ?
314 reinterpret_cast <bytecxx . . $ byte# *> (_Melder_calloccxx . . $ _Melder_calloc(d58a73cf1dc24cbb). (numberOfCells, cellSize)) :
315 reinterpret_cast <bytecxx . . $ byte# *> (_Melder_malloccxx . . $ _Melder_malloc(1de1fe46563a6a2a). (numberOfCells * cellSize)) );
316 MelderArraycxx . . $ MelderArray/::allocationCountcxx . . $ MelderArray/allocationCount. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
317 MelderArraycxx . . $ MelderArray/::cellAllocationCountcxx . . $ MelderArray/cellAllocationCount. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). numberOfCells;
318 return result;
319 } catch (MelderErrorcxx . . $ MelderError#) {
320 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Tensor of ", numberOfCells, U" cells not created.");
321 }
322 }
323
324 void MelderArraycxx . . $ MelderArray/:: _free_genericcxx . . $ MelderArray/_free_generic(4434d2f5730ad69). (bytecxx . . $ byte# *cells, integercxx . . $ integer# numberOfCells) noexcept {
325 if (! cells)
326 return; // not an error
327 Melder_freecxx . . $ `melder/melder_alloc.h:38:9`! (cells);
328 MelderArraycxx . . $ MelderArray/::deallocationCountcxx . . $ MelderArray/deallocationCount. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). 1;
329 MelderArraycxx . . $ MelderArray/::cellDeallocationCountcxx . . $ MelderArray/cellDeallocationCount. +=cxx . . $ std/__atomic_base#`operator+=`(73de6b30e1938209). numberOfCells;
330 }
331
332 /* End of file melder_alloc.cpp */