Files
@ r10542:27034819446e
Branch filter:
Location: cpp/openttd-patchpack/source/src/core/alloc_func.hpp - annotation
r10542:27034819446e
3.4 KiB
text/x-c++hdr
(svn r14799) -Codechange: move drawing a single settings entry to it's own function (Alberth)
r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8537:706233dcd59f r8537:706233dcd59f r8537:706233dcd59f r8537:706233dcd59f r8537:706233dcd59f r8984:b9a96c1e0b58 r8984:b9a96c1e0b58 r8537:706233dcd59f r8537:706233dcd59f r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r9575:665324c39b56 r9575:665324c39b56 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8537:706233dcd59f r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r9575:665324c39b56 r9575:665324c39b56 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8537:706233dcd59f r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r9575:665324c39b56 r9576:65e400bfeb96 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r8537:706233dcd59f r8130:bece2e5c7460 r8130:bece2e5c7460 r8130:bece2e5c7460 r9488:2873382f8f4a r9488:2873382f8f4a r9488:2873382f8f4a r8130:bece2e5c7460 | /* $Id$ */
/** @file alloc_func.hpp Functions related to the allocation of memory */
#ifndef ALLOC_FUNC_HPP
#define ALLOC_FUNC_HPP
/**
* Functions to exit badly with an error message.
* It has to be linked so the error messages are not
* duplicated in each object file making the final
* binary needlessly large.
*/
void NORETURN MallocError(size_t size);
void NORETURN ReallocError(size_t size);
/**
* Simplified allocation function that allocates the specified number of
* elements of the given type. It also explicitly casts it to the requested
* type.
* @note throws an error when there is no memory anymore.
* @note the memory contains garbage data (i.e. possibly non-zero values).
* @param T the type of the variable(s) to allocation.
* @param num_elements the number of elements to allocate of the given type.
* @return NULL when num_elements == 0, non-NULL otherwise.
*/
template <typename T>
static FORCEINLINE T *MallocT(size_t num_elements)
{
/*
* MorphOS cannot handle 0 elements allocations, or rather that always
* returns NULL. So we do that for *all* allocations, thus causing it
* to behave the same on all OSes.
*/
if (num_elements == 0) return NULL;
T *t_ptr = (T*)malloc(num_elements * sizeof(T));
if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
return t_ptr;
}
/**
* Simplified allocation function that allocates the specified number of
* elements of the given type. It also explicitly casts it to the requested
* type.
* @note throws an error when there is no memory anymore.
* @note the memory contains all zero values.
* @param T the type of the variable(s) to allocation.
* @param num_elements the number of elements to allocate of the given type.
* @return NULL when num_elements == 0, non-NULL otherwise.
*/
template <typename T>
static FORCEINLINE T *CallocT(size_t num_elements)
{
/*
* MorphOS cannot handle 0 elements allocations, or rather that always
* returns NULL. So we do that for *all* allocations, thus causing it
* to behave the same on all OSes.
*/
if (num_elements == 0) return NULL;
T *t_ptr = (T*)calloc(num_elements, sizeof(T));
if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
return t_ptr;
}
/**
* Simplified reallocation function that allocates the specified number of
* elements of the given type. It also explicitly casts it to the requested
* type. It extends/shrinks the memory allocation given in t_ptr.
* @note throws an error when there is no memory anymore.
* @note the memory contains all zero values.
* @param T the type of the variable(s) to allocation.
* @param t_ptr the previous allocation to extend/shrink.
* @param num_elements the number of elements to allocate of the given type.
* @return NULL when num_elements == 0, non-NULL otherwise.
*/
template <typename T>
static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
{
/*
* MorphOS cannot handle 0 elements allocations, or rather that always
* returns NULL. So we do that for *all* allocations, thus causing it
* to behave the same on all OSes.
*/
if (num_elements == 0) {
free(t_ptr);
return NULL;
}
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
return t_ptr;
}
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
#define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
#endif /* ALLOC_FUNC_HPP */
|