# alloc_type.hpp

Go to the documentation of this file.

    00001 /* $Id: alloc_type.hpp 18047 2009-11-11 22:09:34Z rubidium $ */
    00002
    00005 #ifndef ALLOC_TYPE_HPP
    00006 #define ALLOC_TYPE_HPP
    00007
    00008 #include "alloc_func.hpp"
    00009
    00019 template <typename T, size_t length>
    00020 struct SmallStackSafeStackAlloc {
    00021 #if !defined(__NDS__)
    00022
    00023   T data[length];
    00024 #else
    00025
    00026   T *data;
    00028   size_t len;
    00029
    00031   SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
    00032
    00034   ~SmallStackSafeStackAlloc()
    00035   {
    00036     free(data);
    00037   }
    00038 #endif
    00039
    00044   FORCEINLINE operator T *()
    00045   {
    00046     return data;
    00047   }
    00048
    00053   FORCEINLINE T *operator -> ()
    00054   {
    00055     return data;
    00056   }
    00057
    00063   FORCEINLINE T *EndOf()
    00064   {
    00065 #if !defined(__NDS__)
    00066     return endof(data);
    00067 #else
    00068     return &data[len];
    00069 #endif
    00070   }
    00071 };
    00072
    00081 template <typename T>
    00082 class ReusableBuffer {
    00083 private:
    00084   T *buffer;
    00085   size_t count;
    00086
    00087 public:
    00089   ReusableBuffer() : buffer(NULL), count(0) {}
    00091   ~ReusableBuffer() { free(this->buffer); }
    00092
    00100   T *Allocate(size_t count)
    00101   {
    00102     if (this->count < count) {
    00103       free(this->buffer);
    00104       this->buffer = MallocT<T>(count);
    00105       this->count = count;
    00106     }
    00107     return this->buffer;
    00108   }
    00109
    00117   T *ZeroAllocate(size_t count)
    00118   {
    00119     if (this->count < count) {
    00120       free(this->buffer);
    00121       this->buffer = CallocT<T>(count);
    00122       this->count = count;
    00123     } else {
    00124       memset(this->buffer, 0, sizeof(T) * count);
    00125     }
    00126     return this->buffer;
    00127   }
    00128
    00133   FORCEINLINE const T *GetBuffer() const
    00134   {
    00135     return this->buffer;
    00136   }
    00137 };
    00138
    00143 class ZeroedMemoryAllocator
    00144 {
    00145 public:
    00146   ZeroedMemoryAllocator() {}
    00147   virtual ~ZeroedMemoryAllocator() {}
    00148
    00154   FORCEINLINE void *operator new(size_t size) { return CallocT<byte>(size); }
    00155
    00161   FORCEINLINE void *operator new[](size_t size) { return CallocT<byte>(size); }
    00162
    00167   FORCEINLINE void operator delete(void *ptr, size_t size) { free(ptr); }
    00168
    00173   FORCEINLINE void operator delete[](void *ptr, size_t size) { free(ptr); }
    00174 };
    00175
    00176 #endif /* ALLOC_TYPE_HPP */