author | richk |
Wed, 16 Apr 2008 22:16:04 +0000 | |
branch | NewGRF_ports |
changeset 10210 | a2131f7a315d |
child 10242 | 52b4a9006029 |
permissions | -rw-r--r-- |
10210
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
1 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
2 |
/* $Id$ */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
3 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
4 |
/** @file alloc_type.hpp Helper types related to the allocation of memory */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
5 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
6 |
#ifndef ALLOC_TYPE_HPP |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
7 |
#define ALLOC_TYPE_HPP |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
8 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
9 |
#include "alloc_func.hpp" |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
10 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
11 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
12 |
* A small 'wrapper' for allocations that can be done on most OSes on the |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
13 |
* stack, but are just too large to fit in the stack on devices with a small |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
14 |
* stack such as the NDS. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
15 |
* So when it is possible a stack allocation is made, otherwise a heap |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
16 |
* allocation is made and this is freed once the struct goes out of scope. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
17 |
* @param T the type to make the allocation for |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
18 |
* @param length the amount of items to allocate |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
19 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
20 |
template <typename T, size_t length> |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
21 |
struct SmallStackSafeStackAlloc { |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
22 |
#if !defined(__NDS__) |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
23 |
/** Storing the data on the stack */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
24 |
T data[length]; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
25 |
#else |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
26 |
/** Storing it on the heap */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
27 |
T *data; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
28 |
/** The length (in elements) of data in this allocator. */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
29 |
size_t len; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
30 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
31 |
/** Allocating the memory */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
32 |
SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {} |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
33 |
/** And freeing when it goes out of scope */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
34 |
~SmallStackSafeStackAlloc() { free(data); } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
35 |
#endif |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
36 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
37 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
38 |
* Gets a pointer to the data stored in this wrapper. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
39 |
* @return the pointer. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
40 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
41 |
inline operator T* () { return data; } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
42 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
43 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
44 |
* Gets a pointer to the data stored in this wrapper. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
45 |
* @return the pointer. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
46 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
47 |
inline T* operator -> () { return data; } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
48 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
49 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
50 |
* Gets a pointer to the last data element stored in this wrapper. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
51 |
* @note needed because endof does not work properly for pointers. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
52 |
* @return the 'endof' pointer. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
53 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
54 |
inline T* EndOf() { |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
55 |
#if !defined(__NDS__) |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
56 |
return endof(data); |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
57 |
#else |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
58 |
return &data[len]; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
59 |
#endif |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
60 |
} |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
61 |
}; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
62 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
63 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
64 |
* Base class that provides memory initialization on dynamically created objects. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
65 |
* All allocated memory will be zeroed. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
66 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
67 |
class ZeroedMemoryAllocator |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
68 |
{ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
69 |
public: |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
70 |
ZeroedMemoryAllocator() {} |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
71 |
virtual ~ZeroedMemoryAllocator() {} |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
72 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
73 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
74 |
* Memory allocator for a single class instance. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
75 |
* @param size the amount of bytes to allocate. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
76 |
* @return the given amounts of bytes zeroed. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
77 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
78 |
void *operator new(size_t size) { return CallocT<byte>(size); } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
79 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
80 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
81 |
* Memory allocator for an array of class instances. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
82 |
* @param size the amount of bytes to allocate. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
83 |
* @return the given amounts of bytes zeroed. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
84 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
85 |
void *operator new[](size_t size) { return CallocT<byte>(size); } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
86 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
87 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
88 |
* Memory release for a single class instance. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
89 |
* @param ptr the memory to free. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
90 |
* @param size the amount of allocated memory (unused). |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
91 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
92 |
void operator delete(void *ptr, size_t size) { free(ptr); } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
93 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
94 |
/** |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
95 |
* Memory release for an array of class instances. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
96 |
* @param ptr the memory to free. |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
97 |
* @param size the amount of allocated memory (unused). |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
98 |
*/ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
99 |
void operator delete[](void *ptr, size_t size) { free(ptr); } |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
100 |
}; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
101 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
diff
changeset
|
102 |
#endif /* ALLOC_TYPE_HPP */ |