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