ARB
arb_mem.h
Go to the documentation of this file.
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) {
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) {
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) {
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) {
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) {
77  tgt = ARB_alloc<TYPE>(nelem);
78 }
79 
80 template<class TYPE>
81 inline TYPE *ARB_calloc(size_t nelem) {
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) {
102  tgt = ARB_calloc<TYPE>(nelem);
103 }
104 
105 #else
106 #error arb_mem.h included twice
107 #endif // ARB_MEM_H
TYPE * ARB_alloc(size_t nelem)
Definition: arb_mem.h:56
static void error(const char *msg)
Definition: mkptypes.cxx:96
void ARB_recalloc(TYPE *&tgt, size_t oelem, size_t nelem)
Definition: arb_mem.h:49
void failed_to_allocate(const char *reason) __ATTR__NORETURN
Definition: arb_mem.cxx:34
TYPE * ARB_calloc(size_t nelem)
Definition: arb_mem.h:81
void ARB_realloc(TYPE *&tgt, size_t nelem)
Definition: arb_mem.h:43
#define __ATTR__NORETURN
Definition: attributes.h:56
void alloc_aligned(void **tgt, size_t alignment, size_t len)
Definition: arb_mem.h:30
void ARB_alloc_aligned(TYPE *&tgt, size_t nelems)
Definition: arb_mem.h:37
PT1_TYPE
Definition: probe_tree.h:185