blob: aaa38acb590f3cf176663a55bf64ac7a29ae8af8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/* $Id$ */
/** @file helpers.hpp */
#ifndef HELPERS_HPP
#define HELPERS_HPP
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */
template <typename T> FORCEINLINE T* MallocT(size_t num_elements)
{
T *t_ptr = (T*)malloc(num_elements * sizeof(T));
if (t_ptr == NULL && num_elements != 0) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
return t_ptr;
}
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */
template <typename T> FORCEINLINE T* CallocT(size_t num_elements)
{
T *t_ptr = (T*)calloc(num_elements, sizeof(T));
if (t_ptr == NULL && num_elements != 0) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
return t_ptr;
}
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */
template <typename T> FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements)
{
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
if (t_ptr == NULL && num_elements != 0) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T));
return t_ptr;
}
/** type safe swap operation */
template<typename T> void Swap(T& a, T& b)
{
T t = a;
a = b;
b = t;
}
#endif /* HELPERS_HPP */
|