Skip to content

Commit

Permalink
make stack use checker not have stupid performance penalty
Browse files Browse the repository at this point in the history
  • Loading branch information
randomdude999 committed Jul 31, 2023
1 parent 3be9e5b commit 95cd3fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/asar/asar.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ virtual_file_error asar_get_last_io_error();
extern volatile int recursioncount;
extern int pass;

void reset_stack_use_check();
bool have_enough_stack_left();

class recurseblock {
Expand Down
4 changes: 4 additions & 0 deletions src/asar/assembleblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,10 @@ void initstuff()
initmathcore();

callstack.reset();
#if !defined(_WIN32) && defined(NO_USE_THREADS)
#else
reset_stack_use_check();
#endif
}

int get_freespace_pin_target(int target_id) {
Expand Down
13 changes: 7 additions & 6 deletions src/asar/platform/generic/thread-helpers-pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ bool run_as_thread(functor&& callback) {
}

#ifndef NO_USE_THREADS
bool have_enough_stack_left() {
void* stack_start = nullptr;
size_t stack_size = 0;
void reset_stack_use_check() {
pthread_attr_t attrs;
void *stack_start;
size_t stack_size;

pthread_getattr_np(pthread_self(), &attrs);
pthread_attr_getstack(&attrs, &stack_start, &stack_size);
pthread_attr_destroy(&attrs);

}
bool have_enough_stack_left() {
// using a random local as a rough estimate for current top-of-stack
size_t stack_left = (char*)&stack_size - (char*)stack_start;
char stackvar;
size_t stack_left = &stackvar - (char*)stack_start;
return stack_left >= 32768;
}
#endif
8 changes: 6 additions & 2 deletions src/asar/platform/windows/thread-helpers-win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ bool run_as_thread(functor&& callback) {
return wrapper.result;
}

bool have_enough_stack_left() {
char* stack_bottom;
void reset_stack_use_check() {
MEMORY_BASIC_INFORMATION mbi;
char stackvar;
VirtualQuery(&stackvar, &mbi, sizeof(mbi));
char* stack_bottom = (char*)mbi.AllocationBase;
stack_bottom = (char*)mbi.AllocationBase;
}
bool have_enough_stack_left() {
char stackvar;
return (&stackvar - stack_bottom) >= 32768;
}
#endif

0 comments on commit 95cd3fd

Please sign in to comment.