source: tags/ms_r17q1/CORE/arb_mem.h

Last change on this file was 15176, checked in by westram, 8 years ago
File size: 3.9 KB
Line 
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
25namespace 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
36template<class TYPE>
37inline 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
42template<class TYPE>
43inline 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}
48template<class TYPE>
49inline 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__ATTR__DEPRECATED("use ARB_alloc") inline void *ARB_alloc_ELIM(size_t size) {
56    void *mem = malloc(size);
57    if (!mem) arb_mem::failed_to_allocate(size);
58    return mem;
59}
60template<class TYPE>
61inline TYPE *ARB_alloc(size_t nelem) {
62    /*! malloc memory for an array of 'nelem' instances of TYPE (terminates on failure).
63     * @code
64     * int *p = ARB_alloc<int>(7); // allocates space for 7 int
65     * @endcode
66     * @see ARB_alloc
67     */
68
69    TYPE *mem = (TYPE*)malloc(nelem*sizeof(TYPE));
70    if (!mem) arb_mem::failed_to_allocate(nelem, sizeof(TYPE));
71    return mem;
72}
73template<class TYPE>
74inline void ARB_alloc(TYPE*& tgt, size_t nelem) {
75    /*! malloc memory for an array of 'nelem' instances of TYPE and assign its address to 'tgt' (terminates on failure).
76     * @code
77     * int *p;
78     * ARB_alloc(p, 7); // allocates space for 7 int
79     * @endcode
80     * @see ARB_alloc<TYPE>
81     */
82    tgt = ARB_alloc<TYPE>(nelem);
83}
84
85template<class TYPE>
86inline TYPE *ARB_calloc(size_t nelem) {
87    /*! calloc memory for an array of 'nelem' instances of TYPE (terminates on failure).
88     * @code
89     * int *p = ARB_calloc<int>(7); // allocates space for 7 int (initializes memory with 0)
90     * @endcode
91     * @see ARB_calloc
92     */
93
94    TYPE *mem = (TYPE*)calloc(nelem, sizeof(TYPE));
95    if (!mem) arb_mem::failed_to_allocate(nelem, sizeof(TYPE));
96    return mem;
97}
98template<class TYPE>
99inline void ARB_calloc(TYPE*& tgt, size_t nelem) {
100    /*! calloc memory for an array of 'nelem' instances of TYPE and assign its address to 'tgt' (terminates on failure).
101     * @code
102     * int *p;
103     * ARB_calloc(p, 7); // allocates space for 7 int (initializes memory with 0)
104     * @endcode
105     * @see ARB_calloc<TYPE>
106     */
107    tgt = ARB_calloc<TYPE>(nelem);
108}
109
110#else
111#error arb_mem.h included twice
112#endif // ARB_MEM_H
Note: See TracBrowser for help on using the repository browser.