author | Tero Marttila <terom@fixme.fi> |
Fri, 19 Dec 2008 02:20:26 +0200 | |
changeset 10441 | d09735696a9e |
parent 9576 | 55069e97e7d2 |
permissions | -rw-r--r-- |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
1 |
/* $Id$ */ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
2 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
3 |
/** @file alloc_type.hpp Helper types related to the allocation of memory */ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
4 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
5 |
#ifndef ALLOC_TYPE_HPP |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
6 |
#define ALLOC_TYPE_HPP |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
7 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
8 |
#include "alloc_func.hpp" |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
9 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
10 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
11 |
* A small 'wrapper' for allocations that can be done on most OSes on the |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
12 |
* stack, but are just too large to fit in the stack on devices with a small |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
13 |
* stack such as the NDS. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
14 |
* So when it is possible a stack allocation is made, otherwise a heap |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
15 |
* allocation is made and this is freed once the struct goes out of scope. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
16 |
* @param T the type to make the allocation for |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
17 |
* @param length the amount of items to allocate |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
18 |
*/ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
19 |
template <typename T, size_t length> |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
20 |
struct SmallStackSafeStackAlloc { |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
21 |
#if !defined(__NDS__) |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
22 |
/** Storing the data on the stack */ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
23 |
T data[length]; |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
24 |
#else |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
25 |
/** Storing it on the heap */ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
26 |
T *data; |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
27 |
/** The length (in elements) of data in this allocator. */ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
28 |
size_t len; |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
29 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
30 |
/** Allocating the memory */ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
31 |
SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {} |
9576
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
32 |
|
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
33 |
/** And freeing when it goes out of scope */ |
9576
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
34 |
~SmallStackSafeStackAlloc() |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
35 |
{ |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
36 |
free(data); |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
37 |
} |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
38 |
#endif |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
39 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
40 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
41 |
* Gets a pointer to the data stored in this wrapper. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
42 |
* @return the pointer. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
43 |
*/ |
9576
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
44 |
FORCEINLINE operator T* () |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
45 |
{ |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
46 |
return data; |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
47 |
} |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
48 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
49 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
50 |
* Gets a pointer to the data stored in this wrapper. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
51 |
* @return the pointer. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
52 |
*/ |
9576
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
53 |
FORCEINLINE T* operator -> () |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
54 |
{ |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
55 |
return data; |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
56 |
} |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
57 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
58 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
59 |
* Gets a pointer to the last data element stored in this wrapper. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
60 |
* @note needed because endof does not work properly for pointers. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
61 |
* @return the 'endof' pointer. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
62 |
*/ |
9576
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
63 |
FORCEINLINE T* EndOf() |
55069e97e7d2
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents:
9575
diff
changeset
|
64 |
{ |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
65 |
#if !defined(__NDS__) |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
66 |
return endof(data); |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
67 |
#else |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
68 |
return &data[len]; |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
69 |
#endif |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
70 |
} |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
71 |
}; |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
72 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
73 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
74 |
* Base class that provides memory initialization on dynamically created objects. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
75 |
* All allocated memory will be zeroed. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
76 |
*/ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
77 |
class ZeroedMemoryAllocator |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
78 |
{ |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
79 |
public: |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
80 |
ZeroedMemoryAllocator() {} |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
81 |
virtual ~ZeroedMemoryAllocator() {} |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
82 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
83 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
84 |
* Memory allocator for a single class instance. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
85 |
* @param size the amount of bytes to allocate. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
86 |
* @return the given amounts of bytes zeroed. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
87 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9111
diff
changeset
|
88 |
FORCEINLINE void *operator new(size_t size) { return CallocT<byte>(size); } |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
89 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
90 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
91 |
* Memory allocator for an array of class instances. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
92 |
* @param size the amount of bytes to allocate. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
93 |
* @return the given amounts of bytes zeroed. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
94 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9111
diff
changeset
|
95 |
FORCEINLINE void *operator new[](size_t size) { return CallocT<byte>(size); } |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
96 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
97 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
98 |
* Memory release for a single class instance. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
99 |
* @param ptr the memory to free. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
100 |
* @param size the amount of allocated memory (unused). |
8957
ef3f499ff423
(svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents:
8925
diff
changeset
|
101 |
* |
ef3f499ff423
(svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents:
8925
diff
changeset
|
102 |
* @warning The value of the \a size parameter can only be trusted for |
ef3f499ff423
(svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents:
8925
diff
changeset
|
103 |
* classes that have their own (virtual) destructor method. |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
104 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9111
diff
changeset
|
105 |
FORCEINLINE void operator delete(void *ptr, size_t size) { free(ptr); } |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
106 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
107 |
/** |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
108 |
* Memory release for an array of class instances. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
109 |
* @param ptr the memory to free. |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
110 |
* @param size the amount of allocated memory (unused). |
8957
ef3f499ff423
(svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents:
8925
diff
changeset
|
111 |
* |
ef3f499ff423
(svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents:
8925
diff
changeset
|
112 |
* @warning The value of the \a size parameter can only be trusted for |
ef3f499ff423
(svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents:
8925
diff
changeset
|
113 |
* classes that have their own (virtual) destructor method. |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
114 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9111
diff
changeset
|
115 |
FORCEINLINE void operator delete[](void *ptr, size_t size) { free(ptr); } |
8925
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
116 |
}; |
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
117 |
|
e0d37ce1eba8
(svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff
changeset
|
118 |
#endif /* ALLOC_TYPE_HPP */ |