OpenTTD Source 20241224-master-gf74b0cf984
alloc_func.hpp File Reference

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

Go to the source code of this file.

Functions

void MallocError (size_t size)
 Function to exit with an error message after malloc() or calloc() have failed.
 
void ReallocError (size_t size)
 Function to exit with an error message after realloc() have failed.
 
void CheckAllocationConstraints (size_t element_size, size_t num_elements)
 Checks whether allocating memory would overflow size_t.
 
template<typename T >
void CheckAllocationConstraints (size_t num_elements)
 Checks whether allocating memory would overflow size_t.
 
template<typename T >
T * MallocT (size_t num_elements)
 Simplified allocation function that allocates the specified number of elements of the given type.
 
template<typename T >
T * CallocT (size_t num_elements)
 Simplified allocation function that allocates the specified number of elements of the given type.
 
template<typename T >
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

◆ CallocT()

template<typename T >
T * CallocT ( size_t  num_elements)
inline

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.
Template Parameters
Tthe type of the variable(s) to allocation.
Parameters
num_elementsthe number of elements to allocate of the given type.
Returns
nullptr when num_elements == 0, non-nullptr otherwise.

Definition at line 85 of file alloc_func.hpp.

References MallocError().

◆ CheckAllocationConstraints() [1/2]

void CheckAllocationConstraints ( size_t  element_size,
size_t  num_elements 
)
inline

Checks whether allocating memory would overflow size_t.

Parameters
element_sizeSize of the structure to allocate.
num_elementsNumber of elements to allocate.

Definition at line 29 of file alloc_func.hpp.

References MallocError().

Referenced by CheckAllocationConstraints().

◆ CheckAllocationConstraints() [2/2]

template<typename T >
void CheckAllocationConstraints ( size_t  num_elements)
inline

Checks whether allocating memory would overflow size_t.

Template Parameters
TStructure to allocate.
Parameters
num_elementsNumber of elements to allocate.

Definition at line 41 of file alloc_func.hpp.

References CheckAllocationConstraints().

◆ MallocError()

void MallocError ( size_t  size)

Function to exit with an error message after malloc() or calloc() have failed.

Parameters
sizenumber of bytes we tried to allocate

Definition at line 20 of file alloc_func.cpp.

Referenced by CallocT(), ScriptAllocator::CheckAllocation(), CheckAllocationConstraints(), and MallocT().

◆ MallocT()

template<typename T >
T * MallocT ( size_t  num_elements)
inline

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).
Template Parameters
Tthe type of the variable(s) to allocation.
Parameters
num_elementsthe number of elements to allocate of the given type.
Returns
nullptr when num_elements == 0, non-nullptr otherwise.

Definition at line 57 of file alloc_func.hpp.

References MallocError().

◆ ReallocError()

void ReallocError ( size_t  size)

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

Parameters
sizenumber of bytes we tried to allocate

Definition at line 29 of file alloc_func.cpp.

Referenced by ReallocT().

◆ ReallocT()

template<typename T >
T * ReallocT ( T *  t_ptr,
size_t  num_elements 
)
inline

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.
Template Parameters
Tthe type of the variable(s) to allocation.
Parameters
t_ptrthe previous allocation to extend/shrink.
num_elementsthe number of elements to allocate of the given type.
Returns
nullptr when num_elements == 0, non-nullptr otherwise.

Definition at line 111 of file alloc_func.hpp.

References free(), and ReallocError().

Referenced by DEFINE_POOL_METHOD().