// License: Modified MIT (NON-AI) // See the LICENSE file in the root directory for license information. // Copyright 2025 Timothy Prepscius #include "StackUsage.h" #include #include #include #include "../system/System.h" #ifdef SYS_APPLE #include #include #include #include #endif namespace tjp { namespace core { #ifndef SYS_ANDROID #ifdef SYS_APPLE void *getStackBase () { return pthread_get_stackaddr_np(pthread_self()); /* uint64_t threadId; pthread_threadid_np(nullptr, &threadId); // Get thread info mach_msg_type_number_t threadInfoCount = THREAD_BASIC_INFO_COUNT; thread_basic_info_data_t threadInfo; if (thread_info((thread_act_t)threadId, THREAD_BASIC_INFO, (thread_info_t)&threadInfo, &threadInfoCount) != KERN_SUCCESS) { return 0; } // Get the stack base address void* stackBase = (void*)threadInfo.stack_base; return stackBase; */ } #else void *getStackBase() { pthread_attr_t attr; pthread_getattr_np(pthread_self(), &attr); // Get the stack base address void* stackBase = nullptr; pthread_attr_getstackaddr(&attr, &stackBase); // Clean up thread attributes pthread_attr_destroy(&attr); return stackBase; } #endif std::size_t getStackUsage() { // Define a local variable int localVariable; void* stackBase = getStackBase(); // Calculate the current stack pointer (address of localVariable) std::uintptr_t currentStackPointer = reinterpret_cast(&localVariable); // Calculate the stack usage std::size_t stackUsage = static_cast(reinterpret_cast(stackBase) - currentStackPointer); return stackUsage; } #else std::size_t getStackUsage() { pthread_attr_t attr; pthread_getattr_np(pthread_self(), &attr); size_t size = 0; pthread_attr_getstacksize(&attr, &size); return size; } #endif } // namespace } // namespace