MCPcopy Create free account
hub / github.com/ThePhD/sol2 / alloc

Method alloc

examples/source/allocation_limit.cpp:43–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41
42private:
43 void* alloc(void* ptr, size_t original_block_size_or_code,
44 size_t new_block_size) {
45 std::size_t original_block_size
46 = original_block_size_or_code;
47 if (ptr == nullptr) {
48 // object code!
49 sol::type object_type = static_cast<sol::type>(
50 original_block_size_or_code);
51 switch (object_type) {
52 case sol::type::function:
53 ++n_functions;
54 break;
55 case sol::type::string:
56 ++n_strings;
57 break;
58 case sol::type::userdata:
59 ++n_userdata;
60 break;
61 case sol::type::table:
62 ++n_tables;
63 break;
64 case sol::type::thread:
65 ++n_threads;
66 break;
67 default:
68 // not a clue, fam
69 break;
70 }
71 // because it is an object code,
72 // it tells us literally nothing about
73 // the old block size,
74 // so set that to 0
75 original_block_size = 0;
76 }
77 if (new_block_size == 0) {
78 // Lua expects us to act like a "free"
79 // when the new block size is 0
80 std::free(ptr);
81 used -= original_block_size;
82 return nullptr;
83 }
84
85 // did we hit the limit?
86 std::size_t memory_differntial
87 = new_block_size - original_block_size;
88 std::size_t desired_use = used + memory_differntial;
89 if (desired_use > limit) {
90 // tell the Lua Virtual Machine
91 // to toss off (by returning nullptr)
92 return nullptr;
93 }
94 // alright now we have to expand this shit
95 // guess we use C's realloc
96 ptr = std::realloc(ptr, new_block_size);
97 if (ptr != nullptr) {
98 // alright, we successfully allocated some space
99 // track it
100 used = desired_use;

Callers 1

allocateMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected