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, ... ) {