| 1 |
#ifndef _melder_alloc_h_ |
| 2 |
#define _melder_alloc_h_ |
| 3 |
/* melder_alloc.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2020 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* These functions call malloc, free, realloc, and calloc. */ |
| 22 |
/* If out of memory, the non-f versions throw an error message (like "Out of memory"); */ |
| 23 |
/* the f versions open up a rainy day fund or crash Praat. */ |
| 24 |
/* These functions also maintain a count of the total number of blocks allocated. */ |
| 25 |
|
| 26 |
void Melder_alloc_init (); // to be called around program start-up |
| 27 |
void * _Melder_malloc (int64 size); |
| 28 |
#define Melder_malloc(type,numberOfElements) (type *) _Melder_malloc ((numberOfElements) * (int64) sizeof (type)) |
| 29 |
void * _Melder_malloc_f (int64 size); |
| 30 |
#define Melder_malloc_f(type,numberOfElements) (type *) _Melder_malloc_f ((numberOfElements) * (int64) sizeof (type)) |
| 31 |
void * Melder_realloc (void *pointer, int64 size); |
| 32 |
void * Melder_realloc_f (void *pointer, int64 size); |
| 33 |
void * _Melder_calloc (int64 numberOfElements, int64 elementSize); |
| 34 |
#define Melder_calloc(type,numberOfElements) (type *) _Melder_calloc (numberOfElements, sizeof (type)) |
| 35 |
void * _Melder_calloc_f (int64 numberOfElements, int64 elementSize); |
| 36 |
#define Melder_calloc_f(type,numberOfElements) (type *) _Melder_calloc_f (numberOfElements, sizeof (type)) |
| 37 |
|
| 38 |
#define Melder_free(pointer) _Melder_free ((void **) & (pointer)) |
| 39 |
void _Melder_free (void **pointer) noexcept; |
| 40 |
/* |
| 41 |
Preconditions: |
| 42 |
none (*pointer may be null). |
| 43 |
Postconditions: |
| 44 |
*pointer == nullptr; |
| 45 |
*/ |
| 46 |
|
| 47 |
int64 Melder_allocationCount (); |
| 48 |
/* |
| 49 |
Returns the total number of successful calls to |
| 50 |
Melder_malloc, Melder_realloc (if 'ptr' is null), and Melder_calloc, |
| 51 |
since the start of the process. Mainly for debugging purposes. |
| 52 |
*/ |
| 53 |
|
| 54 |
int64 Melder_deallocationCount (); |
| 55 |
/* |
| 56 |
Returns the total number of successful calls to Melder_free, |
| 57 |
since the start of the process. Mainly for debugging purposes. |
| 58 |
*/ |
| 59 |
|
| 60 |
int64 Melder_allocationSize (); |
| 61 |
/* |
| 62 |
Returns the total number of bytes allocated in calls to |
| 63 |
Melder_malloc, Melder_realloc (if moved), and Melder_calloc, |
| 64 |
since the start of the process. Mainly for debugging purposes. |
| 65 |
*/ |
| 66 |
|
| 67 |
int64 Melder_reallocationsInSituCount (); |
| 68 |
int64 Melder_movingReallocationsCount (); |
| 69 |
|
| 70 |
/********** Arrays. **********/ |
| 71 |
|
| 72 |
namespace MelderArray { |
| 73 |
|
| 74 |
enum class kInitializationType { RAW = 0, ZERO = 1 }; |
| 75 |
|
| 76 |
byte * _alloc_generic (integer cellSize, integer numberOfCells, kInitializationType initializationType); |
| 77 |
void _free_generic (byte *cells, integer numberOfCells) noexcept; |
| 78 |
|
| 79 |
template <class T> |
| 80 |
T* _alloc (integer numberOfCells, kInitializationType initializationType) { |
| 81 |
T* result = reinterpret_cast <T*> (MelderArray:: _alloc_generic (sizeof (T), numberOfCells, initializationType)); |
| 82 |
return result; |
| 83 |
} |
| 84 |
|
| 85 |
template <class T> |
| 86 |
void _free (T* cells, integer numberOfCells) noexcept { |
| 87 |
_free_generic (reinterpret_cast <byte *> (cells), numberOfCells); |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
int64 MelderArray_allocationCount (); |
| 93 |
int64 MelderArray_deallocationCount (); |
| 94 |
int64 MelderArray_cellAllocationCount (); |
| 95 |
int64 MelderArray_cellDeallocationCount (); |
| 96 |
|
| 97 |
/* End of file melder_alloc.h */ |
| 98 |
#endif |