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

Function user_allocate

include/sol/stack_core.hpp:407–431  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

405
406 template <typename T>
407 T* user_allocate(lua_State* L) {
408 typedef std::integral_constant<bool,
409#if SOL_IS_OFF(SOL_ALIGN_MEMORY)
410 false
411#else
412 (std::alignment_of_v<T> > 1)
413#endif
414 >
415 use_align;
416 if (!use_align::value) {
417 T* pointer = static_cast<T*>(alloc_newuserdata(L, sizeof(T)));
418 return pointer;
419 }
420
421 constexpr std::size_t initial_size = aligned_space_for<T>();
422
423 std::size_t allocated_size = initial_size;
424 void* unadjusted = alloc_newuserdata(L, allocated_size);
425 void* adjusted = align(std::alignment_of_v<T>, unadjusted, allocated_size);
426 if (adjusted == nullptr) {
427 lua_pop(L, 1);
428 luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T>().data());
429 }
430 return static_cast<T*>(adjusted);
431 }
432
433 template <typename T>
434 int usertype_alloc_destroy(lua_State* L) noexcept {

Callers

nothing calls this directly

Calls 3

alloc_newuserdataFunction · 0.85
alignFunction · 0.85
dataMethod · 0.45

Tested by

no test coverage detected