rubidium@5838: /* $Id$ */ rubidium@5838: rubidium@8626: /** @file alloc_func.hpp Functions related to the allocation of memory */ belugas@6505: rubidium@8626: #ifndef ALLOC_FUNC_HPP rubidium@8626: #define ALLOC_FUNC_HPP rubidium@5838: rubidium@8626: /** rubidium@8626: * Simplified allocation function that allocates the specified number of rubidium@8626: * elements of the given type. It also explicitly casts it to the requested rubidium@8626: * type. rubidium@8626: * @note throws an error when there is no memory anymore. rubidium@8626: * @note the memory contains garbage data (i.e. possibly non-zero values). rubidium@8626: * @param T the type of the variable(s) to allocation. rubidium@8626: * @param num_elements the number of elements to allocate of the given type. rubidium@8626: * @return NULL when num_elements == 0, non-NULL otherwise. rubidium@8626: */ KUDr@5860: template FORCEINLINE T* MallocT(size_t num_elements) rubidium@5838: { rubidium@8626: /* rubidium@8626: * MorphOS cannot handle 0 elements allocations, or rather that always rubidium@8626: * returns NULL. So we do that for *all* allocations, thus causing it rubidium@8626: * to behave the same on all OSes. rubidium@8626: */ rubidium@8626: if (num_elements == 0) return NULL; rubidium@8626: KUDr@5860: T *t_ptr = (T*)malloc(num_elements * sizeof(T)); rubidium@8626: if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T)); KUDr@5860: return t_ptr; rubidium@5838: } rubidium@5838: rubidium@8626: /** rubidium@8626: * Simplified allocation function that allocates the specified number of rubidium@8626: * elements of the given type. It also explicitly casts it to the requested rubidium@8626: * type. rubidium@8626: * @note throws an error when there is no memory anymore. rubidium@8626: * @note the memory contains all zero values. rubidium@8626: * @param T the type of the variable(s) to allocation. rubidium@8626: * @param num_elements the number of elements to allocate of the given type. rubidium@8626: * @return NULL when num_elements == 0, non-NULL otherwise. rubidium@8626: */ rubidium@8626: template FORCEINLINE T* CallocT(size_t num_elements) rubidium@8626: { rubidium@8626: /* rubidium@8626: * MorphOS cannot handle 0 elements allocations, or rather that always rubidium@8626: * returns NULL. So we do that for *all* allocations, thus causing it rubidium@8626: * to behave the same on all OSes. rubidium@8626: */ rubidium@8626: if (num_elements == 0) return NULL; rubidium@5838: rubidium@8626: T *t_ptr = (T*)calloc(num_elements, sizeof(T)); rubidium@8626: if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T)); rubidium@8626: return t_ptr; rubidium@5838: } rubidium@5838: rubidium@8626: /** rubidium@8626: * Simplified reallocation function that allocates the specified number of rubidium@8626: * elements of the given type. It also explicitly casts it to the requested rubidium@8626: * type. It extends/shrinks the memory allocation given in t_ptr. rubidium@8626: * @note throws an error when there is no memory anymore. rubidium@8626: * @note the memory contains all zero values. rubidium@8626: * @param T the type of the variable(s) to allocation. rubidium@8626: * @param t_ptr the previous allocation to extend/shrink. rubidium@8626: * @param num_elements the number of elements to allocate of the given type. rubidium@8626: * @return NULL when num_elements == 0, non-NULL otherwise. rubidium@8626: */ rubidium@8626: template FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements) rubidium@8626: { rubidium@8626: /* rubidium@8626: * MorphOS cannot handle 0 elements allocations, or rather that always rubidium@8626: * returns NULL. So we do that for *all* allocations, thus causing it rubidium@8626: * to behave the same on all OSes. rubidium@8626: */ rubidium@8626: if (num_elements == 0) { rubidium@8626: free(t_ptr); rubidium@8626: return NULL; rubidium@8626: } rubidium@8626: rubidium@8626: t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); rubidium@8626: if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T)); rubidium@8626: return t_ptr; rubidium@8626: } rubidium@8626: rubidium@8872: /** rubidium@8872: * A small 'wrapper' for allocations that can be done on most OSes on the rubidium@8872: * stack, but are just too large to fit in the stack on devices with a small rubidium@8872: * stack such as the NDS. rubidium@8872: * So when it is possible a stack allocation is made, otherwise a heap rubidium@8872: * allocation is made and this is freed once the struct goes out of scope. rubidium@8872: * @param T the type to make the allocation for rubidium@8872: * @param length the amount of items to allocate rubidium@8872: */ rubidium@8872: template rubidium@8872: struct SmallStackSafeStackAlloc { rubidium@8872: #if !defined(__NDS__) rubidium@8872: /** Storing the data on the stack */ rubidium@8872: T data[length]; rubidium@8872: #else rubidium@8872: /** Storing it on the heap */ rubidium@8872: T *data; rubidium@8872: rubidium@8872: /** Allocating the memory */ rubidium@8872: SmallStackSafeStackAlloc() : data(MallocT(length)) {} rubidium@8872: /** And freeing when it goes out of scope */ rubidium@8872: ~SmallStackSafeStackAlloc() { free(data); } rubidium@8872: #endif rubidium@8872: rubidium@8872: /** rubidium@8872: * Gets a pointer to the data stored in this wrapper. rubidium@8872: * @return the pointer. rubidium@8872: */ rubidium@8872: operator T* () { return data; } rubidium@8872: }; rubidium@8872: rubidium@8626: #endif /* ALLOC_FUNC_HPP */