author | rubidium |
Fri, 18 Apr 2008 23:33:51 +0000 | |
branch | noai |
changeset 10249 | 58810805030e |
parent 10181 | 54df587fef5d |
child 10920 | e33442a2b239 |
permissions | -rw-r--r-- |
9723 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file alloc_func.hpp Functions related to the allocation of memory */ |
|
4 |
||
5 |
#ifndef ALLOC_FUNC_HPP |
|
6 |
#define ALLOC_FUNC_HPP |
|
7 |
||
8 |
/** |
|
9732 | 9 |
* Functions to exit badly with an error message. |
10 |
* It has to be linked so the error messages are not |
|
11 |
* duplicated in each object file making the final |
|
12 |
* binary needlessly large. |
|
13 |
*/ |
|
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
14 |
void NORETURN MallocError(size_t size); |
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10181
diff
changeset
|
15 |
void NORETURN ReallocError(size_t size); |
9732 | 16 |
|
17 |
/** |
|
9723 | 18 |
* Simplified allocation function that allocates the specified number of |
19 |
* elements of the given type. It also explicitly casts it to the requested |
|
20 |
* type. |
|
21 |
* @note throws an error when there is no memory anymore. |
|
22 |
* @note the memory contains garbage data (i.e. possibly non-zero values). |
|
23 |
* @param T the type of the variable(s) to allocation. |
|
24 |
* @param num_elements the number of elements to allocate of the given type. |
|
25 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
|
26 |
*/ |
|
27 |
template <typename T> FORCEINLINE T* MallocT(size_t num_elements) |
|
28 |
{ |
|
29 |
/* |
|
30 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
|
31 |
* returns NULL. So we do that for *all* allocations, thus causing it |
|
32 |
* to behave the same on all OSes. |
|
33 |
*/ |
|
34 |
if (num_elements == 0) return NULL; |
|
35 |
||
36 |
T *t_ptr = (T*)malloc(num_elements * sizeof(T)); |
|
9732 | 37 |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); |
9723 | 38 |
return t_ptr; |
39 |
} |
|
40 |
||
41 |
/** |
|
42 |
* Simplified allocation function that allocates the specified number of |
|
43 |
* elements of the given type. It also explicitly casts it to the requested |
|
44 |
* type. |
|
45 |
* @note throws an error when there is no memory anymore. |
|
46 |
* @note the memory contains all zero values. |
|
47 |
* @param T the type of the variable(s) to allocation. |
|
48 |
* @param num_elements the number of elements to allocate of the given type. |
|
49 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
|
50 |
*/ |
|
51 |
template <typename T> FORCEINLINE T* CallocT(size_t num_elements) |
|
52 |
{ |
|
53 |
/* |
|
54 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
|
55 |
* returns NULL. So we do that for *all* allocations, thus causing it |
|
56 |
* to behave the same on all OSes. |
|
57 |
*/ |
|
58 |
if (num_elements == 0) return NULL; |
|
59 |
||
60 |
T *t_ptr = (T*)calloc(num_elements, sizeof(T)); |
|
9732 | 61 |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); |
9723 | 62 |
return t_ptr; |
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* Simplified reallocation function that allocates the specified number of |
|
67 |
* elements of the given type. It also explicitly casts it to the requested |
|
68 |
* type. It extends/shrinks the memory allocation given in t_ptr. |
|
69 |
* @note throws an error when there is no memory anymore. |
|
70 |
* @note the memory contains all zero values. |
|
71 |
* @param T the type of the variable(s) to allocation. |
|
72 |
* @param t_ptr the previous allocation to extend/shrink. |
|
73 |
* @param num_elements the number of elements to allocate of the given type. |
|
74 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
|
75 |
*/ |
|
76 |
template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements) |
|
77 |
{ |
|
78 |
/* |
|
79 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
|
80 |
* returns NULL. So we do that for *all* allocations, thus causing it |
|
81 |
* to behave the same on all OSes. |
|
82 |
*/ |
|
83 |
if (num_elements == 0) { |
|
84 |
free(t_ptr); |
|
85 |
return NULL; |
|
86 |
} |
|
87 |
||
88 |
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
|
9732 | 89 |
if (t_ptr == NULL) ReallocError(num_elements * sizeof(T)); |
9723 | 90 |
return t_ptr; |
91 |
} |
|
92 |
||
93 |
#endif /* ALLOC_FUNC_HPP */ |