// AUTOGENERATED COPYRIGHT HEADER START // Copyright (C) 2017-2023 Michael Fabian 'Xaymar' Dirks // Copyright (C) 2022 lainon // AUTOGENERATED COPYRIGHT HEADER END #include "utility.hpp" #include "common.hpp" #include "plugin.hpp" #include "warning-disable.hpp" #include #include #include #include "warning-enable.hpp" obs_property_t* streamfx::util::obs_properties_add_tristate(obs_properties_t* props, const char* name, const char* desc) { obs_property_t* p = obs_properties_add_list(props, name, desc, OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT); obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DEFAULT), -1); obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DISABLED), 0); obs_property_list_add_int(p, D_TRANSLATE(S_STATE_ENABLED), 1); return p; } std::pair streamfx::util::size_from_string(std::string_view text, bool allowSquare) { int64_t width, height; const auto* begin = text.data(); const auto* end = text.data() + text.size() + 1; char* here = const_cast(end); long long res = strtoll(begin, &here, 0); if (errno == ERANGE) { return {0, 0}; } width = res; while (here != end) { if (isdigit(*here) || (*here == '-') || (*here == '+')) { break; } here++; } if (here == end) { // Are we allowed to return a square? if (allowSquare) { // Yes: Return width,width. return {width, width}; } else { // No: Return width,0. return {width, 0}; } } res = strtoll(here, nullptr, 0); if (errno == ERANGE) { return {width, 0}; } height = res; return {width, height}; } void* streamfx::util::memory::malloc_aligned(std::size_t align, std::size_t size) { void* ptr = nullptr; #ifdef USE_MSC_ALLOC ptr = _aligned_malloc(size, align); #elif defined(USE_STD_ALLOC) ptr = aligned_alloc(size, align); #else // Ensure that we have space for the pointer and the data. std::size_t asize = aligned_offset(align, size + (sizeof(void*) * 2)); // Allocate memory and store integer representation of pointer. ptr = malloc(asize); // Calculate actual aligned position intptr_t ptr_off = static_cast(aligned_offset(align, reinterpret_cast(ptr) + sizeof(void*))); // Store actual pointer at ptr_off - sizeof(void*). *reinterpret_cast(ptr_off - sizeof(void*)) = reinterpret_cast(ptr); // Return aligned pointer ptr = reinterpret_cast(ptr_off); #endif if (!ptr) { throw std::bad_alloc(); } else { #ifndef D_PLATFORM_WINDOWS // For user-space allocations, Windows automatically zeroes out memory no questions asked. We can ask // for this not to happen, but usually we want to do it anyway. So on other platforms, this ensures // the memory region zeroed by default. memset(ptr, 0, size); #endif } return ptr; } void streamfx::util::memory::free_aligned(void* mem) { #ifdef USE_MSC_ALLOC _aligned_free(mem); #elif defined(USE_STD_ALLOC_FREE) free(mem); #else if (mem == nullptr) return; void* ptr = reinterpret_cast(*reinterpret_cast(static_cast(mem) - sizeof(void*))); free(ptr); #endif }