diff --git a/source/exports.cpp b/source/exports.cpp index d75ad39..dea5da4 100644 --- a/source/exports.cpp +++ b/source/exports.cpp @@ -6,6 +6,8 @@ #define MAX_HANDLERS 16 static SDAttachHandlerFn sHandlers[MAX_HANDLERS] = {nullptr}; +static SDCleanUpHandlesHandlerFn sCleanupHandlesHandlers[MAX_HANDLERS] = {nullptr}; + void callAttachCallbacks(SDUtilsAttachStatus status) { int i; for (i = 0; i < MAX_HANDLERS; ++i) { @@ -15,10 +17,23 @@ void callAttachCallbacks(SDUtilsAttachStatus status) { } } +void callCleanUpHandlesCallbacks() { + int i; + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sCleanupHandlesHandlers[i]) { + sCleanupHandlesHandlers[i](); + } + } +} + void cleanUpAttachCallbacks() { memset(sHandlers, 0, sizeof(sHandlers)); } +void cleanUpCleanUpHandlesCallbacks() { + memset(sCleanupHandlesHandlers, 0, sizeof(sCleanupHandlesHandlers)); +} + bool SDUtilsAddAttachHandler(SDAttachHandlerFn fn) { int i; @@ -48,6 +63,35 @@ bool SDUtilsRemoveAttachHandler(SDAttachHandlerFn fn) { return false; } +bool SDUtilsAddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { + int i; + + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sCleanupHandlesHandlers[i] == fn) { + return true; + } + if (!sCleanupHandlesHandlers[i]) { + sCleanupHandlesHandlers[i] = fn; + return true; + } + } + + return false; +} + +bool SDUtilsRemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { + int i; + + for (i = 0; i < MAX_HANDLERS; ++i) { + if (sCleanupHandlesHandlers[i] == fn) { + sCleanupHandlesHandlers[i] = nullptr; + return true; + } + } + + return false; +} + SDUtilsVersion SDUtilsGetVersion() { return SDUTILS_MODULE_VERSION; } @@ -55,4 +99,7 @@ SDUtilsVersion SDUtilsGetVersion() { WUMS_EXPORT_FUNCTION(SDUtilsGetVersion); WUMS_EXPORT_FUNCTION(SDUtilsAddAttachHandler); -WUMS_EXPORT_FUNCTION(SDUtilsRemoveAttachHandler); \ No newline at end of file +WUMS_EXPORT_FUNCTION(SDUtilsRemoveAttachHandler); + +WUMS_EXPORT_FUNCTION(SDUtilsAddCleanUpHandlesHandler); +WUMS_EXPORT_FUNCTION(SDUtilsRemoveCleanUpHandlesHandler); \ No newline at end of file diff --git a/source/exports.h b/source/exports.h index 432da4b..fa21d8c 100644 --- a/source/exports.h +++ b/source/exports.h @@ -2,5 +2,7 @@ #include void callAttachCallbacks(SDUtilsAttachStatus status); +void cleanUpAttachCallbacks(); -void cleanUpAttachCallbacks(); \ No newline at end of file +void callCleanUpHandlesCallbacks(); +void cleanUpCleanUpHandlesCallbacks(); \ No newline at end of file diff --git a/source/sdcard.cpp b/source/sdcard.cpp index 3c9cfe0..7d2344a 100644 --- a/source/sdcard.cpp +++ b/source/sdcard.cpp @@ -40,6 +40,7 @@ void DeInitSDCheck() { } cleanUpAttachCallbacks(); + cleanUpCleanUpHandlesCallbacks(); OSMemoryBarrier(); } @@ -85,6 +86,7 @@ int UnmountSDCard() { return -1; } std::lock_guard lk(*mutex); + callCleanUpHandlesCallbacks(); FSCmdBlock fsCmd; FSInitCmdBlock(&fsCmd);