# alloc_func.hpp File Reference

Functions related to the allocation of memory. More...

Go to the source code of this file.

## Defines
#define | AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
| alloca() has to be called in the parent function, so define AllocaM() as a macro
## Functions
void NORETURN | MallocError (size_t size)
| Functions to exit badly with an error message.
void NORETURN | ReallocError (size_t size)
| Function to exit with an error message after realloc() have failed.
template<typename T>
static FORCEINLINE T * | MallocT (size_t num_elements)
| Simplified allocation function that allocates the specified number of elements of the given type.
template<typename T>
static FORCEINLINE T * | CallocT (size_t num_elements)
| Simplified allocation function that allocates the specified number of elements of the given type.
template<typename T>
static FORCEINLINE T * | ReallocT (T *t_ptr, size_t num_elements)
| Simplified reallocation function that allocates the specified number of elements of the given type.

## Detailed Description

Functions related to the allocation of memory.

Definition in file alloc_func.hpp.

## Function Documentation

template<typename T>

static FORCEINLINE T* CallocT | ( | size_t | num_elements | ) | `[inline, static]`

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.

  the memory contains all zero values.

Parameters:

  | T | the type of the variable(s) to allocation.
  | num_elements | the number of elements to allocate of the given type.

Returns:
  NULL when num_elements == 0, non-NULL otherwise.

Definition at line 53 of file alloc_func.hpp.

References MallocError().

void NORETURN MallocError | ( | size_t | size | ) |

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.

Functions to exit badly with an error message.

Parameters:

  | size | number of bytes we tried to allocate

Definition at line 12 of file alloc_func.cpp.

References error.

Referenced by CallocT(), and MallocT().

template<typename T>

static FORCEINLINE T* MallocT | ( | size_t | num_elements | ) | `[inline, static]`

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.

  the memory contains garbage data (i.e. possibly non-zero values).

Parameters:

  | T | the type of the variable(s) to allocation.
  | num_elements | the number of elements to allocate of the given type.

Returns:
  NULL when num_elements == 0, non-NULL otherwise.

Definition at line 28 of file alloc_func.hpp.

References MallocError().

void NORETURN ReallocError | ( | size_t | size | ) |

Function to exit with an error message after realloc() have failed.

Parameters:

  | size | number of bytes we tried to allocate

Definition at line 21 of file alloc_func.cpp.

References error.

Referenced by ReallocT().

template<typename T>

static FORCEINLINE T* ReallocT | ( | T * | t_ptr,
|  | size_t | num_elements |
| ) |  |  | `[inline, static]`

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.

  the pointer to the data may change, but the data will remain valid.

Parameters:

  | T | the type of the variable(s) to allocation.
  | t_ptr | the previous allocation to extend/shrink.
  | num_elements | the number of elements to allocate of the given type.

Returns:
  NULL when num_elements == 0, non-NULL otherwise.

Definition at line 79 of file alloc_func.hpp.

References ReallocError().

Referenced by OldMemoryPoolBase::AddBlockToPool(), AllocateSpecToStation(), SmallVector< EngineIDMapping, 256 >::Append(), SmallVector< EngineIDMapping, 256 >::Compact(), DeallocateSpecFromStation(), GamelogChange(), GetNewEngine(), IniFile::LoadFromDisk(), SquirrelStd::require(), and SetCustomStationSpec().