OpenTTD Source: src/core/alloc_func.hpp File Reference

OpenTTD Source 13.2.1

Macros | Functions
alloc_func.hpp File Reference

Go to the source code of this file.

## Macros
#define | AllocaM(T, num_elements)
| alloca() has to be called in the parent function, so define AllocaM() as a macro More...

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

## Detailed Description

Functions related to the allocation of memory

Definition in file alloc_func.hpp.

## Macro Definition Documentation

## ◆ AllocaM

#define AllocaM | ( |  | T,
|  |  | num_elements
| ) |  |

Value:
(CheckAllocationConstraints<T>(num_elements), \
(T*)alloca((num_elements) * sizeof(T)))

alloca() has to be called in the parent function, so define AllocaM() as a macro

Definition at line 132 of file alloc_func.hpp.

## Function Documentation

## ◆ CallocT()

template<typename T >

static T* CallocT | ( | size_t | num_elements | ) |
| inlinestatic

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

  T | the type of the variable(s) to allocation.

Parameters

  num_elements | the 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.

## ◆ CheckAllocationConstraints() [1/2]

static void CheckAllocationConstraints | ( | size_t | element_size,
|  | size_t | num_elements
| ) |  |
| inlinestatic

Checks whether allocating memory would overflow size_t.

Parameters

  element_size | Size of the structure to allocate.
  num_elements | Number of elements to allocate.

Definition at line 29 of file alloc_func.hpp.

Referenced by CheckAllocationConstraints(), and Hash::Init().

## ◆ CheckAllocationConstraints() [2/2]

template<typename T >

static void CheckAllocationConstraints | ( | size_t | num_elements | ) |
| inlinestatic

Checks whether allocating memory would overflow size_t.

Template Parameters

  T | Structure to allocate.

Parameters

  num_elements | Number of elements to allocate.

Definition at line 41 of file alloc_func.hpp.

References CheckAllocationConstraints().

## ◆ MallocError()

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

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

Parameters

  size | number of bytes we tried to allocate

Definition at line 18 of file alloc_func.cpp.

References error().

## ◆ MallocT()

template<typename T >

static T* MallocT | ( | size_t | num_elements | ) |
| inlinestatic

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

  T | the type of the variable(s) to allocation.

Parameters

  num_elements | the 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.

## ◆ ReallocError()

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 27 of file alloc_func.cpp.

References error().

## ◆ ReallocT()

template<typename T >

static T* ReallocT | ( | T * | t_ptr,
|  | size_t | num_elements
| ) |  |
| inlinestatic

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

  T | the type of the variable(s) to allocation.

Parameters

  t_ptr | the previous allocation to extend/shrink.
  num_elements | the 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.

Referenced by SmallMatrix< BaseEdge >::Compact(), GamelogChange(), Gunzip(), IniLoadFile::LoadFromDisk(), TextfileWindow::LoadTextfile(), and NetworkReInitChatBoxSize().