OpenTTD Source 20250312-master-gcdcc6b491d
ScriptAllocator Struct Reference

Public Member Functions

size_t GetAllocatedSize () const
 
void CheckLimit () const
 
void Reset ()
 
void * Malloc (SQUnsignedInteger size)
 
void * Realloc (void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
 
void Free (void *p, SQUnsignedInteger size)
 

Private Member Functions

void CheckAllocationAllowed (size_t requested_size)
 Checks whether an allocation is allowed by the memory limit set for the script.
 
void * DoAlloc (SQUnsignedInteger requested_size)
 Internal helper to allocate the given amount of bytes.
 

Private Attributes

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

Static Private Attributes

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

Detailed Description

Definition at line 30 of file squirrel.cpp.

Constructor & Destructor Documentation

◆ ScriptAllocator()

ScriptAllocator::ScriptAllocator ( )
inline

Definition at line 153 of file squirrel.cpp.

◆ ~ScriptAllocator()

ScriptAllocator::~ScriptAllocator ( )
inline

Definition at line 159 of file squirrel.cpp.

Member Function Documentation

◆ CheckAllocationAllowed()

void ScriptAllocator::CheckAllocationAllowed ( size_t  requested_size)
inlineprivate

Checks whether an allocation is allowed by the memory limit set for the script.

Parameters
requested_sizeThe requested size that was requested to be allocated.
Exceptions
Script_FatalErrorWhen memory may not be allocated (limit reached, except for error handling).

Definition at line 54 of file squirrel.cpp.

References allocation_limit.

◆ CheckLimit()

void ScriptAllocator::CheckLimit ( ) const
inline

Definition at line 105 of file squirrel.cpp.

◆ DoAlloc()

void * ScriptAllocator::DoAlloc ( SQUnsignedInteger  requested_size)
inlineprivate

Internal helper to allocate the given amount of bytes.

Parameters
requested_sizeThe requested size.
Returns
The allocated memory.
Exceptions
Script_FatalErrorWhen memory could not be allocated.

Definition at line 74 of file squirrel.cpp.

◆ Free()

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

Definition at line 141 of file squirrel.cpp.

◆ GetAllocatedSize()

size_t ScriptAllocator::GetAllocatedSize ( ) const
inline

Definition at line 103 of file squirrel.cpp.

◆ Malloc()

void * ScriptAllocator::Malloc ( SQUnsignedInteger  size)
inline

Definition at line 116 of file squirrel.cpp.

◆ Realloc()

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

Definition at line 122 of file squirrel.cpp.

◆ Reset()

void ScriptAllocator::Reset ( )
inline

Definition at line 110 of file squirrel.cpp.

Field Documentation

◆ allocated_size

size_t ScriptAllocator::allocated_size = 0
private

Sum of allocated data size.

Definition at line 33 of file squirrel.cpp.

◆ allocation_limit

size_t ScriptAllocator::allocation_limit
private

Maximum this allocator may use before allocations fail.

Definition at line 34 of file squirrel.cpp.

Referenced by CheckAllocationAllowed().

◆ allocator

std::allocator<uint8_t> ScriptAllocator::allocator
private

Definition at line 32 of file squirrel.cpp.

◆ error_thrown

bool ScriptAllocator::error_thrown = false
private

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 41 of file squirrel.cpp.

◆ SAFE_LIMIT

const size_t ScriptAllocator::SAFE_LIMIT = 0x8000000
staticprivate

128 MiB, a safe choice for almost any situation

Definition at line 43 of file squirrel.cpp.


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