OpenTTD Source 20241224-master-gee860a5c8e
ScriptAllocator Struct Reference

In the memory allocator for Squirrel we want to directly use malloc/realloc, so when the OS does not have enough memory the game does not go into unrecoverable error mode and kill the whole game, but rather let the AI die though then we need to circumvent MallocT/ReallocT. More...

Public Member Functions

void CheckLimit () const
 
void CheckAllocation (size_t requested_size, void *p)
 Catch all validation for the allocation; did it allocate too much memory according to the allocation limit or did the allocation at the OS level maybe fail? In those error situations a Script_FatalError is thrown, but once that has been done further allocations are allowed to make it possible for Squirrel to throw the error and clean everything up.
 
void * Malloc (SQUnsignedInteger size)
 
void * Realloc (void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
 
void Free (void *p, SQUnsignedInteger size)
 

Data Fields

size_t allocated_size
 Sum of allocated data size.
 
size_t allocation_limit
 Maximum this allocator may use before allocations fail.
 
bool error_thrown
 Whether the error has already been thrown, so to not throw secondary errors in the handling of the allocation error.
 

Static Public Attributes

static const size_t SAFE_LIMIT = 0x8000000
 128 MiB, a safe choice for almost any situation
 

Detailed Description

In the memory allocator for Squirrel we want to directly use malloc/realloc, so when the OS does not have enough memory the game does not go into unrecoverable error mode and kill the whole game, but rather let the AI die though then we need to circumvent MallocT/ReallocT.

So no include "../safeguards.h" here as is required, but after the allocator's implementation.

Definition at line 36 of file squirrel.cpp.

Constructor & Destructor Documentation

◆ ScriptAllocator()

ScriptAllocator::ScriptAllocator ( )
inline

Definition at line 164 of file squirrel.cpp.

◆ ~ScriptAllocator()

ScriptAllocator::~ScriptAllocator ( )
inline

Definition at line 172 of file squirrel.cpp.

Member Function Documentation

◆ CheckAllocation()

void ScriptAllocator::CheckAllocation ( size_t  requested_size,
void *  p 
)
inline

Catch all validation for the allocation; did it allocate too much memory according to the allocation limit or did the allocation at the OS level maybe fail? In those error situations a Script_FatalError is thrown, but once that has been done further allocations are allowed to make it possible for Squirrel to throw the error and clean everything up.

Parameters
requested_sizeThe requested size that was requested to be allocated.
pThe pointer to the allocated object, or null if allocation failed.

Definition at line 67 of file squirrel.cpp.

References free(), and MallocError().

◆ CheckLimit()

void ScriptAllocator::CheckLimit ( ) const
inline

Definition at line 53 of file squirrel.cpp.

◆ Free()

void ScriptAllocator::Free ( void *  p,
SQUnsignedInteger  size 
)
inline

Definition at line 152 of file squirrel.cpp.

◆ Malloc()

void * ScriptAllocator::Malloc ( SQUnsignedInteger  size)
inline

Definition at line 97 of file squirrel.cpp.

◆ Realloc()

void * ScriptAllocator::Realloc ( void *  p,
SQUnsignedInteger  oldsize,
SQUnsignedInteger  size 
)
inline

Definition at line 114 of file squirrel.cpp.

Field Documentation

◆ allocated_size

size_t ScriptAllocator::allocated_size

Sum of allocated data size.

Definition at line 37 of file squirrel.cpp.

◆ allocation_limit

size_t ScriptAllocator::allocation_limit

Maximum this allocator may use before allocations fail.

Definition at line 38 of file squirrel.cpp.

◆ error_thrown

bool ScriptAllocator::error_thrown

Whether the error has already been thrown, so to not throw secondary errors in the handling of the allocation error.

This as the handling of the error will throw a Squirrel error so the Squirrel stack can be dumped, however that gets allocated by this allocator and then you might end up in an infinite loop.

Definition at line 45 of file squirrel.cpp.

◆ SAFE_LIMIT

const size_t ScriptAllocator::SAFE_LIMIT = 0x8000000
static

128 MiB, a safe choice for almost any situation

Definition at line 47 of file squirrel.cpp.


The documentation for this struct was generated from the following file: