OpenTTD: src/core/alloc_type.hpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

alloc_type.hpp
Go to the documentation of this file.
1 /* $Id: alloc_type.hpp 23880 2012-02-04 13:28:35Z michi_cc $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #ifndef ALLOC_TYPE_HPP
13 #define ALLOC_TYPE_HPP
14
15 #include "alloc_func.hpp"
16
26 template <typename T, size_t length>
27 struct SmallStackSafeStackAlloc {
28 #if !defined(__NDS__)
29
30 T data[length];
31 #else
32
33 T *data;
35 size_t len;
36
38 SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
39
41 ~SmallStackSafeStackAlloc()
42 {
43 free(data);
44 }
45 #endif
46
51 inline operator T *()
52 {
53 return data;
54 }
55
60 inline T *operator -> ()
61 {
62 return data;
63 }
64
70 inline T *EndOf()
71 {
72 #if !defined(__NDS__)
73 return endof(data);
74 #else
75 return &data[len];
76 #endif
77 }
78 };
79
88 template <typename T>
89 class ReusableBuffer {
90 private:
91 T *buffer;
92 size_t count;
93
94 public:
96 ReusableBuffer() : buffer(NULL), count(0) {}
98 ~ReusableBuffer() { free(this->buffer); }
99
107 T *Allocate(size_t count)
108 {
109 if (this->count < count) {
110 free(this->buffer);
111 this->buffer = MallocT<T>(count);
112 this->count = count;
113 }
114 return this->buffer;
115 }
116
124 T *ZeroAllocate(size_t count)
125 {
126 if (this->count < count) {
127 free(this->buffer);
128 this->buffer = CallocT<T>(count);
129 this->count = count;
130 } else {
131 memset(this->buffer, 0, sizeof(T) * count);
132 }
133 return this->buffer;
134 }
135
140 inline const T *GetBuffer() const
141 {
142 return this->buffer;
143 }
144 };
145
150 class ZeroedMemoryAllocator
151 {
152 public:
153 ZeroedMemoryAllocator() {}
154 virtual ~ZeroedMemoryAllocator() {}
155
161 inline void *operator new(size_t size) { return CallocT<byte>(size); }
162
168 inline void *operator new[](size_t size) { return CallocT<byte>(size); }
169
174 inline void operator delete(void *ptr) { free(ptr); }
175
180 inline void operator delete[](void *ptr) { free(ptr); }
181 };
182
187 template <typename T>
188 class AutoFreePtr
189 {
190 T *ptr;
191
192 public:
193 AutoFreePtr(T *ptr) : ptr(ptr) {}
194 ~AutoFreePtr() { free(this->ptr); }
195
200 inline void Assign(T *ptr)
201 {
202 free(this->ptr);
203 this->ptr = ptr;
204 }
205
207 inline T *operator ->() { return this->ptr; }
209 inline const T *operator ->() const { return this->ptr; }
210
212 inline operator T *() { return this->ptr; }
214 inline operator const T *() const { return this->ptr; }
215 };
216
217 #endif /* ALLOC_TYPE_HPP */