Skip to content

Commit

Permalink
Rename and use function to exit when out of memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensBuechner committed Jan 31, 2024
1 parent d7db49d commit 412794f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/search/utils/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ void exit_with(ExitCode exitcode) {
exit(static_cast<int>(exitcode));
}

void exit_after_receiving_signal(ExitCode exitcode) {
/*
In signal handlers, we have to use the "safe function" _Exit() rather
than the unsafe function exit().
*/
void exit_with_reentrant(ExitCode exitcode) {
/* In signal handlers or when we run out of memory, we have to use the
"safe function" _Exit() rather than the unsafe function exit(). */
report_exit_code_reentrant(exitcode);
_Exit(static_cast<int>(exitcode));
}
Expand Down
2 changes: 1 addition & 1 deletion src/search/utils/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum class ExitCode {
};

NO_RETURN extern void exit_with(ExitCode returncode);
NO_RETURN extern void exit_after_receiving_signal(ExitCode returncode);
NO_RETURN extern void exit_with_reentrant(ExitCode returncode);

int get_peak_memory_in_kb();
const char *get_exit_code_message_reentrant(ExitCode exitcode);
Expand Down
4 changes: 2 additions & 2 deletions src/search/utils/system_unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static void out_of_memory_handler() {
memory for the stack of the signal handler and raising a signal here.
*/
write_reentrant_str(STDOUT_FILENO, "Failed to allocate memory.\n");
exit_with(ExitCode::SEARCH_OUT_OF_MEMORY);
exit_with_reentrant(ExitCode::SEARCH_OUT_OF_MEMORY);
}

static void signal_handler(int signal_number) {
Expand All @@ -173,7 +173,7 @@ static void signal_handler(int signal_number) {
write_reentrant_int(STDOUT_FILENO, signal_number);
write_reentrant_str(STDOUT_FILENO, " -- exiting\n");
if (signal_number == SIGXCPU) {
exit_after_receiving_signal(ExitCode::SEARCH_OUT_OF_TIME);
exit_with_reentrant(ExitCode::SEARCH_OUT_OF_TIME);
}
raise(signal_number);
}
Expand Down
2 changes: 1 addition & 1 deletion src/search/utils/system_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace std;
namespace utils {
void out_of_memory_handler() {
cout << "Failed to allocate memory." << endl;
exit_with(ExitCode::SEARCH_OUT_OF_MEMORY);
exit_with_reentrant(ExitCode::SEARCH_OUT_OF_MEMORY);
}

void signal_handler(int signal_number) {
Expand Down

0 comments on commit 412794f

Please sign in to comment.