author | peter1138 |
Tue, 22 Jan 2008 07:27:06 +0000 | |
changeset 8374 | 7a1b6c89cb89 |
parent 8130 | d2eb7d04f6e1 |
child 8376 | aab0b14b96e3 |
permissions | -rw-r--r-- |
8130
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
1 |
/* $Id$ */ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
2 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
3 |
/** @file alloc_func.hpp Functions related to the allocation of memory */ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
4 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
5 |
#ifndef ALLOC_FUNC_HPP |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
6 |
#define ALLOC_FUNC_HPP |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
7 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
8 |
/** |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
9 |
* Simplified allocation function that allocates the specified number of |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
10 |
* elements of the given type. It also explicitly casts it to the requested |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
11 |
* type. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
12 |
* @note throws an error when there is no memory anymore. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
13 |
* @note the memory contains garbage data (i.e. possibly non-zero values). |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
14 |
* @param T the type of the variable(s) to allocation. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
15 |
* @param num_elements the number of elements to allocate of the given type. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
16 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
17 |
*/ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
18 |
template <typename T> FORCEINLINE T* MallocT(size_t num_elements) |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
19 |
{ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
20 |
/* |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
21 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
22 |
* returns NULL. So we do that for *all* allocations, thus causing it |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
23 |
* to behave the same on all OSes. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
24 |
*/ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
25 |
if (num_elements == 0) return NULL; |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
26 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
27 |
T *t_ptr = (T*)malloc(num_elements * sizeof(T)); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
28 |
if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T)); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
29 |
return t_ptr; |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
30 |
} |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
31 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
32 |
/** |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
33 |
* Simplified allocation function that allocates the specified number of |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
34 |
* elements of the given type. It also explicitly casts it to the requested |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
35 |
* type. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
36 |
* @note throws an error when there is no memory anymore. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
37 |
* @note the memory contains all zero values. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
38 |
* @param T the type of the variable(s) to allocation. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
39 |
* @param num_elements the number of elements to allocate of the given type. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
40 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
41 |
*/ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
42 |
template <typename T> FORCEINLINE T* CallocT(size_t num_elements) |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
43 |
{ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
44 |
/* |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
45 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
46 |
* returns NULL. So we do that for *all* allocations, thus causing it |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
47 |
* to behave the same on all OSes. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
48 |
*/ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
49 |
if (num_elements == 0) return NULL; |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
50 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
51 |
T *t_ptr = (T*)calloc(num_elements, sizeof(T)); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
52 |
if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T)); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
53 |
return t_ptr; |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
54 |
} |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
55 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
56 |
/** |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
57 |
* Simplified reallocation function that allocates the specified number of |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
58 |
* elements of the given type. It also explicitly casts it to the requested |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
59 |
* type. It extends/shrinks the memory allocation given in t_ptr. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
60 |
* @note throws an error when there is no memory anymore. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
61 |
* @note the memory contains all zero values. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
62 |
* @param T the type of the variable(s) to allocation. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
63 |
* @param t_ptr the previous allocation to extend/shrink. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
64 |
* @param num_elements the number of elements to allocate of the given type. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
65 |
* @return NULL when num_elements == 0, non-NULL otherwise. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
66 |
*/ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
67 |
template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements) |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
68 |
{ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
69 |
/* |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
70 |
* MorphOS cannot handle 0 elements allocations, or rather that always |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
71 |
* returns NULL. So we do that for *all* allocations, thus causing it |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
72 |
* to behave the same on all OSes. |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
73 |
*/ |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
74 |
if (num_elements == 0) { |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
75 |
free(t_ptr); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
76 |
return NULL; |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
77 |
} |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
78 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
79 |
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
80 |
if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T)); |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
81 |
return t_ptr; |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
82 |
} |
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
83 |
|
d2eb7d04f6e1
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff
changeset
|
84 |
#endif /* ALLOC_FUNC_HPP */ |