author | rubidium |
Wed, 09 Jul 2008 13:32:13 +0000 | |
branch | noai |
changeset 11126 | 72d4c9314c72 |
parent 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 |
*/ |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
10920
diff
changeset
|
27 |
template <typename T> |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
10920
diff
changeset
|
28 |
static FORCEINLINE T *MallocT(size_t num_elements) |
9723 | 29 |
{ |
30 |
/* |
|
31 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
|
32 |
* returns NULL. So we do that for *all* allocations, thus causing it |
|
33 |
* to behave the same on all OSes. |
|
34 |
*/ |
|
35 |
if (num_elements == 0) return NULL; |
|
36 |
||
37 |
T *t_ptr = (T*)malloc(num_elements * sizeof(T)); |
|
9732 | 38 |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); |
9723 | 39 |
return t_ptr; |
40 |
} |
|
41 |
||
42 |
/** |
|
43 |
* Simplified allocation function that allocates the specified number of |
|
44 |
* elements of the given type. It also explicitly casts it to the requested |
|
45 |
* type. |
|
46 |
* @note throws an error when there is no memory anymore. |
|
47 |
* @note the memory contains all zero values. |
|
48 |
* @param T the type of the variable(s) to allocation. |
|
49 |
* @param num_elements the number of elements to allocate of the given type. |
|
50 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
|
51 |
*/ |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
10920
diff
changeset
|
52 |
template <typename T> |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
10920
diff
changeset
|
53 |
static FORCEINLINE T *CallocT(size_t num_elements) |
9723 | 54 |
{ |
55 |
/* |
|
56 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
|
57 |
* returns NULL. So we do that for *all* allocations, thus causing it |
|
58 |
* to behave the same on all OSes. |
|
59 |
*/ |
|
60 |
if (num_elements == 0) return NULL; |
|
61 |
||
62 |
T *t_ptr = (T*)calloc(num_elements, sizeof(T)); |
|
9732 | 63 |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); |
9723 | 64 |
return t_ptr; |
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Simplified reallocation function that allocates the specified number of |
|
69 |
* elements of the given type. It also explicitly casts it to the requested |
|
70 |
* type. It extends/shrinks the memory allocation given in t_ptr. |
|
71 |
* @note throws an error when there is no memory anymore. |
|
72 |
* @note the memory contains all zero values. |
|
73 |
* @param T the type of the variable(s) to allocation. |
|
74 |
* @param t_ptr the previous allocation to extend/shrink. |
|
75 |
* @param num_elements the number of elements to allocate of the given type. |
|
76 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
|
77 |
*/ |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
10920
diff
changeset
|
78 |
template <typename T> |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
10920
diff
changeset
|
79 |
static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements) |
9723 | 80 |
{ |
81 |
/* |
|
82 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
|
83 |
* returns NULL. So we do that for *all* allocations, thus causing it |
|
84 |
* to behave the same on all OSes. |
|
85 |
*/ |
|
86 |
if (num_elements == 0) { |
|
87 |
free(t_ptr); |
|
88 |
return NULL; |
|
89 |
} |
|
90 |
||
91 |
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
|
9732 | 92 |
if (t_ptr == NULL) ReallocError(num_elements * sizeof(T)); |
9723 | 93 |
return t_ptr; |
94 |
} |
|
95 |
||
10920 | 96 |
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */ |
97 |
#define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T))) |
|
98 |
||
9723 | 99 |
#endif /* ALLOC_FUNC_HPP */ |