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