rubidium@8925: /* $Id$ */ rubidium@8925: rubidium@8925: /** @file alloc_type.hpp Helper types related to the allocation of memory */ rubidium@8925: rubidium@8925: #ifndef ALLOC_TYPE_HPP rubidium@8925: #define ALLOC_TYPE_HPP rubidium@8925: rubidium@8925: #include "alloc_func.hpp" rubidium@8925: rubidium@8925: /** rubidium@8925: * A small 'wrapper' for allocations that can be done on most OSes on the rubidium@8925: * stack, but are just too large to fit in the stack on devices with a small rubidium@8925: * stack such as the NDS. rubidium@8925: * So when it is possible a stack allocation is made, otherwise a heap rubidium@8925: * allocation is made and this is freed once the struct goes out of scope. rubidium@8925: * @param T the type to make the allocation for rubidium@8925: * @param length the amount of items to allocate rubidium@8925: */ rubidium@8925: template rubidium@8925: struct SmallStackSafeStackAlloc { rubidium@8925: #if !defined(__NDS__) rubidium@8925: /** Storing the data on the stack */ rubidium@8925: T data[length]; rubidium@8925: #else rubidium@8925: /** Storing it on the heap */ rubidium@8925: T *data; rubidium@8925: /** The length (in elements) of data in this allocator. */ rubidium@8925: size_t len; rubidium@8925: rubidium@8925: /** Allocating the memory */ rubidium@8925: SmallStackSafeStackAlloc() : data(MallocT(length)), len(length) {} rubidium@8925: /** And freeing when it goes out of scope */ rubidium@8925: ~SmallStackSafeStackAlloc() { free(data); } rubidium@8925: #endif rubidium@8925: rubidium@8925: /** rubidium@8925: * Gets a pointer to the data stored in this wrapper. rubidium@8925: * @return the pointer. rubidium@8925: */ rubidium@8925: inline operator T* () { return data; } rubidium@8925: rubidium@8925: /** rubidium@8925: * Gets a pointer to the data stored in this wrapper. rubidium@8925: * @return the pointer. rubidium@8925: */ rubidium@8925: inline T* operator -> () { return data; } rubidium@8925: rubidium@8925: /** rubidium@8925: * Gets a pointer to the last data element stored in this wrapper. rubidium@8925: * @note needed because endof does not work properly for pointers. rubidium@8925: * @return the 'endof' pointer. rubidium@8925: */ rubidium@8925: inline T* EndOf() { rubidium@8925: #if !defined(__NDS__) rubidium@8925: return endof(data); rubidium@8925: #else rubidium@8925: return &data[len]; rubidium@8925: #endif rubidium@8925: } rubidium@8925: }; rubidium@8925: rubidium@8925: /** rubidium@8925: * Base class that provides memory initialization on dynamically created objects. rubidium@8925: * All allocated memory will be zeroed. rubidium@8925: */ rubidium@8925: class ZeroedMemoryAllocator rubidium@8925: { rubidium@8925: public: rubidium@8925: ZeroedMemoryAllocator() {} rubidium@8925: virtual ~ZeroedMemoryAllocator() {} rubidium@8925: rubidium@8925: /** rubidium@8925: * Memory allocator for a single class instance. rubidium@8925: * @param size the amount of bytes to allocate. rubidium@8925: * @return the given amounts of bytes zeroed. rubidium@8925: */ rubidium@8925: void *operator new(size_t size) { return CallocT(size); } rubidium@8925: rubidium@8925: /** rubidium@8925: * Memory allocator for an array of class instances. rubidium@8925: * @param size the amount of bytes to allocate. rubidium@8925: * @return the given amounts of bytes zeroed. rubidium@8925: */ rubidium@8925: void *operator new[](size_t size) { return CallocT(size); } rubidium@8925: rubidium@8925: /** rubidium@8925: * Memory release for a single class instance. rubidium@8925: * @param ptr the memory to free. rubidium@8925: * @param size the amount of allocated memory (unused). rubidium@8957: * rubidium@8957: * @warning The value of the \a size parameter can only be trusted for rubidium@8957: * classes that have their own (virtual) destructor method. rubidium@8925: */ rubidium@8925: void operator delete(void *ptr, size_t size) { free(ptr); } rubidium@8925: rubidium@8925: /** rubidium@8925: * Memory release for an array of class instances. rubidium@8925: * @param ptr the memory to free. rubidium@8925: * @param size the amount of allocated memory (unused). rubidium@8957: * rubidium@8957: * @warning The value of the \a size parameter can only be trusted for rubidium@8957: * classes that have their own (virtual) destructor method. rubidium@8925: */ rubidium@8925: void operator delete[](void *ptr, size_t size) { free(ptr); } rubidium@8925: }; rubidium@8925: rubidium@8925: #endif /* ALLOC_TYPE_HPP */