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

Method DoAlloc

src/script/squirrel.cpp:76–102  ·  view source on GitHub ↗

* 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. */

Source from the content-addressed store, hash-verified

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
104public:
105 size_t GetAllocatedSize() const { return this->allocated_size; }

Callers 2

MallocMethod · 0.95
ReallocMethod · 0.95

Calls 5

Script_FatalErrorClass · 0.85
allocateMethod · 0.80
formatFunction · 0.50
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected