# alloc_type.hpp

Go to the documentation of this file.

    00001 /* $Id: alloc_type.hpp 18042 2009-11-11 20:53:23Z rubidium $ */
    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   FORCEINLINE operator T *()
    00052   {
    00053     return data;
    00054   }
    00055
    00060   FORCEINLINE T *operator -> ()
    00061   {
    00062     return data;
    00063   }
    00064
    00070   FORCEINLINE 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   FORCEINLINE 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   FORCEINLINE void *operator new(size_t size) { return CallocT<byte>(size); }
    00162
    00168   FORCEINLINE void *operator new[](size_t size) { return CallocT<byte>(size); }
    00169
    00174   FORCEINLINE void operator delete(void *ptr) { free(ptr); }
    00175
    00180   FORCEINLINE void operator delete[](void *ptr) { free(ptr); }
    00181 };
    00182
    00183 #endif /* ALLOC_TYPE_HPP */