From 6307a67f8fbf05d12b891e37ba651cc28ba164c3 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 13 Aug 2019 13:40:25 +0300 Subject: [PATCH] C: allow to redefine directory where to write GC trace files --- .../resources/org/teavm/backend/c/runtime.c | 52 +++++++++++++++++-- .../resources/org/teavm/backend/c/runtime.h | 3 +- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/core/src/main/resources/org/teavm/backend/c/runtime.c b/core/src/main/resources/org/teavm/backend/c/runtime.c index 1a7ea2e98..db5b346d1 100644 --- a/core/src/main/resources/org/teavm/backend/c/runtime.c +++ b/core/src/main/resources/org/teavm/backend/c/runtime.c @@ -257,7 +257,7 @@ void teavm_interrupt() { #ifndef TEAVM_WINDOWS_LOG #define TEAVM_OUTPUT_STRING(s) fprintf(stderr, s) #else - #define TEAVM_OUTPUT_STRING(s) OutputDebugStringW(L##s) + #define TEAVM_OUTPUT_STRING(s) OutputDebugStringW(L##s) #endif void teavm_outOfMemory() { @@ -600,6 +600,8 @@ void teavm_logchar(int32_t c) { #ifdef TEAVM_MEMORY_TRACE +static wchar_t* teavm_gc_dumpDirectory = NULL; + void teavm_gc_assertSize(int32_t size) { if (size % sizeof(void*) != 0) { abort(); @@ -759,13 +761,43 @@ void teavm_gc_move(void* from, void* to, int32_t size) { #endif } -#ifdef TEAVM_MEMORY_TRACE - static FILE* teavm_gc_traceFile = NULL; +static FILE* teavm_gc_traceFile = NULL; +static FILE* teavm_gc_openDumpFile(wchar_t* name) { + wchar_t* fullName = name; + size_t fullNameLen = wcslen(name); + if (teavm_gc_dumpDirectory != NULL) { + size_t prefixLen = wcslen(teavm_gc_dumpDirectory); + size_t nameLen = fullNameLen; + fullNameLen = nameLen + prefixLen; + fullName = malloc((prefixLen + nameLen + 1) * sizeof(wchar_t)); + memcpy(fullName, teavm_gc_dumpDirectory, prefixLen * sizeof(wchar_t)); + memcpy(fullName + prefixLen, name, (nameLen + 1) * sizeof(wchar_t)); + } + + FILE* result; + #ifdef _MSC_VER + _wfopen_s(&result, fullName, L"w"); + #else + size_t fullNameMbSize = 3 * (fullNameLen + 1) * sizeof(wchar_t); + char* fullNameMb = malloc(fullNameMbSize); + mbstate_t state = { 0 }; + wcsrtombs(fullNameMb, fullName, fullNameMbSize, state); + result = fopen(fullNameMb, "w"); + #endif + + if (fullName != name) { + free(fullName); + } + + return result; +} + +#ifdef TEAVM_MEMORY_TRACE static void teavm_writeHeapMemory(char* name) { #ifdef TEAVM_GC_LOG if (teavm_gc_traceFile == NULL) { - teavm_gc_traceFile = fopen("teavm-gc-trace.txt", "w"); + teavm_gc_traceFile = teavm_gc_openDumpFile(L"teavm-gc-trace.txt"); } FILE* file = teavm_gc_traceFile; fprintf(file, "%s:", name); @@ -856,4 +888,16 @@ void teavm_gc_defragCompleted() { #ifdef TEAVM_MEMORY_TRACE teavm_writeHeapMemory("defrag"); #endif +} + +void teavm_gc_setDumpDirectory(const wchar_t* path) { + #ifdef TEAVM_MEMORY_TRACE + if (teavm_gc_dumpDirectory != NULL) { + free(teavm_gc_dumpDirectory); + } + size_t pathLen = wcslen(path); + size_t bytesLen = sizeof(wchar_t) * (pathLen + 1); + teavm_gc_dumpDirectory = malloc(bytesLen); + memcpy(teavm_gc_dumpDirectory, path, bytesLen); + #endif } \ No newline at end of file diff --git a/core/src/main/resources/org/teavm/backend/c/runtime.h b/core/src/main/resources/org/teavm/backend/c/runtime.h index 08432d414..b23343e0a 100644 --- a/core/src/main/resources/org/teavm/backend/c/runtime.h +++ b/core/src/main/resources/org/teavm/backend/c/runtime.h @@ -473,4 +473,5 @@ extern void teavm_gc_mark(void* address); extern void teavm_gc_move(void* from, void* to, int32_t size); extern void teavm_gc_gcStarted(); extern void teavm_gc_sweepCompleted(); -extern void teavm_gc_defragCompleted(); \ No newline at end of file +extern void teavm_gc_defragCompleted(); +extern void teavm_gc_setDumpDirectory(const wchar_t* path); \ No newline at end of file