From 7a445aabd71c79779fdb1d42420f981cf4ad4bd6 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:25:26 -0500 Subject: [PATCH 01/15] Create AllocRoutines.h --- .../src/main/AllocRoutines.h | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h b/flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h new file mode 100644 index 00000000..f4a8f0fc --- /dev/null +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h @@ -0,0 +1,44 @@ +#ifndef FLATLAF_WIN32_ALLOCROUTINES_H +#define FLATLAF_WIN32_ALLOCROUTINES_H + +#include +#include + +struct NoThrowingWin32HeapAllocT { +}; + +constexpr NoThrowingWin32HeapAllocT NoThrowingWin32HeapAlloc{}; + +#if defined(_MSC_VER) +#define FLATLAF_WIN32_ALLOC_INLINE __forceinline +#elif (defined(__GNUC__) || defined(__clang__)) +#define FLATLAF_WIN32_ALLOC_INLINE inline __attribute__((__always_inline__)) +#else +#define FLATLAF_WIN32_ALLOC_INLINE inline +#endif + +FLATLAF_WIN32_ALLOC_INLINE void* AllocateUsingProcessHeap(size_t cb) noexcept { + return ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, cb); +} + +FLATLAF_WIN32_ALLOC_INLINE void DeleteFromProcessHeap(void* pv) noexcept { + if(pv) + ::HeapFree(::GetProcessHeap(), 0, pv); +} + +FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { + return AllocateUsingProcessHeap(cb); +} + +inline void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { + return AllocateUsingProcessHeap(cb); +} + +inline void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { + DeleteFromProcessHeap(pv); +} + +inline void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { + DeleteFromProcessHeap(pv); +} +#endif From b43278439a8c764aac7fc55cbd532bf86593185e Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:26:01 -0500 Subject: [PATCH 02/15] Delete AllocRoutines.h --- .../src/main/AllocRoutines.h | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h b/flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h deleted file mode 100644 index f4a8f0fc..00000000 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/AllocRoutines.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef FLATLAF_WIN32_ALLOCROUTINES_H -#define FLATLAF_WIN32_ALLOCROUTINES_H - -#include -#include - -struct NoThrowingWin32HeapAllocT { -}; - -constexpr NoThrowingWin32HeapAllocT NoThrowingWin32HeapAlloc{}; - -#if defined(_MSC_VER) -#define FLATLAF_WIN32_ALLOC_INLINE __forceinline -#elif (defined(__GNUC__) || defined(__clang__)) -#define FLATLAF_WIN32_ALLOC_INLINE inline __attribute__((__always_inline__)) -#else -#define FLATLAF_WIN32_ALLOC_INLINE inline -#endif - -FLATLAF_WIN32_ALLOC_INLINE void* AllocateUsingProcessHeap(size_t cb) noexcept { - return ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, cb); -} - -FLATLAF_WIN32_ALLOC_INLINE void DeleteFromProcessHeap(void* pv) noexcept { - if(pv) - ::HeapFree(::GetProcessHeap(), 0, pv); -} - -FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { - return AllocateUsingProcessHeap(cb); -} - -inline void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { - return AllocateUsingProcessHeap(cb); -} - -inline void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { - DeleteFromProcessHeap(pv); -} - -inline void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { - DeleteFromProcessHeap(pv); -} -#endif From 01125e030e94ceca2192f5d0ee2fcaac9317635a Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:26:33 -0500 Subject: [PATCH 03/15] Create AllocRoutines.h --- .../src/main/cpp/AllocRoutines.h | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h new file mode 100644 index 00000000..f4a8f0fc --- /dev/null +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h @@ -0,0 +1,44 @@ +#ifndef FLATLAF_WIN32_ALLOCROUTINES_H +#define FLATLAF_WIN32_ALLOCROUTINES_H + +#include +#include + +struct NoThrowingWin32HeapAllocT { +}; + +constexpr NoThrowingWin32HeapAllocT NoThrowingWin32HeapAlloc{}; + +#if defined(_MSC_VER) +#define FLATLAF_WIN32_ALLOC_INLINE __forceinline +#elif (defined(__GNUC__) || defined(__clang__)) +#define FLATLAF_WIN32_ALLOC_INLINE inline __attribute__((__always_inline__)) +#else +#define FLATLAF_WIN32_ALLOC_INLINE inline +#endif + +FLATLAF_WIN32_ALLOC_INLINE void* AllocateUsingProcessHeap(size_t cb) noexcept { + return ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, cb); +} + +FLATLAF_WIN32_ALLOC_INLINE void DeleteFromProcessHeap(void* pv) noexcept { + if(pv) + ::HeapFree(::GetProcessHeap(), 0, pv); +} + +FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { + return AllocateUsingProcessHeap(cb); +} + +inline void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { + return AllocateUsingProcessHeap(cb); +} + +inline void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { + DeleteFromProcessHeap(pv); +} + +inline void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { + DeleteFromProcessHeap(pv); +} +#endif From 32e071ab89962a86ee80f1f7732fcd4a55f9725d Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:36:10 -0500 Subject: [PATCH 04/15] Update AllocRoutines.h --- .../flatlaf-natives-windows/src/main/cpp/AllocRoutines.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h index f4a8f0fc..82185260 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h @@ -30,15 +30,15 @@ FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const NoThrowingWin32He return AllocateUsingProcessHeap(cb); } -inline void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { +FLATLAF_WIN32_ALLOC_INLINE void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { return AllocateUsingProcessHeap(cb); } -inline void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { +FLATLAF_WIN32_ALLOC_INLINE void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { DeleteFromProcessHeap(pv); } -inline void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { +FLATLAF_WIN32_ALLOC_INLINE void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { DeleteFromProcessHeap(pv); } #endif From c1402d85e10a4a8c4dc2dc394076e626119d45d6 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:39:47 -0500 Subject: [PATCH 05/15] Update HWNDMap.h --- flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h | 1 + 1 file changed, 1 insertion(+) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h index c94e66ec..41b580fd 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h @@ -44,6 +44,7 @@ public: LPVOID get( HWND key ); void put( HWND key, LPVOID value ); void remove( HWND key ); + bool isTableAllocated() noexcept { return static_cast(table); } private: int binarySearch( HWND key ); From 28437f99cff7812a91793caec84cf2c894f758fd Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:53:33 -0500 Subject: [PATCH 06/15] Update new and delete FlatWndProc.cpp --- .../src/main/cpp/FlatWndProc.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp index b2f72ce1..12758ebd 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp @@ -99,15 +99,24 @@ HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) { return 0; // create HWND map - if( hwndMap == NULL ) - hwndMap = new HWNDMap(); + if( hwndMap == NULL ) { + HWNDMap* newHwndMap = new (FlatLafNoThrow) HWNDMap(); + if(newHwndMap == NULL) { + return 0; + } else if(!newHwndMap->isTableAllocated()) { + FlatLafWin32ProcessHeapFree(newHwndMap); + return 0; + } + + hwndMap = newHwndMap; + } // get window handle HWND hwnd = getWindowHandle( env, window ); if( hwnd == NULL || hwndMap->get( hwnd ) != NULL ) return 0; - FlatWndProc* fwp = new FlatWndProc(); + FlatWndProc* fwp = new (FlatLafNoThrow) FlatWndProc(); env->GetJavaVM( &fwp->jvm ); fwp->obj = env->NewGlobalRef( obj ); fwp->hwnd = hwnd; @@ -140,7 +149,7 @@ void FlatWndProc::uninstall( JNIEnv *env, jobject obj, HWND hwnd ) { env->DeleteGlobalRef( fwp->obj ); if( fwp->background != NULL ) ::DeleteObject( fwp->background ); - delete fwp; + FlatLafWin32ProcessHeapDelete(fwp); } void FlatWndProc::initIDs( JNIEnv *env, jobject obj ) { @@ -298,7 +307,7 @@ LRESULT FlatWndProc::WmDestroy( HWND hwnd, int uMsg, WPARAM wParam, LPARAM lPara if( background != NULL ) ::DeleteObject( background ); hwndMap->remove( hwnd ); - delete this; + FlatLafWin32ProcessHeapFree(this); // call original AWT window procedure because it may fire window closed event in AwtWindow::WmDestroy() return ::CallWindowProc( defaultWndProc2, hwnd, uMsg, wParam, lParam ); From 829c537fd31e1afce16d937bda8f02f6a27a7f64 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:55:33 -0500 Subject: [PATCH 07/15] Add checks for allocation failure --- .../flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp index 12758ebd..c54b6268 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp @@ -117,10 +117,14 @@ HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) { return 0; FlatWndProc* fwp = new (FlatLafNoThrow) FlatWndProc(); + if(fwp == NULL) + return 0; + env->GetJavaVM( &fwp->jvm ); fwp->obj = env->NewGlobalRef( obj ); fwp->hwnd = hwnd; - hwndMap->put( hwnd, fwp ); + if(!hwndMap->put( hwnd, fwp )) + return 0; // replace window procedure fwp->defaultWndProc = reinterpret_cast( From a4d2d347e3254cfbce72cd332eebe94caf10baf2 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:56:28 -0500 Subject: [PATCH 08/15] Change put method to return a bool --- flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h index 41b580fd..8ace9a04 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h @@ -42,7 +42,7 @@ public: HWNDMap(); LPVOID get( HWND key ); - void put( HWND key, LPVOID value ); + bool put( HWND key, LPVOID value ); void remove( HWND key ); bool isTableAllocated() noexcept { return static_cast(table); } From a7099c039fe8265b60f7794f6e348e3da34bd8ec Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 13:57:25 -0500 Subject: [PATCH 09/15] Rename allocation functions --- .../src/main/cpp/AllocRoutines.h | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h index 82185260..a96a35ca 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h @@ -4,10 +4,10 @@ #include #include -struct NoThrowingWin32HeapAllocT { +struct FlatLafNoThrowT { }; -constexpr NoThrowingWin32HeapAllocT NoThrowingWin32HeapAlloc{}; +constexpr FlatLafNoThrowT FlatLafNoThrow{}; #if defined(_MSC_VER) #define FLATLAF_WIN32_ALLOC_INLINE __forceinline @@ -17,28 +17,37 @@ constexpr NoThrowingWin32HeapAllocT NoThrowingWin32HeapAlloc{}; #define FLATLAF_WIN32_ALLOC_INLINE inline #endif -FLATLAF_WIN32_ALLOC_INLINE void* AllocateUsingProcessHeap(size_t cb) noexcept { +FLATLAF_WIN32_ALLOC_INLINE void* FlatLafWin32ProcessHeapAlloc(size_t cb) noexcept { + #ifdef _WIN32 return ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, cb); + #else + return ::calloc(cb, 1); + #endif } -FLATLAF_WIN32_ALLOC_INLINE void DeleteFromProcessHeap(void* pv) noexcept { +FLATLAF_WIN32_ALLOC_INLINE void FlatLafWin32ProcessHeapFree(void* pv) noexcept { + #ifdef _WIN32 if(pv) ::HeapFree(::GetProcessHeap(), 0, pv); + #else + if(pv) + ::free(pv); + #endif } -FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { - return AllocateUsingProcessHeap(cb); +FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const FlatLafNoThrowT& tag) noexcept { + return FlatLafWin32ProcessHeapAlloc(cb); } -FLATLAF_WIN32_ALLOC_INLINE void* operator new[](size_t cb, const NoThrowingWin32HeapAllocT& tag) noexcept { - return AllocateUsingProcessHeap(cb); +FLATLAF_WIN32_ALLOC_INLINE void* operator new[](size_t cb, const FlatLafNoThrowT& tag) noexcept { + return FlatLafWin32ProcessHeapAlloc(cb); } -FLATLAF_WIN32_ALLOC_INLINE void operator delete(void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { - DeleteFromProcessHeap(pv); +FLATLAF_WIN32_ALLOC_INLINE void operator delete(void* pv, const FlatLafNoThrowT& tag) noexcept { + FlatLafWin32ProcessHeapFree(pv); } -FLATLAF_WIN32_ALLOC_INLINE void operator delete[](void* pv, const NoThrowingWin32HeapAllocT& tag) noexcept { - DeleteFromProcessHeap(pv); +FLATLAF_WIN32_ALLOC_INLINE void operator delete[](void* pv, const FlatLafNoThrowT& tag) noexcept { + FlatLafWin32ProcessHeapFree(pv); } #endif From b8c7801365399fa53c0fdcc3f3917f0f469b9051 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 14:06:26 -0500 Subject: [PATCH 10/15] Change ensureCapacity method to return a bool --- flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h index 8ace9a04..b9b5c6a8 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h @@ -48,7 +48,7 @@ public: private: int binarySearch( HWND key ); - void ensureCapacity( int newCapacity ); + bool ensureCapacity( int newCapacity ); // void dump( char* msg ); }; From 0a4dc54fb940faf2a6986c97ac1dc75adc1c7790 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 14:17:37 -0500 Subject: [PATCH 11/15] Update put and ensureCapacity routines --- .../src/main/cpp/HWNDMap.cpp | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp index 8f9ae24a..cf9e2822 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp @@ -18,7 +18,10 @@ #define _NO_CRT_STDIO_INLINE #include +#include +#include #include "HWNDMap.h" +#include "AllocRoutines.h" #define DEFAULT_CAPACITY 20 #define INCREASE_CAPACITY 10 @@ -58,7 +61,7 @@ LPVOID HWNDMap::get( HWND key ) { return (index >= 0) ? table[index].value : NULL; } -void HWNDMap::put( HWND key, LPVOID value ) { +bool HWNDMap::put( HWND key, LPVOID value ) { LOCK lock( &criticalSection ); int index = binarySearch( key ); @@ -66,9 +69,11 @@ void HWNDMap::put( HWND key, LPVOID value ) { if( index >= 0 ) { // key already in map --> replace table[index].value = value; + return true; } else { // insert new key - ensureCapacity( size + 1 ); + if(size == INT_MAX || !ensureCapacity( size + 1 )) + return false; // make roor for new entry index = -(index + 1); @@ -79,6 +84,7 @@ void HWNDMap::put( HWND key, LPVOID value ) { // insert entry table[index].key = key; table[index].value = value; + return true; } // dump( "put" ); @@ -121,23 +127,38 @@ int HWNDMap::binarySearch( HWND key ) { return -(low + 1); } -void HWNDMap::ensureCapacity( int minCapacity ) { - if( minCapacity <= capacity ) - return; +static constexpr size_t MaxEntryArrayLength = (SIZE_MAX >> 1) / sizeof(Entry); +static_assert(MaxEntryArrayLength > 0, "MaxEntryArrayLength > 0 must be true"); + +static constexpr int MaxEntryArrayIntCapacity = + (MaxEntryArrayLength <= INT_MAX) ? static_cast(MaxEntryArrayLength) : INT_MAX; +static_assert(MaxEntryArrayIntCapacity > 0, "MaxEntryArrayIntCapacity > 0 must be true"); + +bool HWNDMap::ensureCapacity( int minCapacity ) { + if(minCapacity <= capacity) + return true; + if(minCapacity > MaxEntryArrayIntCapacity) + return false; // allocate new table - int newCapacity = minCapacity + INCREASE_CAPACITY; - Entry* newTable = new Entry[newCapacity]; + unsigned newCapacity = static_cast(minCapacity) + INCREASE_CAPACITY; + if(newCapacity > MaxEntryArrayIntCapacity) + newCapacity = MaxEntryArrayIntCapacity; + + Entry* newTable = new (FlatLafNoThrow) Entry[newCapacity]; + if(newTable == NULL) + return false; // copy old table to new table for( int i = 0; i < capacity; i++ ) newTable[i] = table[i]; // delete old table - delete table; + FlatLafWin32ProcessHeapFree(table); table = newTable; - capacity = newCapacity; + capacity = static_cast(newCapacity); + return true; } /* From a1d1e221ae53c9a12e45c9eeae9f6f708517e7bf Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 14:23:16 -0500 Subject: [PATCH 12/15] Remove operator new and operator delete overloads from Runtime.cpp The ```operator new``` and ```operator delete``` overloads in Runtime.cpp are replaced by placement ```operator new``` and ```operator delete``` operators in AllocRoutines.h that take a const FlatLafNoThrowT& placement parameter. Using ```new (FlatLafNoThrow) FlatWndProc``` instead of ```new FlatWndProc``` also allows for inlining by the C++ compiler. --- .../src/main/cpp/Runtime.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp index efe5f3ac..582a67d5 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp @@ -41,19 +41,6 @@ BOOL WINAPI _DllMainCRTStartup( HINSTANCE instance, DWORD reason, LPVOID reserve return TRUE; } -void* __cdecl operator new( size_t cb ) { - return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb ); -} - -void* __cdecl operator new[]( size_t cb ) { - return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb ); -} - -void __cdecl operator delete( void* pv, size_t cb ) { - if( pv != NULL ) - ::HeapFree( ::GetProcessHeap(), 0, pv ); -} - /* extern "C" int __cdecl printf( const char* format, ... ) { From f7be12df6732f4b73b57c5fbf9c38d6a4a6b2d88 Mon Sep 17 00:00:00 2001 From: John Platts Date: Mon, 19 Sep 2022 14:23:51 -0500 Subject: [PATCH 13/15] Add AllocRoutines.h include --- .../flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp index c54b6268..41b55c40 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp @@ -23,6 +23,7 @@ #include #include #include "FlatWndProc.h" +#include "AllocRoutines.h" #include "com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h" /** From c3adadfe2f43813b0d0fdf352d766222c27211ce Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 23 Nov 2022 21:27:55 +0100 Subject: [PATCH 14/15] flatlaf-natives-windows: fixed compile and link errors --- .../flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp | 2 +- .../flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp index 41b55c40..df825872 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp @@ -154,7 +154,7 @@ void FlatWndProc::uninstall( JNIEnv *env, jobject obj, HWND hwnd ) { env->DeleteGlobalRef( fwp->obj ); if( fwp->background != NULL ) ::DeleteObject( fwp->background ); - FlatLafWin32ProcessHeapDelete(fwp); + FlatLafWin32ProcessHeapFree(fwp); } void FlatWndProc::initIDs( JNIEnv *env, jobject obj ) { diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp index cf9e2822..44796c8e 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp @@ -47,7 +47,7 @@ public: HWNDMap::HWNDMap() { size = 0; capacity = DEFAULT_CAPACITY; - table = new Entry[capacity]; + table = new (FlatLafNoThrow) Entry[capacity]; ::InitializeCriticalSection( &criticalSection ); From c9b5274ccfe987f75393818e938b391689f7c700 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 26 Nov 2022 19:05:24 +0100 Subject: [PATCH 15/15] flatlaf-natives-windows: reworked memory allocation error handling --- .../src/main/cpp/AllocRoutines.h | 53 ------------------- .../src/main/cpp/FlatWndProc.cpp | 28 +++++----- .../src/main/cpp/HWNDMap.cpp | 52 +++++++++--------- .../src/main/cpp/HWNDMap.h | 3 +- .../src/main/cpp/Runtime.cpp | 22 ++++++++ 5 files changed, 60 insertions(+), 98 deletions(-) delete mode 100644 flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h deleted file mode 100644 index a96a35ca..00000000 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef FLATLAF_WIN32_ALLOCROUTINES_H -#define FLATLAF_WIN32_ALLOCROUTINES_H - -#include -#include - -struct FlatLafNoThrowT { -}; - -constexpr FlatLafNoThrowT FlatLafNoThrow{}; - -#if defined(_MSC_VER) -#define FLATLAF_WIN32_ALLOC_INLINE __forceinline -#elif (defined(__GNUC__) || defined(__clang__)) -#define FLATLAF_WIN32_ALLOC_INLINE inline __attribute__((__always_inline__)) -#else -#define FLATLAF_WIN32_ALLOC_INLINE inline -#endif - -FLATLAF_WIN32_ALLOC_INLINE void* FlatLafWin32ProcessHeapAlloc(size_t cb) noexcept { - #ifdef _WIN32 - return ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, cb); - #else - return ::calloc(cb, 1); - #endif -} - -FLATLAF_WIN32_ALLOC_INLINE void FlatLafWin32ProcessHeapFree(void* pv) noexcept { - #ifdef _WIN32 - if(pv) - ::HeapFree(::GetProcessHeap(), 0, pv); - #else - if(pv) - ::free(pv); - #endif -} - -FLATLAF_WIN32_ALLOC_INLINE void* operator new(size_t cb, const FlatLafNoThrowT& tag) noexcept { - return FlatLafWin32ProcessHeapAlloc(cb); -} - -FLATLAF_WIN32_ALLOC_INLINE void* operator new[](size_t cb, const FlatLafNoThrowT& tag) noexcept { - return FlatLafWin32ProcessHeapAlloc(cb); -} - -FLATLAF_WIN32_ALLOC_INLINE void operator delete(void* pv, const FlatLafNoThrowT& tag) noexcept { - FlatLafWin32ProcessHeapFree(pv); -} - -FLATLAF_WIN32_ALLOC_INLINE void operator delete[](void* pv, const FlatLafNoThrowT& tag) noexcept { - FlatLafWin32ProcessHeapFree(pv); -} -#endif diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp index df825872..77b8006d 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp @@ -23,7 +23,6 @@ #include #include #include "FlatWndProc.h" -#include "AllocRoutines.h" #include "com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h" /** @@ -101,15 +100,9 @@ HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) { // create HWND map if( hwndMap == NULL ) { - HWNDMap* newHwndMap = new (FlatLafNoThrow) HWNDMap(); - if(newHwndMap == NULL) { + hwndMap = new HWNDMap(); + if( hwndMap == NULL ) return 0; - } else if(!newHwndMap->isTableAllocated()) { - FlatLafWin32ProcessHeapFree(newHwndMap); - return 0; - } - - hwndMap = newHwndMap; } // get window handle @@ -117,15 +110,18 @@ HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) { if( hwnd == NULL || hwndMap->get( hwnd ) != NULL ) return 0; - FlatWndProc* fwp = new (FlatLafNoThrow) FlatWndProc(); - if(fwp == NULL) + FlatWndProc* fwp = new FlatWndProc(); + if( fwp == NULL ) return 0; - + + if( !hwndMap->put( hwnd, fwp ) ) { + delete fwp; + return 0; + } + env->GetJavaVM( &fwp->jvm ); fwp->obj = env->NewGlobalRef( obj ); fwp->hwnd = hwnd; - if(!hwndMap->put( hwnd, fwp )) - return 0; // replace window procedure fwp->defaultWndProc = reinterpret_cast( @@ -154,7 +150,7 @@ void FlatWndProc::uninstall( JNIEnv *env, jobject obj, HWND hwnd ) { env->DeleteGlobalRef( fwp->obj ); if( fwp->background != NULL ) ::DeleteObject( fwp->background ); - FlatLafWin32ProcessHeapFree(fwp); + delete fwp; } void FlatWndProc::initIDs( JNIEnv *env, jobject obj ) { @@ -312,7 +308,7 @@ LRESULT FlatWndProc::WmDestroy( HWND hwnd, int uMsg, WPARAM wParam, LPARAM lPara if( background != NULL ) ::DeleteObject( background ); hwndMap->remove( hwnd ); - FlatLafWin32ProcessHeapFree(this); + delete this; // call original AWT window procedure because it may fire window closed event in AwtWindow::WmDestroy() return ::CallWindowProc( defaultWndProc2, hwnd, uMsg, wParam, lParam ); diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp index 44796c8e..08e6df8a 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp @@ -18,10 +18,7 @@ #define _NO_CRT_STDIO_INLINE #include -#include -#include #include "HWNDMap.h" -#include "AllocRoutines.h" #define DEFAULT_CAPACITY 20 #define INCREASE_CAPACITY 10 @@ -46,8 +43,8 @@ public: HWNDMap::HWNDMap() { size = 0; - capacity = DEFAULT_CAPACITY; - table = new (FlatLafNoThrow) Entry[capacity]; + capacity = 0; + table = NULL; ::InitializeCriticalSection( &criticalSection ); @@ -69,13 +66,12 @@ bool HWNDMap::put( HWND key, LPVOID value ) { if( index >= 0 ) { // key already in map --> replace table[index].value = value; - return true; } else { // insert new key - if(size == INT_MAX || !ensureCapacity( size + 1 )) + if( !ensureCapacity() ) return false; - // make roor for new entry + // make room for new entry index = -(index + 1); for( int i = size - 1; i >= index; i-- ) table[i + 1] = table[i]; @@ -84,10 +80,10 @@ bool HWNDMap::put( HWND key, LPVOID value ) { // insert entry table[index].key = key; table[index].value = value; - return true; } // dump( "put" ); + return true; } void HWNDMap::remove( HWND key ) { @@ -108,6 +104,9 @@ void HWNDMap::remove( HWND key ) { } int HWNDMap::binarySearch( HWND key ) { + if( table == NULL ) + return -1; + __int64 ikey = reinterpret_cast<__int64>( key ); int low = 0; int high = size - 1; @@ -127,26 +126,25 @@ int HWNDMap::binarySearch( HWND key ) { return -(low + 1); } -static constexpr size_t MaxEntryArrayLength = (SIZE_MAX >> 1) / sizeof(Entry); -static_assert(MaxEntryArrayLength > 0, "MaxEntryArrayLength > 0 must be true"); +bool HWNDMap::ensureCapacity() { + if( table == NULL ) { + table = new Entry[DEFAULT_CAPACITY]; + if( table == NULL ) + return false; -static constexpr int MaxEntryArrayIntCapacity = - (MaxEntryArrayLength <= INT_MAX) ? static_cast(MaxEntryArrayLength) : INT_MAX; -static_assert(MaxEntryArrayIntCapacity > 0, "MaxEntryArrayIntCapacity > 0 must be true"); - -bool HWNDMap::ensureCapacity( int minCapacity ) { - if(minCapacity <= capacity) + capacity = DEFAULT_CAPACITY; + return true; + } + + // check capacity + int minCapacity = size + 1; + if( minCapacity <= capacity ) return true; - if(minCapacity > MaxEntryArrayIntCapacity) - return false; // allocate new table - unsigned newCapacity = static_cast(minCapacity) + INCREASE_CAPACITY; - if(newCapacity > MaxEntryArrayIntCapacity) - newCapacity = MaxEntryArrayIntCapacity; - - Entry* newTable = new (FlatLafNoThrow) Entry[newCapacity]; - if(newTable == NULL) + int newCapacity = minCapacity + INCREASE_CAPACITY; + Entry* newTable = new Entry[newCapacity]; + if( newTable == NULL ) return false; // copy old table to new table @@ -154,10 +152,10 @@ bool HWNDMap::ensureCapacity( int minCapacity ) { newTable[i] = table[i]; // delete old table - FlatLafWin32ProcessHeapFree(table); + delete[] table; table = newTable; - capacity = static_cast(newCapacity); + capacity = newCapacity; return true; } diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h index b9b5c6a8..f22f78ce 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h @@ -44,11 +44,10 @@ public: LPVOID get( HWND key ); bool put( HWND key, LPVOID value ); void remove( HWND key ); - bool isTableAllocated() noexcept { return static_cast(table); } private: int binarySearch( HWND key ); - bool ensureCapacity( int newCapacity ); + bool ensureCapacity(); // void dump( char* msg ); }; diff --git a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp index 582a67d5..cbd5163d 100644 --- a/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp +++ b/flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp @@ -41,6 +41,28 @@ BOOL WINAPI _DllMainCRTStartup( HINSTANCE instance, DWORD reason, LPVOID reserve return TRUE; } +void* __cdecl operator new( size_t cb ) { +// printf( "new %d\n", cb ); + return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb ); +} + +void* __cdecl operator new[]( size_t cb ) { +// printf( "new[] %d\n", cb ); + return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb ); +} + +void __cdecl operator delete( void* pv, size_t cb ) { +// printf( "delete %p %d\n", pv, cb ); + if( pv != NULL ) + ::HeapFree( ::GetProcessHeap(), 0, pv ); +} + +void __cdecl operator delete[]( void* pv ) { +// printf( "delete[] %p\n", pv ); + if( pv != NULL ) + ::HeapFree( ::GetProcessHeap(), 0, pv ); +} + /* extern "C" int __cdecl printf( const char* format, ... ) {