rubidium@8130: /* $Id$ */ rubidium@8130: rubidium@8130: /** @file alloc_func.hpp Functions related to the allocation of memory */ rubidium@8130: rubidium@8130: #ifndef ALLOC_FUNC_HPP rubidium@8130: #define ALLOC_FUNC_HPP rubidium@8130: rubidium@8130: /** smatz@8537: * Functions to exit badly with an error message. smatz@8537: * It has to be linked so the error messages are not smatz@8537: * duplicated in each object file making the final smatz@8537: * binary needlessly large. smatz@8537: */ smatz@8984: void NORETURN MallocError(size_t size); smatz@8984: void NORETURN ReallocError(size_t size); smatz@8537: smatz@8537: /** rubidium@8130: * Simplified allocation function that allocates the specified number of rubidium@8130: * elements of the given type. It also explicitly casts it to the requested rubidium@8130: * type. rubidium@8130: * @note throws an error when there is no memory anymore. rubidium@8130: * @note the memory contains garbage data (i.e. possibly non-zero values). rubidium@8130: * @param T the type of the variable(s) to allocation. rubidium@8130: * @param num_elements the number of elements to allocate of the given type. rubidium@8130: * @return NULL when num_elements == 0, non-NULL otherwise. rubidium@8130: */ rubidium@8130: template FORCEINLINE T* MallocT(size_t num_elements) rubidium@8130: { rubidium@8130: /* rubidium@8130: * MorphOS cannot handle 0 elements allocations, or rather that always rubidium@8130: * returns NULL. So we do that for *all* allocations, thus causing it rubidium@8130: * to behave the same on all OSes. rubidium@8130: */ rubidium@8130: if (num_elements == 0) return NULL; rubidium@8130: rubidium@8130: T *t_ptr = (T*)malloc(num_elements * sizeof(T)); smatz@8537: if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); rubidium@8130: return t_ptr; rubidium@8130: } rubidium@8130: rubidium@8130: /** rubidium@8130: * Simplified allocation function that allocates the specified number of rubidium@8130: * elements of the given type. It also explicitly casts it to the requested rubidium@8130: * type. rubidium@8130: * @note throws an error when there is no memory anymore. rubidium@8130: * @note the memory contains all zero values. rubidium@8130: * @param T the type of the variable(s) to allocation. rubidium@8130: * @param num_elements the number of elements to allocate of the given type. rubidium@8130: * @return NULL when num_elements == 0, non-NULL otherwise. rubidium@8130: */ rubidium@8130: template FORCEINLINE T* CallocT(size_t num_elements) rubidium@8130: { rubidium@8130: /* rubidium@8130: * MorphOS cannot handle 0 elements allocations, or rather that always rubidium@8130: * returns NULL. So we do that for *all* allocations, thus causing it rubidium@8130: * to behave the same on all OSes. rubidium@8130: */ rubidium@8130: if (num_elements == 0) return NULL; rubidium@8130: rubidium@8130: T *t_ptr = (T*)calloc(num_elements, sizeof(T)); smatz@8537: if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); rubidium@8130: return t_ptr; rubidium@8130: } rubidium@8130: rubidium@8130: /** rubidium@8130: * Simplified reallocation function that allocates the specified number of rubidium@8130: * elements of the given type. It also explicitly casts it to the requested rubidium@8130: * type. It extends/shrinks the memory allocation given in t_ptr. rubidium@8130: * @note throws an error when there is no memory anymore. rubidium@8130: * @note the memory contains all zero values. rubidium@8130: * @param T the type of the variable(s) to allocation. rubidium@8130: * @param t_ptr the previous allocation to extend/shrink. rubidium@8130: * @param num_elements the number of elements to allocate of the given type. rubidium@8130: * @return NULL when num_elements == 0, non-NULL otherwise. rubidium@8130: */ rubidium@8130: template FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements) rubidium@8130: { rubidium@8130: /* rubidium@8130: * MorphOS cannot handle 0 elements allocations, or rather that always rubidium@8130: * returns NULL. So we do that for *all* allocations, thus causing it rubidium@8130: * to behave the same on all OSes. rubidium@8130: */ rubidium@8130: if (num_elements == 0) { rubidium@8130: free(t_ptr); rubidium@8130: return NULL; rubidium@8130: } rubidium@8130: rubidium@8130: t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); smatz@8537: if (t_ptr == NULL) ReallocError(num_elements * sizeof(T)); rubidium@8130: return t_ptr; rubidium@8130: } rubidium@8130: rubidium@8130: #endif /* ALLOC_FUNC_HPP */