Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only perform cleanup when exiting cleanly #288

Merged
merged 2 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ndless-sdk/include/nucleus.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ unsigned int nl_hwsubtype();
BOOL nl_loaded_by_3rd_party_loader();
BOOL nl_isstartup();
BOOL _nl_hassyscall(int nr);
// Mark the program as resident, i.e. don't unload the executable on exit.
// Instead of calling exit or return from main, _exit has to be used.
// Otherwise cleanup breaks some library functions!
void nl_set_resident();
unsigned int nl_osvalue(const unsigned int *values, unsigned size);
int nl_exec(const char* prg, int argc, char** argv);
Expand Down
29 changes: 20 additions & 9 deletions ndless-sdk/libsyscalls/stdlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ constexpr int MAX_OPEN_FILES = 20;
static NUC_FILE* openfiles[MAX_OPEN_FILES];
static void* saved_screen_buffer; //In case the program changes the buffer

static bool is_clean_exit = false; // Whether returned from main or exit called

#ifdef USE_NSPIREIO
#include <nspireio/nspireio.h>

static nio_console csl;
static bool nio_ready = false;
#endif

// Called at startup (even before c++ constructors are run)
Expand All @@ -65,7 +68,10 @@ void initialise_monitor_handles()
nio_init(&csl,NIO_MAX_COLS,NIO_MAX_ROWS,0,0,NIO_COLOR_BLACK,NIO_COLOR_WHITE,TRUE);
nio_set_default(&csl);
nio_fflush(&csl);
nio_ready = true;
#endif

atexit([] { is_clean_exit = true; });
}

static int newslot()
Expand Down Expand Up @@ -183,13 +189,14 @@ void *calloc(size_t nmemb, size_t size)
int _puts(const char *s)
{
#ifdef USE_NSPIREIO
return nio_puts(s);
#else
return syscall<e_puts, int>(s);
if(nio_ready)
return nio_puts(s);
#endif

return syscall<e_puts, int>(s);
}

void __crt0_exit(int ret); // Declared in crt0.S
void __attribute__((noreturn)) __crt0_exit(int ret); // Declared in crt0.S

void _exit(int ret)
{
Expand All @@ -199,9 +206,15 @@ void _exit(int ret)
// it the right place for deinititalizing nspireio, as after this
// nothing has access to csl anymore and newlib itself won't flush
// its buffers either.
nio_free(&csl);
if(nio_ready)
nio_free(&csl);
nio_ready = false;
#endif

// When calling _exit, don't perform any cleanup
if(!is_clean_exit)
__crt0_exit(ret);

// Free memory allocated by libstdc++
if(__gnu_cxx::__freeres)
__gnu_cxx::__freeres();
Expand All @@ -214,8 +227,6 @@ void _exit(int ret)
_reclaim_reent(global_reent);

__crt0_exit(ret);

__builtin_unreachable();
}

std::type_info* __cxa_current_exception_type() __attribute__((weak));
Expand Down Expand Up @@ -314,7 +325,7 @@ int _fstat(int file, struct stat *st)
int _read(int file, char *ptr, int len)
{
#ifdef USE_NSPIREIO
if(file == 0)
if(file == 0 && nio_ready)
{
if(!nio_fgets(ptr, len - 1, &csl))
*ptr = 0;
Expand Down Expand Up @@ -344,7 +355,7 @@ int _read(int file, char *ptr, int len)
int _write(int file, char *ptr, int len)
{
#ifdef USE_NSPIREIO
if(file == 1 || file == 2)
if((file == 1 || file == 2) && nio_ready)
{
int len2 = len;
while(len2--)
Expand Down
4 changes: 3 additions & 1 deletion ndless-sdk/samples/luaext/luaextdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ int main(void) {
lua_State *L = nl_lua_getstate();
if (!L) return 0; // not being called as Lua module
luaL_register(L, "luaextdemo", lualib);
return 0;

// Skip cleanup on exit, this is a resident program
_exit(0);
}