| 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" |
| 20 | #include <assert.h> |
| 21 | |
| 22 | static std::atomic <int64> totalNumberOfAllocations = 0, totalNumberOfDeallocations = 0, totalAllocationSize = 0, |
| 23 | totalNumberOfMovingReallocs = 0, totalNumberOfReallocsInSitu = 0; |
| 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_SIZE 3'000'000 |
| 43 | static char *theRainyDayFund = nullptr; |
| 44 | |
| 45 | void Melder_alloc_init () { |
| 46 | theRainyDayFund = (char *) malloc (theRainyDayFund_SIZE); // called at application initialization, so cannot fail |
| 47 | assert (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_malloc (int64 size) { |
| 57 | if (size <= 0) |
| 58 | Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 59 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 60 | if (size > MAXIMUM_ALLOCATION_SIZE_64) |
| 61 | Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 62 | if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32) |
| 63 | Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); |
| 64 | void *result = malloc ((size_t) size); // guarded cast |
| 65 | if (! result) |
| 66 | Melder_throw (U"Out of memory: there is not enough room for another ", Melder_bigInteger (size), U" bytes."); |
| 67 | if (Melder_debug == 34) |
| 68 | Melder_casual (U"Melder_malloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); |
| 69 | totalNumberOfAllocations += 1; |
| 70 | totalAllocationSize += size; |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | void * _Melder_malloc_f (int64 size) { |
| 75 | if (size <= 0) |
| 76 | Melder_crash (U"(Melder_malloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 77 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 78 | if (size > MAXIMUM_ALLOCATION_SIZE_64) |
| 79 | Melder_crash (U"(Melder_malloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 80 | if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32) |
| 81 | Melder_crash (U"(Melder_malloc_f:) Cannot allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); |
| 82 | void *result = malloc ((size_t) size); |
| 83 | if (! result) { |
| 84 | if (theRainyDayFund) { |
| 85 | free (theRainyDayFund); |
| 86 | theRainyDayFund = nullptr; |
| 87 | } |
| 88 | result = malloc ((size_t) size); |
| 89 | if (result) |
| 90 | Melder_flushError (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_crash (U"Out of memory: there is not enough room for another ", Melder_bigInteger (size), U" bytes."); |
| 93 | } |
| 94 | totalNumberOfAllocations += 1; |
| 95 | totalAllocationSize += size; |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | void _Melder_free (void **ptr) noexcept { |
| 100 | if (! *ptr) |
| 101 | return; |
| 102 | if (Melder_debug == 34) |
| 103 | Melder_casual (U"Melder_free\t", Melder_pointer (*ptr), U"\t?\t?"); |
| 104 | free (*ptr); |
| 105 | *ptr = nullptr; |
| 106 | totalNumberOfDeallocations += 1; |
| 107 | } |
| 108 | |
| 109 | void * Melder_realloc (void *ptr, int64 size) { |
| 110 | if (size <= 0) |
| 111 | Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 112 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 113 | if (size > MAXIMUM_ALLOCATION_SIZE_64) |
| 114 | Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 115 | if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32) |
| 116 | Melder_throw (U"Cannot allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); |
| 117 | void *result = realloc (ptr, (size_t) size); // will not show in the statistics... |
| 118 | if (! result) |
| 119 | Melder_throw (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes."); |
| 120 | if (! ptr) { // is it like malloc? |
| 121 | if (Melder_debug == 34) |
| 122 | Melder_casual (U"Melder_realloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); |
| 123 | totalNumberOfAllocations += 1; |
| 124 | totalAllocationSize += size; |
| 125 | } else if (result != ptr) { // did realloc do a malloc-and-free? |
| 126 | totalNumberOfAllocations += 1; |
| 127 | totalAllocationSize += size; |
| 128 | totalNumberOfDeallocations += 1; |
| 129 | totalNumberOfMovingReallocs += 1; |
| 130 | } else { |
| 131 | totalNumberOfReallocsInSitu += 1; |
| 132 | } |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | void * Melder_realloc_f (void *ptr, int64 size) { |
| 137 | if (size <= 0) |
| 138 | Melder_crash (U"(Melder_realloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 139 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 140 | if (size > MAXIMUM_ALLOCATION_SIZE_64) |
| 141 | Melder_crash (U"(Melder_realloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); |
| 142 | if (sizeof (size_t) < 8 && size > MAXIMUM_ALLOCATION_SIZE_32) |
| 143 | Melder_crash (U"(Melder_realloc_f:) Cannot allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); |
| 144 | void *result = realloc (ptr, (size_t) size); // will not show in the statistics... |
| 145 | if (! result) { |
| 146 | if (theRainyDayFund) { |
| 147 | free (theRainyDayFund); |
| 148 | theRainyDayFund = nullptr; |
| 149 | } |
| 150 | result = realloc (ptr, (size_t) size); |
| 151 | if (result) |
| 152 | Melder_flushError (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_crash (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes."); |
| 155 | } |
| 156 | if (! ptr) { // is it like malloc? |
| 157 | totalNumberOfAllocations += 1; |
| 158 | totalAllocationSize += size; |
| 159 | } else if (result != ptr) { // did realloc do a malloc-and-free? |
| 160 | totalNumberOfAllocations += 1; |
| 161 | totalAllocationSize += size; |
| 162 | totalNumberOfDeallocations += 1; |
| 163 | totalNumberOfMovingReallocs += 1; |
| 164 | } else { |
| 165 | totalNumberOfReallocsInSitu += 1; |
| 166 | } |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | void * _Melder_calloc (int64 nelem, int64 elsize) { |
| 171 | if (nelem <= 0) |
| 172 | Melder_throw (U"Can never allocate ", Melder_bigInteger (nelem), U" elements."); |
| 173 | if (elsize <= 0) |
| 174 | Melder_throw (U"Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes."); |
| 175 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 176 | if ((uint64) nelem > MAXIMUM_ALLOCATION_SIZE_64 / (uint64) elsize) // guarded casts to unsigned |
| 177 | Melder_throw (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 && (uint64) nelem > MAXIMUM_ALLOCATION_SIZE_32 / (uint64) elsize) |
| 180 | Melder_throw (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 = calloc ((size_t) nelem, (size_t) elsize); |
| 183 | if (! result) |
| 184 | Melder_throw (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_debug == 34) |
| 186 | Melder_casual (U"Melder_calloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (nelem), U"\t", Melder_bigInteger (elsize)); |
| 187 | totalNumberOfAllocations += 1; |
| 188 | totalAllocationSize += nelem * elsize; |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | void * _Melder_calloc_f (int64 nelem, int64 elsize) { |
| 193 | if (nelem <= 0) |
| 194 | Melder_crash (U"(Melder_calloc_f:) Can never allocate ", Melder_bigInteger (nelem), U" elements."); |
| 195 | if (elsize <= 0) |
| 196 | Melder_crash (U"(Melder_calloc_f:) Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes."); |
| 197 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 198 | if ((uint64) nelem > MAXIMUM_ALLOCATION_SIZE_64 / (uint64) elsize) // guarded casts to unsigned |
| 199 | Melder_crash (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 && (uint64) nelem > MAXIMUM_ALLOCATION_SIZE_32 / (uint64) elsize) |
| 202 | Melder_crash (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 = calloc ((size_t) nelem, (size_t) elsize); |
| 205 | if (! result) { |
| 206 | if (theRainyDayFund) { |
| 207 | free (theRainyDayFund); |
| 208 | theRainyDayFund = nullptr; |
| 209 | } |
| 210 | result = calloc ((size_t) nelem, (size_t) elsize); |
| 211 | if (result) |
| 212 | Melder_flushError (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_crash (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 | totalNumberOfAllocations += 1; |
| 218 | totalAllocationSize += nelem * elsize; |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | autostring32 Melder_dup (conststring32 string /* cattable */) { |
| 223 | if (! string) |
| 224 | return autostring32(); |
| 225 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 226 | const integer numberOfCharactersToCopy = Melder_length (string); |
| 227 | Melder_assert (numberOfCharactersToCopy >= 0); |
| 228 | Melder_assert (numberOfCharactersToCopy < MAXIMUM_ALLOCATION_SIZE); // there should be no way to get longer strings into Praat |
| 229 | const int64 requestedBufferSize = (int64) numberOfCharactersToCopy + 1; |
| 230 | if (requestedBufferSize > (int64) MAXIMUM_ALLOCATION_SIZE_64 / (int64) sizeof (char32)) |
| 231 | Melder_throw (U"Can never allocate ", Melder_bigInteger (requestedBufferSize), U" characters."); |
| 232 | if (sizeof (size_t) < 8 && requestedBufferSize > (int64) MAXIMUM_ALLOCATION_SIZE_32 / (int64) sizeof (char32)) |
| 233 | Melder_throw (U"Cannot allocate ", Melder_bigInteger (requestedBufferSize), |
| 234 | U" characters. Use a 64-bit edition of Praat instead?"); |
| 235 | autostring32 result (numberOfCharactersToCopy, false); // guarded conversion |
| 236 | str32cpy (result.get(), string); |
| 237 | if (Melder_debug == 34) |
| 238 | Melder_casual (U"Melder_dup\t", Melder_pointer (result.get()), U"\t", |
| 239 | Melder_bigInteger (requestedBufferSize), U"\t", sizeof (char32)); |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | autostring32 Melder_ndup (conststring32 string /* cattable */, const integer numberOfCharactersToCopy) { |
| 244 | if (! string) |
| 245 | return autostring32(); |
| 246 | const int64 requestedBufferSize = (int64) numberOfCharactersToCopy + 1; |
| 247 | if (sizeof (size_t) < 8 && requestedBufferSize > SIZE_MAX / sizeof (char32)) |
| 248 | Melder_throw (U"Can never allocate ", Melder_bigInteger (requestedBufferSize), |
| 249 | U" characters. Use a 64-bit edition of Praat instead?"); |
| 250 | autostring32 result (numberOfCharactersToCopy, false); // guarded conversion |
| 251 | str32ncpy (result.get(), string, numberOfCharactersToCopy); |
| 252 | result [numberOfCharactersToCopy] = U'\0'; |
| 253 | if (Melder_debug == 34) |
| 254 | Melder_casual (U"Melder_ndup\t", Melder_pointer (result.get()), U"\t", |
| 255 | Melder_bigInteger (requestedBufferSize), U"\t", sizeof (char32)); |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | autostring32 Melder_dup_f (conststring32 string /* cattable */) { |
| 260 | if (! string) |
| 261 | return autostring32(); |
| 262 | static_assert (MAXIMUM_ALLOCATION_SIZE < SIZE_MAX); |
| 263 | const integer numberOfCharactersToCopy = Melder_length (string); |
| 264 | Melder_assert (numberOfCharactersToCopy >= 0); |
| 265 | Melder_assert (numberOfCharactersToCopy < MAXIMUM_ALLOCATION_SIZE); // there should be no way to get longer strings into Praat |
| 266 | const int64 requestedBufferSize = (int64) numberOfCharactersToCopy + 1; |
| 267 | if (requestedBufferSize > (int64) MAXIMUM_ALLOCATION_SIZE_64 / (int64) sizeof (char32)) |
| 268 | Melder_crash (U"(Melder_dup_f:) Can never allocate ", Melder_bigInteger (requestedBufferSize), U" characters."); |
| 269 | if (sizeof (size_t) < 8 && requestedBufferSize > (int64) MAXIMUM_ALLOCATION_SIZE_32 / (int64) sizeof (char32)) |
| 270 | Melder_crash (U"(Melder_dup_f:) Cannot allocate ", Melder_bigInteger (requestedBufferSize), |
| 271 | U" characters. Use a 64-bit edition of Praat instead?"); |
| 272 | autostring32 result (numberOfCharactersToCopy, true); |
| 273 | str32cpy (result.get(), string); |
| 274 | return result; |
| 275 | } |
| 276 | |
| 277 | int64 Melder_allocationCount () { |
| 278 | return totalNumberOfAllocations; |
| 279 | } |
| 280 | |
| 281 | int64 Melder_deallocationCount () { |
| 282 | return totalNumberOfDeallocations; |
| 283 | } |
| 284 | |
| 285 | int64 Melder_allocationSize () { |
| 286 | return totalAllocationSize; |
| 287 | } |
| 288 | |
| 289 | int64 Melder_reallocationsInSituCount () { |
| 290 | return totalNumberOfReallocsInSitu; |
| 291 | } |
| 292 | |
| 293 | int64 Melder_movingReallocationsCount () { |
| 294 | return totalNumberOfMovingReallocs; |
| 295 | } |
| 296 | |
| 297 | #pragma mark - Generic memory functions for vectors and matrices |
| 298 | |
| 299 | namespace MelderArray { // reopen |
| 300 | std::atomic <int64> allocationCount = 0, deallocationCount = 0; |
| 301 | std::atomic <int64> cellAllocationCount = 0, cellDeallocationCount = 0; |
| 302 | } |
| 303 | |
| 304 | int64 MelderArray_allocationCount () { return MelderArray :: allocationCount; } |
| 305 | int64 MelderArray_deallocationCount () { return MelderArray :: deallocationCount; } |
| 306 | int64 MelderArray_cellAllocationCount () { return MelderArray :: cellAllocationCount; } |
| 307 | int64 MelderArray_cellDeallocationCount () { return MelderArray :: cellDeallocationCount; } |
| 308 | |
| 309 | byte * MelderArray:: _alloc_generic (integer cellSize, integer numberOfCells, kInitializationType initializationType) { |
| 310 | try { |
| 311 | if (numberOfCells <= 0) |
| 312 | return nullptr; // not an error |
| 313 | byte *result = ( initializationType == kInitializationType :: ZERO ? |
| 314 | reinterpret_cast <byte *> (_Melder_calloc (numberOfCells, cellSize)) : |
| 315 | reinterpret_cast <byte *> (_Melder_malloc (numberOfCells * cellSize)) ); |
| 316 | MelderArray::allocationCount += 1; |
| 317 | MelderArray::cellAllocationCount += numberOfCells; |
| 318 | return result; |
| 319 | } catch (MelderError) { |
| 320 | Melder_throw (U"Tensor of ", numberOfCells, U" cells not created."); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void MelderArray:: _free_generic (byte *cells, integer numberOfCells) noexcept { |
| 325 | if (! cells) |
| 326 | return; // not an error |
| 327 | Melder_free (cells); |
| 328 | MelderArray::deallocationCount += 1; |
| 329 | MelderArray::cellDeallocationCount += numberOfCells; |
| 330 | } |
| 331 | |
| 332 | /* End of file melder_alloc.cpp */ |