about summary refs log tree commit diff stats
path: root/src/tools/cleanup.c
diff options
context:
space:
mode:
authorYang Liu <liuyang22@iscas.ac.cn>2025-04-11 18:26:51 +0800
committerGitHub <noreply@github.com>2025-04-11 12:26:51 +0200
commit6c46e3d9b15be3e5c6227bb97fd542a4100ec4d2 (patch)
treebf7552c358c240e44e53c176db44bad11a415de3 /src/tools/cleanup.c
parentfa85d4d900c3e03b69bdea65204b51151fc62114 (diff)
downloadbox64-6c46e3d9b15be3e5c6227bb97fd542a4100ec4d2.tar.gz
box64-6c46e3d9b15be3e5c6227bb97fd542a4100ec4d2.zip
[WOW64] Splitted freq and cleanup functions from x64emu (#2521)
Diffstat (limited to 'src/tools/cleanup.c')
-rw-r--r--src/tools/cleanup.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/tools/cleanup.c b/src/tools/cleanup.c
new file mode 100644
index 00000000..79ac3451
--- /dev/null
+++ b/src/tools/cleanup.c
@@ -0,0 +1,68 @@
+#include <string.h>
+
+#include "cleanup.h"
+#include "elfs/elfloader_private.h"
+#include "box64context.h"
+#include "debug.h"
+#include "callback.h"
+
+typedef struct cleanup_s {
+    void*       f;
+    int         arg;
+    void*       a;
+} cleanup_t;
+
+void AddCleanup(x64emu_t *emu, void *p)
+{
+    (void)emu;
+
+    if(my_context->clean_sz == my_context->clean_cap) {
+        my_context->clean_cap += 32;
+        my_context->cleanups = (cleanup_t*)box_realloc(my_context->cleanups, sizeof(cleanup_t)*my_context->clean_cap);
+    }
+    my_context->cleanups[my_context->clean_sz].arg = 0;
+    my_context->cleanups[my_context->clean_sz].a = NULL;
+    my_context->cleanups[my_context->clean_sz++].f = p;
+}
+
+void AddCleanup1Arg(x64emu_t *emu, void *p, void* a, elfheader_t* h)
+{
+    (void)emu;
+    if(!h)
+        return;
+
+    if(h->clean_sz == h->clean_cap) {
+        h->clean_cap += 32;
+        h->cleanups = (cleanup_t*)box_realloc(h->cleanups, sizeof(cleanup_t)*h->clean_cap);
+    }
+    h->cleanups[h->clean_sz].arg = 1;
+    h->cleanups[h->clean_sz].a = a;
+    h->cleanups[h->clean_sz++].f = p;
+}
+
+void CallCleanup(x64emu_t *emu, elfheader_t* h)
+{
+    printf_log(LOG_DEBUG, "Calling atexit registered functions for elf: %p/%s\n", h, h?h->name:"(nil)");
+    if(!h)
+        return;
+    for(int i=h->clean_sz-1; i>=0; --i) {
+        printf_log(LOG_DEBUG, "Call cleanup #%d (args:%d, arg:%p)\n", i, h->cleanups[i].arg, h->cleanups[i].a);
+        RunFunctionWithEmu(emu, 0, (uintptr_t)(h->cleanups[i].f), h->cleanups[i].arg, h->cleanups[i].a );
+        // now remove the cleanup
+        if(i!=h->clean_sz-1)
+            memmove(h->cleanups+i, h->cleanups+i+1, (h->clean_sz-i-1)*sizeof(cleanup_t));
+        --h->clean_sz;
+    }
+}
+
+void CallAllCleanup(x64emu_t *emu)
+{
+    printf_log(LOG_DEBUG, "Calling atexit registered functions\n");
+    for(int i=my_context->clean_sz-1; i>=0; --i) {
+        printf_log(LOG_DEBUG, "Call cleanup #%d\n", i);
+        --my_context->clean_sz;
+        RunFunctionWithEmu(emu, 0, (uintptr_t)(my_context->cleanups[i].f), my_context->cleanups[i].arg, my_context->cleanups[i].a );
+    }
+    box_free(my_context->cleanups);
+    my_context->cleanups = NULL;
+}
\ No newline at end of file