flatten 20260225

This commit is contained in:
Timothy Prepscius
2026-02-25 12:43:06 -05:00
commit 996151a56a
521 changed files with 46722 additions and 0 deletions

99
tjp/core/debug/StackUsage.cpp Executable file
View File

@@ -0,0 +1,99 @@
// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#include "StackUsage.h"
#include <pthread.h>
#include <iostream>
#include <cstdint>
#include "../system/System.h"
#ifdef SYS_APPLE
#include <mach/mach.h>
#include <mach/thread_info.h>
#include <mach/thread_act.h>
#include <mach/mach_types.h>
#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<std::uintptr_t>(&localVariable);
// Calculate the stack usage
std::size_t stackUsage = static_cast<std::size_t>(reinterpret_cast<std::uintptr_t>(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