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

Function main

examples/source/allocation_limit.cpp:123–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

121#include <iostream>
122
123int main() {
124
125 std::cout << "=== memory tracker ===" << std::endl;
126
127#if SOL_LUAJIT_VERSION < 20100 && (UINTPTR_MAX > 0xFFFFFFFF)
128 std::cout << "LuaJIT in x64 mode on LuaJIT 2.0.X versions "
129 "does not support using a custom allocator!"
130 << std::endl;
131#else
132 memory_tracker box;
133 std::cout << "memory at start: " << box.currently_used()
134 << " bytes / " << box.memory_limit() << " bytes"
135 << std::endl;
136 sol::state lua(&sol::default_at_panic,
137 &memory_tracker::allocate,
138 &box);
139 lua.open_libraries(sol::lib::base);
140
141 std::cout << "memory after state creation: "
142 << box.currently_used() << " bytes / "
143 << box.memory_limit() << " bytes" << std::endl;
144
145 // trigger some allocations
146 lua.new_usertype<my_type>("my_type",
147 "a",
148 &my_type::a,
149 "b",
150 &my_type::b,
151 "d",
152 &my_type::d);
153 lua["f"] = [](std::string s) { return s + " woof!"; };
154
155 std::cout << "memory after function and usertype setup: "
156 << box.currently_used() << " bytes / "
157 << box.memory_limit() << " bytes" << std::endl;
158
159 lua.safe_script("print(f('bark'))");
160 lua.safe_script(
161 "local obj = my_type.new() print(obj.a, obj.b, obj.c) "
162 "obj.b = true print(obj.a, obj.b, obj.c)");
163
164 std::cout << "memory at end: " << box.currently_used()
165 << " bytes / " << box.memory_limit() << " bytes"
166 << std::endl;
167#endif
168 return 0;
169}

Callers

nothing calls this directly

Calls 4

currently_usedMethod · 0.80
memory_limitMethod · 0.80
open_librariesMethod · 0.80
safe_scriptMethod · 0.80

Tested by

no test coverage detected