From 69d5e0b2804ea5cc373de88409bd30045be065bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 30 Jun 2023 16:59:14 +0300 Subject: [PATCH 1/2] Use u8string for TTF cache --- src/openrct2/drawing/TTF.cpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/openrct2/drawing/TTF.cpp b/src/openrct2/drawing/TTF.cpp index b551f6a3b8..e1749579a7 100644 --- a/src/openrct2/drawing/TTF.cpp +++ b/src/openrct2/drawing/TTF.cpp @@ -35,7 +35,7 @@ struct ttf_cache_entry { TTFSurface* surface; TTF_Font* font; - utf8* text; + u8string text; uint32_t lastUseTick; }; @@ -43,7 +43,7 @@ struct ttf_getwidth_cache_entry { uint32_t width; TTF_Font* font; - utf8* text; + u8string text; uint32_t lastUseTick; }; @@ -197,11 +197,9 @@ static void TTFSurfaceCacheDispose(ttf_cache_entry* entry) if (entry->surface != nullptr) { TTFFreeSurface(entry->surface); - free(entry->text); - + entry->text.clear(); entry->surface = nullptr; entry->font = nullptr; - entry->text = nullptr; } } @@ -270,21 +268,19 @@ TTFSurface* TTFSurfaceCacheGetOrAdd(TTF_Font* font, std::string_view text) _ttfSurfaceCacheCount++; entry->surface = surface; entry->font = font; - entry->text = strndup(text.data(), text.size()); + entry->text = text; entry->lastUseTick = gCurrentDrawCount; return entry->surface; } static void TTFGetWidthCacheDispose(ttf_getwidth_cache_entry* entry) { - if (entry->text != nullptr) - { - free(entry->text); + if (entry->text.empty()) + return; - entry->width = 0; - entry->font = nullptr; - entry->text = nullptr; - } + entry->text.clear(); + entry->width = 0; + entry->font = nullptr; } static void TTFGetWidthCacheDisposeAll() @@ -310,7 +306,7 @@ uint32_t TTFGetWidthCacheGetOrAdd(TTF_Font* font, std::string_view text) entry = &_ttfGetWidthCache[index]; // Check if entry is a hit - if (entry->text == nullptr) + if (entry->text.empty()) break; if (entry->font == font && String::Equals(entry->text, text)) { @@ -342,7 +338,7 @@ uint32_t TTFGetWidthCacheGetOrAdd(TTF_Font* font, std::string_view text) _ttfGetWidthCacheCount++; entry->width = width; entry->font = font; - entry->text = strndup(text.data(), text.size()); + entry->text = text; entry->lastUseTick = gCurrentDrawCount; return entry->width; } From 99aea9dba88795fb37440807c7ef4b38e0bb7db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 30 Jun 2023 17:13:35 +0300 Subject: [PATCH 2/2] Remove dead code --- src/openrct2/common.h | 4 ---- src/openrct2/platform/Platform.Win32.cpp | 15 --------------- 2 files changed, 19 deletions(-) diff --git a/src/openrct2/common.h b/src/openrct2/common.h index 7eb31ddfb7..3750f0cf0a 100644 --- a/src/openrct2/common.h +++ b/src/openrct2/common.h @@ -56,10 +56,6 @@ using colour_t = uint8_t; #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) -#ifdef _WIN32 -char* strndup(const char* src, size_t size); -#endif - // BSD and macOS have MAP_ANON instead of MAP_ANONYMOUS #ifndef MAP_ANONYMOUS # define MAP_ANONYMOUS MAP_ANON diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 402848f9e9..96c3c56972 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -47,21 +47,6 @@ static constexpr wchar_t SINGLE_INSTANCE_MUTEX_NAME[] = L"RollerCoaster Tycoon 2 # define SOFTWARE_CLASSES L"Software\\Classes" # define MUI_CACHE L"Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache" -char* strndup(const char* src, size_t size) -{ - size_t len = strnlen(src, size); - char* dst = reinterpret_cast(malloc(len + 1)); - - if (dst == nullptr) - { - return nullptr; - } - - dst = reinterpret_cast(std::memcpy(dst, src, len)); - dst[len] = '\0'; - return dst; -} - namespace Platform { static std::string WIN32_GetKnownFolderPath(REFKNOWNFOLDERID rfid);