* 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). */
| 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. |
no test coverage detected