void * Melder_realloc_f (void *ptr, int64 size) { void *result; if (size <= 0) Melder_fatal (U"(Melder_realloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_fatal (U"(Melder_realloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); result = realloc (ptr, (size_t) size); // will not show in the statistics... if (result == NULL) { if (theRainyDayFund != NULL) { free (theRainyDayFund); theRainyDayFund = NULL; } result = realloc (ptr, (size_t) size); if (result != NULL) { Melder_flushError (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash."); } else { Melder_fatal (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes."); } } if (ptr == NULL) { // is it like malloc? totalNumberOfAllocations += 1; totalAllocationSize += size; } else if (result != ptr) { // did realloc do a malloc-and-free? totalNumberOfAllocations += 1; totalAllocationSize += size; totalNumberOfDeallocations += 1; totalNumberOfMovingReallocs += 1; } else { totalNumberOfReallocsInSitu += 1; } return result; }
void * _Melder_malloc (int64 size) { if (size <= 0) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); void *result = malloc ((size_t) size); // guarded cast if (result == NULL) Melder_throw (U"Out of memory: there is not enough room for another ", Melder_bigInteger (size), U" bytes."); if (Melder_debug == 34) { Melder_casual (U"Melder_malloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
char32 * Melder_dup (const char32 *string /* cattable */) { if (! string) return NULL; int64 size = (int64) str32len (string) + 1; // guaranteed to be positive if (sizeof (size_t) < 8 && size > SIZE_MAX / sizeof (char32)) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" characters. Use a 64-bit edition of Praat instead?"); char32 *result = (char32 *) malloc ((size_t) size * sizeof (char32)); // guarded conversion if (result == NULL) Melder_throw (U"Out of memory: there is not enough room to duplicate a text of ", Melder_bigInteger (size - 1), U" characters."); str32cpy (result, string); if (Melder_debug == 34) { Melder_casual (U"Melder_dup\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t4"); } totalNumberOfAllocations += 1; totalAllocationSize += size * (int64) sizeof (char32); return result; }
char * Melder_strdup (const char *string) { if (! string) return NULL; int64 size = (int64) strlen (string) + 1; if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); char *result = (char *) malloc ((size_t) size); if (result == NULL) Melder_throw (U"Out of memory: there is not enough room to duplicate a text of ", Melder_bigInteger (size - 1), U" characters."); strcpy (result, string); if (Melder_debug == 34) { Melder_casual (U"Melder_strdup\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
Sound Sound_createAsPureTone (long numberOfChannels, double startingTime, double endTime, double sampleRate, double frequency, double amplitude, double fadeInDuration, double fadeOutDuration) { try { double numberOfSamples_f = round ((endTime - startingTime) * sampleRate); if (numberOfSamples_f > (double) INT32_MAX) Melder_throw (U"Cannot create sounds with more than ", Melder_bigInteger (INT32_MAX), U" samples, because they cannot be saved to disk."); autoSound me = Sound_create (numberOfChannels, startingTime, endTime, (long) numberOfSamples_f, 1 / sampleRate, startingTime + 0.5 / sampleRate); for (long isamp = 1; isamp <= my nx; isamp ++) { double time = my x1 + (isamp - 1) * my dx; double value = amplitude * sin (NUM2pi * frequency * time); double timeFromStart = time - startingTime; if (timeFromStart < fadeInDuration) value *= 0.5 - 0.5 * cos (NUMpi * timeFromStart / fadeInDuration); double timeFromEnd = endTime - time; if (timeFromEnd < fadeOutDuration) value *= 0.5 - 0.5 * cos (NUMpi * timeFromEnd / fadeOutDuration); for (long ichan = 1; ichan <= my ny; ichan ++) { my z [ichan] [isamp] = value; } } return me.transfer(); } catch (MelderError) { Melder_throw (U"Sound not created from tone complex."); } }
void * _Melder_calloc (int64 nelem, int64 elsize) { if (nelem <= 0) Melder_throw (U"Can never allocate ", Melder_bigInteger (nelem), U" elements."); if (elsize <= 0) Melder_throw (U"Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes."); if ((uint64_t) nelem > SIZE_MAX / (uint64_t) elsize) // guarded casts to unsigned Melder_throw (U"Can never allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ", Melder_bigInteger (elsize), U" bytes each.", sizeof (size_t) < 8 ? U" Use a 64-bit edition of Praat instead?" : NULL); void *result = calloc ((size_t) nelem, (size_t) elsize); if (result == NULL) 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."); if (Melder_debug == 34) { Melder_casual (U"Melder_calloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (nelem), U"\t", Melder_bigInteger (elsize)); } totalNumberOfAllocations += 1; totalAllocationSize += nelem * elsize; return result; }
Sound Sound_createSimple (long numberOfChannels, double duration, double samplingFrequency) { Melder_assert (duration >= 0.0); Melder_assert (samplingFrequency > 0.0); double numberOfSamples_f = round (duration * samplingFrequency); if (numberOfSamples_f > (double) INT32_MAX) Melder_throw (U"Cannot create sounds with more than ", Melder_bigInteger (INT32_MAX), U" samples, because they cannot be saved to disk."); return Sound_create (numberOfChannels, 0.0, duration, (long) (int32_t) numberOfSamples_f, 1 / samplingFrequency, 0.5 / samplingFrequency); }
void * _Melder_malloc_f (int64 size) { if (size <= 0) Melder_fatal (U"(Melder_malloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_fatal (U"(Melder_malloc_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); void *result = malloc ((size_t) size); if (result == NULL) { if (theRainyDayFund != NULL) { free (theRainyDayFund); theRainyDayFund = NULL; } result = malloc ((size_t) size); if (result != NULL) { Melder_flushError (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash."); } else { Melder_fatal (U"Out of memory: there is not enough room for another %s bytes.", Melder_bigInteger (size)); } } totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
char32 * Melder_dup_f (const char32 *string /* cattable */) { if (! string) return NULL; int64 size = (int64) str32len (string) + 1; if (sizeof (size_t) < 8 && size > SIZE_MAX / sizeof (char32)) Melder_fatal (U"(Melder_dup_f:) Can never allocate ", Melder_bigInteger (size), U" characters."); char32 *result = (char32 *) malloc ((size_t) size * sizeof (char32)); if (result == NULL) { if (theRainyDayFund != NULL) { free (theRainyDayFund); theRainyDayFund = NULL; } result = (char32 *) malloc ((size_t) size * sizeof (char32)); if (result != NULL) { Melder_flushError (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash."); } else { Melder_fatal (U"Out of memory: there is not enough room to duplicate a text of ", Melder_bigInteger (size - 1), U" characters."); } } str32cpy (result, string); totalNumberOfAllocations += 1; totalAllocationSize += size * (int64) sizeof (char32); return result; }
char * Melder_strdup_f (const char *string) { if (! string) return nullptr; int64 size = (int64) strlen (string) + 1; if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_fatal (U"(Melder_strdup_f:) Can never allocate ", Melder_bigInteger (size), U" bytes."); char *result = (char *) malloc ((size_t) size); if (! result) { if (theRainyDayFund) { free (theRainyDayFund); theRainyDayFund = nullptr; } result = (char *) malloc ((size_t) size * sizeof (char)); if (result) { Melder_flushError (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash."); } else { Melder_fatal (U"Out of memory: there is not enough room to duplicate a text of ", Melder_bigInteger (size - 1), U" characters."); } } strcpy (result, string); totalNumberOfAllocations += 1; totalAllocationSize += size; return result; }
void * _Melder_calloc_f (int64 nelem, int64 elsize) { if (nelem <= 0) Melder_fatal (U"(Melder_calloc_f:) Can never allocate ", Melder_bigInteger (nelem), U" elements."); if (elsize <= 0) Melder_fatal (U"(Melder_calloc_f:) Can never allocate elements whose size is ", Melder_bigInteger (elsize), U" bytes."); if ((uint64_t) nelem > SIZE_MAX / (uint64_t) elsize) Melder_fatal (U"(Melder_calloc_f:) Can never allocate ", Melder_bigInteger (nelem), U" elements whose sizes are ", Melder_bigInteger (elsize), U" bytes each."); void *result = calloc ((size_t) nelem, (size_t) elsize); if (result == NULL) { if (theRainyDayFund != NULL) { free (theRainyDayFund); theRainyDayFund = NULL; } result = calloc ((size_t) nelem, (size_t) elsize); if (result != NULL) { Melder_flushError (U"Praat is very low on memory.\nSave your work and quit Praat.\nIf you don't do that, Praat may crash."); } else { Melder_fatal (U"Out of memory: there is not enough room for ", Melder_bigInteger (nelem), U" more elements whose sizes are ", Melder_bigInteger (elsize), U" bytes each."); } } totalNumberOfAllocations += 1; totalAllocationSize += nelem * elsize; return result; }
void * Melder_realloc (void *ptr, int64 size) { if (size <= 0) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes."); if (sizeof (size_t) < 8 && size > SIZE_MAX) Melder_throw (U"Can never allocate ", Melder_bigInteger (size), U" bytes. Use a 64-bit edition of Praat instead?"); void *result = realloc (ptr, (size_t) size); // will not show in the statistics... if (result == NULL) Melder_throw (U"Out of memory. Could not extend room to ", Melder_bigInteger (size), U" bytes."); if (ptr == NULL) { // is it like malloc? if (Melder_debug == 34) { Melder_casual (U"Melder_realloc\t", Melder_pointer (result), U"\t", Melder_bigInteger (size), U"\t1"); } totalNumberOfAllocations += 1; totalAllocationSize += size; } else if (result != ptr) { // did realloc do a malloc-and-free? totalNumberOfAllocations += 1; totalAllocationSize += size; totalNumberOfDeallocations += 1; totalNumberOfMovingReallocs += 1; } else { totalNumberOfReallocsInSitu += 1; } return result; }
void praat_reportMemoryUse () { MelderInfo_open (); MelderInfo_writeLine (U"Memory use by Praat:\n"); MelderInfo_writeLine (U"Currently in use:\n" U" Strings: ", MelderString_allocationCount () - MelderString_deallocationCount ()); MelderInfo_writeLine (U" Arrays: ", NUM_getTotalNumberOfArrays ()); MelderInfo_writeLine (U" Things: ", Thing_getTotalNumberOfThings (), U" (objects in list: ", theCurrentPraatObjects -> n, U")"); long numberOfMotifWidgets = #if motif Gui_getNumberOfMotifWidgets (); MelderInfo_writeLine (U" Motif widgets: ", numberOfMotifWidgets); #else 0; #endif MelderInfo_writeLine (U" Other: ", Melder_allocationCount () - Melder_deallocationCount () - Thing_getTotalNumberOfThings () - NUM_getTotalNumberOfArrays () - (MelderString_allocationCount () - MelderString_deallocationCount ()) - numberOfMotifWidgets); MelderInfo_writeLine ( U"\nMemory history of this session:\n" U" Total created: ", Melder_bigInteger (Melder_allocationCount ()), U" (", Melder_bigInteger (Melder_allocationSize ()), U" bytes)"); MelderInfo_writeLine (U" Total deleted: ", Melder_bigInteger (Melder_deallocationCount ())); MelderInfo_writeLine (U" Reallocations: ", Melder_bigInteger (Melder_movingReallocationsCount ()), U" moving, ", Melder_bigInteger (Melder_reallocationsInSituCount ()), U" in situ"); MelderInfo_writeLine ( U" Strings created: ", Melder_bigInteger (MelderString_allocationCount ()), U" (", Melder_bigInteger (MelderString_allocationSize ()), U" bytes)"); MelderInfo_writeLine ( U" Strings deleted: ", Melder_bigInteger (MelderString_deallocationCount ()), U" (", Melder_bigInteger (MelderString_deallocationSize ()), U" bytes)"); MelderInfo_writeLine (U"\nHistory of all sessions from ", statistics.dateOfFirstSession, U" until today:"); MelderInfo_writeLine (U" Sessions: ", statistics.interactiveSessions, U" interactive, ", statistics.batchSessions, U" batch"); MelderInfo_writeLine (U" Total memory use: ", Melder_bigInteger ((int64) statistics.memory + Melder_allocationSize ()), U" bytes"); MelderInfo_writeLine (U"\nNumber of fixed menu commands: ", praat_getNumberOfMenuCommands ()); MelderInfo_writeLine (U"Number of dynamic menu commands: ", praat_getNumberOfActions ()); MelderInfo_close (); }