| 1 | // ================================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : arb_mem.h // |
|---|
| 4 | // Purpose : "Failsafe" memory handlers // |
|---|
| 5 | // ("succeed or terminate"!) // |
|---|
| 6 | // // |
|---|
| 7 | // Coded by Elmar Pruesse and Ralf Westram // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // ================================================================= // |
|---|
| 11 | |
|---|
| 12 | #ifndef ARB_MEM_H |
|---|
| 13 | #define ARB_MEM_H |
|---|
| 14 | |
|---|
| 15 | #ifndef _GLIBCXX_CSTDLIB |
|---|
| 16 | #include <cstdlib> |
|---|
| 17 | #endif |
|---|
| 18 | #ifndef _GLIBCXX_CSTRING |
|---|
| 19 | #include <cstring> |
|---|
| 20 | #endif |
|---|
| 21 | #ifndef ATTRIBUTES_H |
|---|
| 22 | #include <attributes.h> |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | namespace arb_mem { |
|---|
| 26 | void failed_to_allocate(const char *reason) __ATTR__NORETURN; |
|---|
| 27 | void failed_to_allocate(size_t nelem, size_t elsize) __ATTR__NORETURN; |
|---|
| 28 | void failed_to_allocate(size_t size) __ATTR__NORETURN; |
|---|
| 29 | |
|---|
| 30 | inline void alloc_aligned(void **tgt, size_t alignment, size_t len) { |
|---|
| 31 | int error = posix_memalign(tgt, alignment, len); |
|---|
| 32 | if (error) failed_to_allocate(strerror(error)); |
|---|
| 33 | } |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | template<class TYPE> |
|---|
| 37 | inline void ARB_alloc_aligned(TYPE*& tgt, size_t nelems) { |
|---|
| 38 | /*! allocate 16 byte aligned memory (terminate on failure) */ |
|---|
| 39 | arb_mem::alloc_aligned((void**)&tgt, 16, nelems * sizeof(TYPE)); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | template<class TYPE> |
|---|
| 43 | inline void ARB_realloc(TYPE*& tgt, size_t nelem) { |
|---|
| 44 | /*! reallocate memoryblock to fit 'nelem' entries (terminate on failure) */ |
|---|
| 45 | tgt = (TYPE*)realloc(tgt, nelem*sizeof(TYPE)); |
|---|
| 46 | if (!tgt) arb_mem::failed_to_allocate(nelem, sizeof(TYPE)); |
|---|
| 47 | } |
|---|
| 48 | template<class TYPE> |
|---|
| 49 | inline void ARB_recalloc(TYPE*& tgt, size_t oelem, size_t nelem) { |
|---|
| 50 | /*! reallocate memoryblock to fit 'nelem' entries (partially cleared if 'oelem<nelem'; terminate on failure) */ |
|---|
| 51 | ARB_realloc(tgt, nelem); |
|---|
| 52 | if (nelem>oelem) memset(tgt+oelem, 0, (nelem-oelem)*sizeof(TYPE)); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | template<class TYPE> |
|---|
| 56 | inline TYPE *ARB_alloc(size_t nelem) { |
|---|
| 57 | /*! malloc memory for an array of 'nelem' instances of TYPE (terminates on failure). |
|---|
| 58 | * @code |
|---|
| 59 | * int *p = ARB_alloc<int>(7); // allocates space for 7 int |
|---|
| 60 | * @endcode |
|---|
| 61 | * @see ARB_alloc |
|---|
| 62 | */ |
|---|
| 63 | |
|---|
| 64 | TYPE *mem = (TYPE*)malloc(nelem*sizeof(TYPE)); |
|---|
| 65 | if (!mem) arb_mem::failed_to_allocate(nelem, sizeof(TYPE)); |
|---|
| 66 | return mem; |
|---|
| 67 | } |
|---|
| 68 | template<class TYPE> |
|---|
| 69 | inline void ARB_alloc(TYPE*& tgt, size_t nelem) { |
|---|
| 70 | /*! malloc memory for an array of 'nelem' instances of TYPE and assign its address to 'tgt' (terminates on failure). |
|---|
| 71 | * @code |
|---|
| 72 | * int *p; |
|---|
| 73 | * ARB_alloc(p, 7); // allocates space for 7 int |
|---|
| 74 | * @endcode |
|---|
| 75 | * @see ARB_alloc<TYPE> |
|---|
| 76 | */ |
|---|
| 77 | tgt = ARB_alloc<TYPE>(nelem); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | template<class TYPE> |
|---|
| 81 | inline TYPE *ARB_calloc(size_t nelem) { |
|---|
| 82 | /*! calloc memory for an array of 'nelem' instances of TYPE (terminates on failure). |
|---|
| 83 | * @code |
|---|
| 84 | * int *p = ARB_calloc<int>(7); // allocates space for 7 int (initializes memory with 0) |
|---|
| 85 | * @endcode |
|---|
| 86 | * @see ARB_calloc |
|---|
| 87 | */ |
|---|
| 88 | |
|---|
| 89 | TYPE *mem = (TYPE*)calloc(nelem, sizeof(TYPE)); |
|---|
| 90 | if (!mem) arb_mem::failed_to_allocate(nelem, sizeof(TYPE)); |
|---|
| 91 | return mem; |
|---|
| 92 | } |
|---|
| 93 | template<class TYPE> |
|---|
| 94 | inline void ARB_calloc(TYPE*& tgt, size_t nelem) { |
|---|
| 95 | /*! calloc memory for an array of 'nelem' instances of TYPE and assign its address to 'tgt' (terminates on failure). |
|---|
| 96 | * @code |
|---|
| 97 | * int *p; |
|---|
| 98 | * ARB_calloc(p, 7); // allocates space for 7 int (initializes memory with 0) |
|---|
| 99 | * @endcode |
|---|
| 100 | * @see ARB_calloc<TYPE> |
|---|
| 101 | */ |
|---|
| 102 | tgt = ARB_calloc<TYPE>(nelem); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | #else |
|---|
| 106 | #error arb_mem.h included twice |
|---|
| 107 | #endif // ARB_MEM_H |
|---|