Skip to content

Commit c9b5274

Browse files
committed
flatlaf-natives-windows: reworked memory allocation error handling
1 parent c3adadf commit c9b5274

5 files changed

Lines changed: 59 additions & 97 deletions

File tree

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/AllocRoutines.h

Lines changed: 0 additions & 53 deletions
This file was deleted.

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/FlatWndProc.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <jawt.h>
2424
#include <jawt_md.h>
2525
#include "FlatWndProc.h"
26-
#include "AllocRoutines.h"
2726
#include "com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h"
2827

2928
/**
@@ -101,31 +100,28 @@ HWND FlatWndProc::install( JNIEnv *env, jobject obj, jobject window ) {
101100

102101
// create HWND map
103102
if( hwndMap == NULL ) {
104-
HWNDMap* newHwndMap = new (FlatLafNoThrow) HWNDMap();
105-
if(newHwndMap == NULL) {
103+
hwndMap = new HWNDMap();
104+
if( hwndMap == NULL )
106105
return 0;
107-
} else if(!newHwndMap->isTableAllocated()) {
108-
FlatLafWin32ProcessHeapFree(newHwndMap);
109-
return 0;
110-
}
111-
112-
hwndMap = newHwndMap;
113106
}
114107

115108
// get window handle
116109
HWND hwnd = getWindowHandle( env, window );
117110
if( hwnd == NULL || hwndMap->get( hwnd ) != NULL )
118111
return 0;
119112

120-
FlatWndProc* fwp = new (FlatLafNoThrow) FlatWndProc();
121-
if(fwp == NULL)
113+
FlatWndProc* fwp = new FlatWndProc();
114+
if( fwp == NULL )
115+
return 0;
116+
117+
if( !hwndMap->put( hwnd, fwp ) ) {
118+
delete fwp;
122119
return 0;
123-
120+
}
121+
124122
env->GetJavaVM( &fwp->jvm );
125123
fwp->obj = env->NewGlobalRef( obj );
126124
fwp->hwnd = hwnd;
127-
if(!hwndMap->put( hwnd, fwp ))
128-
return 0;
129125

130126
// replace window procedure
131127
fwp->defaultWndProc = reinterpret_cast<WNDPROC>(
@@ -154,7 +150,7 @@ void FlatWndProc::uninstall( JNIEnv *env, jobject obj, HWND hwnd ) {
154150
env->DeleteGlobalRef( fwp->obj );
155151
if( fwp->background != NULL )
156152
::DeleteObject( fwp->background );
157-
FlatLafWin32ProcessHeapFree(fwp);
153+
delete fwp;
158154
}
159155

160156
void FlatWndProc::initIDs( JNIEnv *env, jobject obj ) {
@@ -312,7 +308,7 @@ LRESULT FlatWndProc::WmDestroy( HWND hwnd, int uMsg, WPARAM wParam, LPARAM lPara
312308
if( background != NULL )
313309
::DeleteObject( background );
314310
hwndMap->remove( hwnd );
315-
FlatLafWin32ProcessHeapFree(this);
311+
delete this;
316312

317313
// call original AWT window procedure because it may fire window closed event in AwtWindow::WmDestroy()
318314
return ::CallWindowProc( defaultWndProc2, hwnd, uMsg, wParam, lParam );

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
#define _NO_CRT_STDIO_INLINE
1919

2020
#include <stdio.h>
21-
#include <stdint.h>
22-
#include <limits.h>
2321
#include "HWNDMap.h"
24-
#include "AllocRoutines.h"
2522

2623
#define DEFAULT_CAPACITY 20
2724
#define INCREASE_CAPACITY 10
@@ -46,8 +43,8 @@ class LOCK {
4643

4744
HWNDMap::HWNDMap() {
4845
size = 0;
49-
capacity = DEFAULT_CAPACITY;
50-
table = new (FlatLafNoThrow) Entry[capacity];
46+
capacity = 0;
47+
table = NULL;
5148

5249
::InitializeCriticalSection( &criticalSection );
5350

@@ -69,13 +66,12 @@ bool HWNDMap::put( HWND key, LPVOID value ) {
6966
if( index >= 0 ) {
7067
// key already in map --> replace
7168
table[index].value = value;
72-
return true;
7369
} else {
7470
// insert new key
75-
if(size == INT_MAX || !ensureCapacity( size + 1 ))
71+
if( !ensureCapacity() )
7672
return false;
7773

78-
// make roor for new entry
74+
// make room for new entry
7975
index = -(index + 1);
8076
for( int i = size - 1; i >= index; i-- )
8177
table[i + 1] = table[i];
@@ -84,10 +80,10 @@ bool HWNDMap::put( HWND key, LPVOID value ) {
8480
// insert entry
8581
table[index].key = key;
8682
table[index].value = value;
87-
return true;
8883
}
8984

9085
// dump( "put" );
86+
return true;
9187
}
9288

9389
void HWNDMap::remove( HWND key ) {
@@ -108,6 +104,9 @@ void HWNDMap::remove( HWND key ) {
108104
}
109105

110106
int HWNDMap::binarySearch( HWND key ) {
107+
if( table == NULL )
108+
return -1;
109+
111110
__int64 ikey = reinterpret_cast<__int64>( key );
112111
int low = 0;
113112
int high = size - 1;
@@ -127,37 +126,36 @@ int HWNDMap::binarySearch( HWND key ) {
127126
return -(low + 1);
128127
}
129128

130-
static constexpr size_t MaxEntryArrayLength = (SIZE_MAX >> 1) / sizeof(Entry);
131-
static_assert(MaxEntryArrayLength > 0, "MaxEntryArrayLength > 0 must be true");
129+
bool HWNDMap::ensureCapacity() {
130+
if( table == NULL ) {
131+
table = new Entry[DEFAULT_CAPACITY];
132+
if( table == NULL )
133+
return false;
132134

133-
static constexpr int MaxEntryArrayIntCapacity =
134-
(MaxEntryArrayLength <= INT_MAX) ? static_cast<int>(MaxEntryArrayLength) : INT_MAX;
135-
static_assert(MaxEntryArrayIntCapacity > 0, "MaxEntryArrayIntCapacity > 0 must be true");
135+
capacity = DEFAULT_CAPACITY;
136+
return true;
137+
}
136138

137-
bool HWNDMap::ensureCapacity( int minCapacity ) {
138-
if(minCapacity <= capacity)
139+
// check capacity
140+
int minCapacity = size + 1;
141+
if( minCapacity <= capacity )
139142
return true;
140-
if(minCapacity > MaxEntryArrayIntCapacity)
141-
return false;
142143

143144
// allocate new table
144-
unsigned newCapacity = static_cast<unsigned>(minCapacity) + INCREASE_CAPACITY;
145-
if(newCapacity > MaxEntryArrayIntCapacity)
146-
newCapacity = MaxEntryArrayIntCapacity;
147-
148-
Entry* newTable = new (FlatLafNoThrow) Entry[newCapacity];
149-
if(newTable == NULL)
145+
int newCapacity = minCapacity + INCREASE_CAPACITY;
146+
Entry* newTable = new Entry[newCapacity];
147+
if( newTable == NULL )
150148
return false;
151149

152150
// copy old table to new table
153151
for( int i = 0; i < capacity; i++ )
154152
newTable[i] = table[i];
155153

156154
// delete old table
157-
FlatLafWin32ProcessHeapFree(table);
155+
delete[] table;
158156

159157
table = newTable;
160-
capacity = static_cast<int>(newCapacity);
158+
capacity = newCapacity;
161159
return true;
162160
}
163161

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/HWNDMap.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ class HWNDMap
4444
LPVOID get( HWND key );
4545
bool put( HWND key, LPVOID value );
4646
void remove( HWND key );
47-
bool isTableAllocated() noexcept { return static_cast<bool>(table); }
4847

4948
private:
5049
int binarySearch( HWND key );
51-
bool ensureCapacity( int newCapacity );
50+
bool ensureCapacity();
5251

5352
// void dump( char* msg );
5453
};

flatlaf-natives/flatlaf-natives-windows/src/main/cpp/Runtime.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ BOOL WINAPI _DllMainCRTStartup( HINSTANCE instance, DWORD reason, LPVOID reserve
4141
return TRUE;
4242
}
4343

44+
void* __cdecl operator new( size_t cb ) {
45+
// printf( "new %d\n", cb );
46+
return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb );
47+
}
48+
49+
void* __cdecl operator new[]( size_t cb ) {
50+
// printf( "new[] %d\n", cb );
51+
return ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, cb );
52+
}
53+
54+
void __cdecl operator delete( void* pv, size_t cb ) {
55+
// printf( "delete %p %d\n", pv, cb );
56+
if( pv != NULL )
57+
::HeapFree( ::GetProcessHeap(), 0, pv );
58+
}
59+
60+
void __cdecl operator delete[]( void* pv ) {
61+
// printf( "delete[] %p\n", pv );
62+
if( pv != NULL )
63+
::HeapFree( ::GetProcessHeap(), 0, pv );
64+
}
65+
4466
/*
4567
extern "C"
4668
int __cdecl printf( const char* format, ... ) {

0 commit comments

Comments
 (0)