# alloc_type.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef ALLOC_TYPE_HPP
    00013 #define ALLOC_TYPE_HPP
    00014
    00015 #include "alloc_func.hpp"
    00016
    00026 template <typename T, size_t length>
    00027 struct SmallStackSafeStackAlloc {
    00028 #if !defined(__NDS__)
    00029
    00030   T data[length];
    00031 #else
    00032
    00033   T *data;
    00035   size_t len;
    00036
    00038   SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
    00039
    00041   ~SmallStackSafeStackAlloc()
    00042   {
    00043     free(data);
    00044   }
    00045 #endif
    00046
    00051   inline operator T *()
    00052   {
    00053     return data;
    00054   }
    00055
    00060   inline T *operator -> ()
    00061   {
    00062     return data;
    00063   }
    00064
    00070   inline T *EndOf()
    00071   {
    00072 #if !defined(__NDS__)
    00073     return endof(data);
    00074 #else
    00075     return &data[len];
    00076 #endif
    00077   }
    00078 };
    00079
    00088 template <typename T>
    00089 class ReusableBuffer {
    00090 private:
    00091   T *buffer;
    00092   size_t count;
    00093
    00094 public:
    00096   ReusableBuffer() : buffer(NULL), count(0) {}
    00098   ~ReusableBuffer() { free(this->buffer); }
    00099
    00107   T *Allocate(size_t count)
    00108   {
    00109     if (this->count < count) {
    00110       free(this->buffer);
    00111       this->buffer = MallocT<T>(count);
    00112       this->count = count;
    00113     }
    00114     return this->buffer;
    00115   }
    00116
    00124   T *ZeroAllocate(size_t count)
    00125   {
    00126     if (this->count < count) {
    00127       free(this->buffer);
    00128       this->buffer = CallocT<T>(count);
    00129       this->count = count;
    00130     } else {
    00131       memset(this->buffer, 0, sizeof(T) * count);
    00132     }
    00133     return this->buffer;
    00134   }
    00135
    00140   inline const T *GetBuffer() const
    00141   {
    00142     return this->buffer;
    00143   }
    00144 };
    00145
    00150 class ZeroedMemoryAllocator
    00151 {
    00152 public:
    00153   ZeroedMemoryAllocator() {}
    00154   virtual ~ZeroedMemoryAllocator() {}
    00155
    00161   inline void *operator new(size_t size) { return CallocT<byte>(size); }
    00162
    00168   inline void *operator new[](size_t size) { return CallocT<byte>(size); }
    00169
    00174   inline void operator delete(void *ptr) { free(ptr); }
    00175
    00180   inline void operator delete[](void *ptr) { free(ptr); }
    00181 };
    00182
    00187 template <typename T>
    00188 class AutoFreePtr
    00189 {
    00190   T *ptr;
    00191
    00192 public:
    00193   AutoFreePtr(T *ptr) : ptr(ptr) {}
    00194   ~AutoFreePtr() { free(this->ptr); }
    00195
    00200   inline void Assign(T *ptr)
    00201   {
    00202     free(this->ptr);
    00203     this->ptr = ptr;
    00204   }
    00205
    00207   inline T *operator ->() { return this->ptr; }
    00209   inline const T *operator ->() const { return this->ptr; }
    00210
    00212   inline operator T *() { return this->ptr; }
    00214   inline operator const T *() const { return this->ptr; }
    00215 };
    00216
    00217 #endif /* ALLOC_TYPE_HPP */