OpenTTD
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>
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 
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>
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 
151 {
152 public:
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>
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 */
T * operator->()
Gets a pointer to the data stored in this wrapper.
Definition: alloc_type.hpp:60
T * EndOf()
Gets a pointer to the last data element stored in this wrapper.
Definition: alloc_type.hpp:70
Functions related to the allocation of memory.
static T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type...
Definition: alloc_func.hpp:59
A small &#39;wrapper&#39; for allocations that can be done on most OSes on the stack, but are just too large ...
Definition: alloc_type.hpp:27
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition: alloc_type.hpp:89
A smart pointer class that free()&#39;s the pointer on destruction.
Definition: alloc_type.hpp:188
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:150
T data[length]
Storing the data on the stack.
Definition: alloc_type.hpp:30
size_t count
Number of T elements in the buffer.
Definition: alloc_type.hpp:92
ReusableBuffer()
Create a new buffer.
Definition: alloc_type.hpp:96
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:427
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
void Assign(T *ptr)
Take ownership of a new pointer and free the old one if needed.
Definition: alloc_type.hpp:200
~ReusableBuffer()
Clear the buffer.
Definition: alloc_type.hpp:98