MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CheckAllocationAllowed

Method CheckAllocationAllowed

src/script/squirrel.cpp:56–68  ·  view source on GitHub ↗

* Checks whether an allocation is allowed by the memory limit set for the script. * @param requested_size The requested size that was requested to be allocated. * @throws Script_FatalError When memory may not be allocated (limit reached, except for error handling). */

Source from the content-addressed store, hash-verified

54 * @throws Script_FatalError When memory may not be allocated (limit reached, except for error handling).
55 */
56 void CheckAllocationAllowed(size_t requested_size)
57 {
58 /* When an error has been thrown, we are allocating just a bit of memory for the stack trace. */
59 if (this->error_thrown) return;
60
61 if (this->allocated_size + requested_size <= this->allocation_limit) return;
62
63 /* Do not allow allocating more than the allocation limit. */
64 this->error_thrown = true;
65 std::string msg = fmt::format("Maximum memory allocation exceeded by {} bytes when allocating {} bytes",
66 this->allocated_size + requested_size - this->allocation_limit, requested_size);
67 throw Script_FatalError(msg);
68 }
69
70 /**
71 * Internal helper to allocate the given amount of bytes.

Callers 2

MallocMethod · 0.95
ReallocMethod · 0.95

Calls 2

Script_FatalErrorClass · 0.85
formatFunction · 0.50

Tested by

no test coverage detected