@@ -60,97 +60,100 @@ struct ScriptAllocator {
/**
* 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.
* @param requested_size The requested size that was requested to be allocated.
* @param p The pointer to the allocated object, or null if allocation failed.
*/
void CheckAllocation(size_t requested_size, void *p)
{
if (this->allocated_size > this->allocation_limit && !this->error_thrown) {
if (this->allocated_size + requested_size > this->allocation_limit && !this->error_thrown) {
/* Do not allow allocating more than the allocation limit, except when an error is
* already as then the allocation is for throwing that error in Squirrel, the
* associated stack trace information and while cleaning up the AI. */
this->error_thrown = true;
char buff[128];
seprintf(buff, lastof(buff), "Maximum memory allocation exceeded by " PRINTF_SIZE " bytes when allocating " PRINTF_SIZE " bytes",
this->allocated_size - this->allocation_limit, requested_size);
this->allocated_size + requested_size - this->allocation_limit, requested_size);
/* Don't leak the rejected allocation. */
free(p);
p = nullptr;
/* Allocation rejected, don't count it. */
this->allocated_size -= requested_size;
throw Script_FatalError(buff);
}
if (p == nullptr) {
/* The OS did not have enough memory to allocate the object, regardless of the
* limit imposed by OpenTTD on the amount of memory that may be allocated. */
if (this->error_thrown) {
/* The allocation is called in the error handling of a memory allocation
* failure, then not being able to allocate that small amount of memory
* means there is no other choice than to bug out completely. */
MallocError(requested_size);
char buff[64];
seprintf(buff, lastof(buff), "Out of memory. Cannot allocate " PRINTF_SIZE " bytes", requested_size);
/* Allocation failed, don't count it. */
void *Malloc(SQUnsignedInteger size)
void *p = malloc(size);
this->allocated_size += size;
this->CheckAllocation(size, p);
#ifdef SCRIPT_DEBUG_ALLOCATIONS
assert(p != nullptr);
assert(this->allocations.find(p) == this->allocations.end());
this->allocations[p] = size;
#endif
return p;
void *Realloc(void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
return this->Malloc(size);
if (size == 0) {
this->Free(p, oldsize);
return nullptr;
assert(this->allocations[p] == oldsize);
this->allocations.erase(p);
/* Can't use realloc directly because memory limit check.
* If memory exception is thrown, the old pointer is expected
* to be valid for engine cleanup.
void *new_p = malloc(size);
void *new_p = realloc(p, size);
this->CheckAllocation(size - oldsize, new_p);
/* Memory limit test passed, we can copy data and free old pointer. */
memcpy(new_p, p, std::min(oldsize, size));
this->allocated_size -= oldsize;
assert(new_p != nullptr);
this->allocations[new_p] = size;
return new_p;
void Free(void *p, SQUnsignedInteger size)
if (p == nullptr) return;
Status change: