* Internal helper to allocate the given amount of bytes. * @param requested_size The requested size. * @return The allocated memory. * @throws Script_FatalError When memory could not be allocated. */
| 74 | * @throws Script_FatalError When memory could not be allocated. |
| 75 | */ |
| 76 | void *DoAlloc(SQUnsignedInteger requested_size) |
| 77 | { |
| 78 | try { |
| 79 | void *p = this->allocator.allocate(requested_size); |
| 80 | assert(p != nullptr); |
| 81 | this->allocated_size += requested_size; |
| 82 | |
| 83 | #ifdef SCRIPT_DEBUG_ALLOCATIONS |
| 84 | assert(this->allocations.find(p) == this->allocations.end()); |
| 85 | this->allocations[p] = requested_size; |
| 86 | #endif |
| 87 | return p; |
| 88 | } catch (const std::bad_alloc &) { |
| 89 | /* The OS did not have enough memory to allocate the object, regardless of the |
| 90 | * limit imposed by OpenTTD on the amount of memory that may be allocated. */ |
| 91 | if (this->error_thrown) { |
| 92 | /* The allocation is called in the error handling of a memory allocation |
| 93 | * failure, then not being able to allocate that small amount of memory |
| 94 | * means there is no other choice than to bug out completely. */ |
| 95 | FatalError("Out of memory. Cannot allocate {} bytes", requested_size); |
| 96 | } |
| 97 | |
| 98 | this->error_thrown = true; |
| 99 | std::string msg = fmt::format("Out of memory. Cannot allocate {} bytes", requested_size); |
| 100 | throw Script_FatalError(msg); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public: |
| 105 | size_t GetAllocatedSize() const { return this->allocated_size; } |