author | richk |
Mon, 10 Mar 2008 15:26:39 +0000 | |
branch | NewGRF_ports |
changeset 6878 | 7d1ff2f621c7 |
parent 6872 | 1c4a4a609f85 |
child 10184 | fcf5fb2548eb |
permissions | -rw-r--r-- |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
1 |
/* $Id$ */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
2 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
3 |
/** @file alloc_func.hpp Functions related to the allocation of memory */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
4 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
5 |
#ifndef ALLOC_FUNC_HPP |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
6 |
#define ALLOC_FUNC_HPP |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
7 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
8 |
/** |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
9 |
* Functions to exit badly with an error message. |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
10 |
* It has to be linked so the error messages are not |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
11 |
* duplicated in each object file making the final |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
12 |
* binary needlessly large. |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
13 |
*/ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
14 |
void MallocError(size_t size); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
15 |
void ReallocError(size_t size); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
16 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
17 |
/** |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
18 |
* Simplified allocation function that allocates the specified number of |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
19 |
* elements of the given type. It also explicitly casts it to the requested |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
20 |
* type. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
21 |
* @note throws an error when there is no memory anymore. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
22 |
* @note the memory contains garbage data (i.e. possibly non-zero values). |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
23 |
* @param T the type of the variable(s) to allocation. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
24 |
* @param num_elements the number of elements to allocate of the given type. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
25 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
26 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
27 |
template <typename T> FORCEINLINE T* MallocT(size_t num_elements) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
28 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
29 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
30 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
31 |
* returns NULL. So we do that for *all* allocations, thus causing it |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
32 |
* to behave the same on all OSes. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
33 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
34 |
if (num_elements == 0) return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
35 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
36 |
T *t_ptr = (T*)malloc(num_elements * sizeof(T)); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
37 |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
38 |
return t_ptr; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
39 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
40 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
41 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
42 |
* Simplified allocation function that allocates the specified number of |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
43 |
* elements of the given type. It also explicitly casts it to the requested |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
44 |
* type. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
45 |
* @note throws an error when there is no memory anymore. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
46 |
* @note the memory contains all zero values. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
47 |
* @param T the type of the variable(s) to allocation. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
48 |
* @param num_elements the number of elements to allocate of the given type. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
49 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
50 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
51 |
template <typename T> FORCEINLINE T* CallocT(size_t num_elements) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
52 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
53 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
54 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
55 |
* returns NULL. So we do that for *all* allocations, thus causing it |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
56 |
* to behave the same on all OSes. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
57 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
58 |
if (num_elements == 0) return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
59 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
60 |
T *t_ptr = (T*)calloc(num_elements, sizeof(T)); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
61 |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
62 |
return t_ptr; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
63 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
64 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
65 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
66 |
* Simplified reallocation function that allocates the specified number of |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
67 |
* elements of the given type. It also explicitly casts it to the requested |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
68 |
* type. It extends/shrinks the memory allocation given in t_ptr. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
69 |
* @note throws an error when there is no memory anymore. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
70 |
* @note the memory contains all zero values. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
71 |
* @param T the type of the variable(s) to allocation. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
72 |
* @param t_ptr the previous allocation to extend/shrink. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
73 |
* @param num_elements the number of elements to allocate of the given type. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
74 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
75 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
76 |
template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
77 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
78 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
79 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
80 |
* returns NULL. So we do that for *all* allocations, thus causing it |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
81 |
* to behave the same on all OSes. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
82 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
83 |
if (num_elements == 0) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
84 |
free(t_ptr); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
85 |
return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
86 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
87 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
88 |
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6872
diff
changeset
|
89 |
if (t_ptr == NULL) ReallocError(num_elements * sizeof(T)); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
90 |
return t_ptr; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
91 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
92 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
93 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
94 |
* A small 'wrapper' for allocations that can be done on most OSes on the |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
95 |
* stack, but are just too large to fit in the stack on devices with a small |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
96 |
* stack such as the NDS. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
97 |
* So when it is possible a stack allocation is made, otherwise a heap |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
98 |
* allocation is made and this is freed once the struct goes out of scope. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
99 |
* @param T the type to make the allocation for |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
100 |
* @param length the amount of items to allocate |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
101 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
102 |
template <typename T, size_t length> |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
103 |
struct SmallStackSafeStackAlloc { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
104 |
#if !defined(__NDS__) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
105 |
/** Storing the data on the stack */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
106 |
T data[length]; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
107 |
#else |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
108 |
/** Storing it on the heap */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
109 |
T *data; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
110 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
111 |
/** Allocating the memory */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
112 |
SmallStackSafeStackAlloc() : data(MallocT<T>(length)) {} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
113 |
/** And freeing when it goes out of scope */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
114 |
~SmallStackSafeStackAlloc() { free(data); } |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
115 |
#endif |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
116 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
117 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
118 |
* Gets a pointer to the data stored in this wrapper. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
119 |
* @return the pointer. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
120 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
121 |
operator T* () { return data; } |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
122 |
}; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
123 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff
changeset
|
124 |
#endif /* ALLOC_FUNC_HPP */ |