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: /** smatz@9033: * Functions to exit badly with an error message. smatz@9033: * It has to be linked so the error messages are not smatz@9033: * duplicated in each object file making the final smatz@9033: * binary needlessly large. smatz@9033: */ smatz@10246: void NORETURN MallocError(size_t size); smatz@10246: void NORETURN ReallocError(size_t size); smatz@9033: smatz@9033: /** 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: */ skidd13@11049: template skidd13@11049: static 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)); smatz@9033: if (t_ptr == NULL) MallocError(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: */ skidd13@11049: template skidd13@11049: static 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)); smatz@9033: if (t_ptr == NULL) MallocError(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: */ skidd13@11049: template skidd13@11050: static 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)); smatz@9033: if (t_ptr == NULL) ReallocError(num_elements * sizeof(T)); rubidium@8626: return t_ptr; rubidium@8626: } rubidium@8626: smatz@10905: /** alloca() has to be called in the parent function, so define AllocaM() as a macro */ smatz@10905: #define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T))) smatz@10905: rubidium@8626: #endif /* ALLOC_FUNC_HPP */