[360] | 1 | /*!
|
---|
| 2 | @brief このファイルでは、ABのガベージコレクションの実装を行います。
|
---|
[109] | 3 | */
|
---|
[1] | 4 |
|
---|
[109] | 5 |
|
---|
[1] | 6 | /*
|
---|
| 7 | ※これらの変数はコンパイラが自動的に定義します。
|
---|
| 8 | Dim _System_gc_StackRoot_StartPtr As VoidPtr
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | Function _System_GetSp() As LONG_PTR 'dummy
|
---|
| 12 | End Function
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | Const _System_GC_FLAG_ATOMIC = 1
|
---|
| 16 | Const _System_GC_FLAG_NEEDFREE = 2
|
---|
| 17 | Const _System_GC_FLAG_INITZERO = 4
|
---|
[144] | 18 | Const _System_GC_FLAG_OBJECT = 8
|
---|
[1] | 19 |
|
---|
[214] | 20 | Type _System_GlobalRoot
|
---|
| 21 | ptr As *LONG_PTR
|
---|
| 22 | count As Long
|
---|
| 23 | End Type
|
---|
| 24 |
|
---|
[361] | 25 | Type _System_MemoryObject
|
---|
| 26 | ptr As VoidPtr
|
---|
[370] | 27 | size As SIZE_T
|
---|
[361] | 28 | flags As DWord
|
---|
| 29 | generationCount As Long
|
---|
| 30 | End Type
|
---|
| 31 |
|
---|
[1] | 32 | Class _System_CGarbageCollection
|
---|
[266] | 33 |
|
---|
[369] | 34 | hHeap As HANDLE ' GC用のヒープ
|
---|
| 35 |
|
---|
[361] | 36 | pMemoryObjects As *_System_MemoryObject ' メモリオブジェクト
|
---|
| 37 | countOfMemoryObjects As Long ' 管理するメモリオブジェクトの個数
|
---|
[1] | 38 |
|
---|
| 39 | iAllSize As SIZE_T
|
---|
| 40 |
|
---|
[360] | 41 | isSweeping As Boolean ' スウィープ中かどうか
|
---|
[144] | 42 |
|
---|
[369] | 43 | minPtr As ULONG_PTR
|
---|
| 44 | maxPtr As ULONG_PTR
|
---|
| 45 |
|
---|
[360] | 46 | ' クリティカルセクション
|
---|
[1] | 47 | CriticalSection As CRITICAL_SECTION
|
---|
| 48 |
|
---|
[214] | 49 | ' メモリの上限値(この値を超えるとGCが発動します)
|
---|
| 50 | limitMemorySize As LONG_PTR ' バイト単位
|
---|
| 51 | limitMemoryObjectNum As Long ' メモリオブジェクトの個数単位
|
---|
[109] | 52 |
|
---|
[360] | 53 | isFinish As Boolean ' GC管理が終了したかどうか
|
---|
[144] | 54 |
|
---|
| 55 |
|
---|
[214] | 56 | ' Global Root
|
---|
| 57 | pGlobalRoots As *_System_GlobalRoot
|
---|
| 58 | globalRootNum As Long
|
---|
| 59 | Sub AddGlobalRootPtr( ptr As *LONG_PTR, count As Long )
|
---|
| 60 | pGlobalRoots = _System_realloc( pGlobalRoots, (globalRootNum + 1) * SizeOf(_System_GlobalRoot) )
|
---|
| 61 | pGlobalRoots[globalRootNum].ptr = ptr
|
---|
| 62 | pGlobalRoots[globalRootNum].count = count
|
---|
| 63 | globalRootNum++
|
---|
| 64 | End Sub
|
---|
| 65 |
|
---|
| 66 | Sub RegisterGlobalRoots()
|
---|
| 67 | ' このメソッドの実装はコンパイラが自動生成する
|
---|
| 68 |
|
---|
| 69 | ' AddGlobalRootPtr(...)
|
---|
| 70 | ' ...
|
---|
| 71 | End Sub
|
---|
| 72 |
|
---|
[144] | 73 | ' 特殊クラスのため、コンストラクタ・デストラクタは呼ばれません
|
---|
[1] | 74 | Sub _System_CGarbageCollection()
|
---|
[144] | 75 | End Sub
|
---|
| 76 | Sub ~_System_CGarbageCollection()
|
---|
| 77 | End Sub
|
---|
| 78 |
|
---|
[214] | 79 | Public
|
---|
| 80 |
|
---|
[360] | 81 | /*!
|
---|
| 82 | @brief 環境変数にGCを登録する
|
---|
| 83 | @author Daisuke Yamamoto
|
---|
| 84 | @date 2007/10/21
|
---|
| 85 | */
|
---|
[266] | 86 | Static Sub Initialize()
|
---|
| 87 | Dim temporary[255] As Char
|
---|
| 88 | If GetEnvironmentVariable( "ActiveBasicGarbageCollection", temporary, 255 ) Then
|
---|
| 89 | ' 既にGCがプロセスに存在するとき
|
---|
[364] | 90 | _stscanf( temporary, "%08x", VarPtr( _System_pGC ) )
|
---|
[266] | 91 | MessageBox(0,temporary,"GetEnvironmentVariable",0)
|
---|
| 92 | Else
|
---|
| 93 | _System_pGC = _System_calloc( SizeOf( _System_CGarbageCollection ) )
|
---|
| 94 | _System_pGC->Begin()
|
---|
| 95 |
|
---|
| 96 | ' GCをプロセスに登録する
|
---|
[364] | 97 | _stprintf( temporary, "%08x", _System_pGC )
|
---|
[266] | 98 | SetEnvironmentVariable( "ActiveBasicGarbageCollection", temporary )
|
---|
| 99 | End If
|
---|
| 100 | End Sub
|
---|
| 101 |
|
---|
[360] | 102 | /*!
|
---|
| 103 | @brief メモリサイズの上限を指定する
|
---|
| 104 | @param limitMemorySize メモリサイズの上限(単位はバイト)
|
---|
| 105 | limitMemoryObjectNum メモリ個数の上限
|
---|
| 106 | @author Daisuke Yamamoto
|
---|
| 107 | @date 2007/10/21
|
---|
| 108 | */
|
---|
| 109 | Sub SetLimit( limitMemorySize As LONG_PTR, limitMemoryObjectNum As Long )
|
---|
| 110 | This.limitMemorySize = limitMemorySize
|
---|
| 111 | This.limitMemoryObjectNum = limitMemoryObjectNum
|
---|
| 112 | End Sub
|
---|
| 113 |
|
---|
| 114 | /*!
|
---|
| 115 | @brief 初期化
|
---|
| 116 | @author Daisuke Yamamoto
|
---|
| 117 | @date 2007/10/21
|
---|
| 118 | */
|
---|
[144] | 119 | Sub Begin()
|
---|
[361] | 120 | If pMemoryObjects Then Exit Sub
|
---|
[1] | 121 |
|
---|
[144] | 122 | isFinish = False
|
---|
| 123 |
|
---|
| 124 | 'メモリの上限値(この値を超えるとGCが発動します)
|
---|
[362] | 125 | SetLimit(
|
---|
| 126 | 1024*1024, ' バイト単位
|
---|
| 127 | 2000 ' メモリオブジェクトの個数単位
|
---|
| 128 | )
|
---|
[144] | 129 |
|
---|
[369] | 130 | hHeap = HeapCreate( 0, 0, 0 )
|
---|
| 131 |
|
---|
[361] | 132 | pMemoryObjects = _System_calloc( 1 )
|
---|
| 133 | countOfMemoryObjects=0
|
---|
[1] | 134 |
|
---|
[214] | 135 | ' Global Root
|
---|
| 136 | pGlobalRoots = _System_calloc( 1 )
|
---|
| 137 | globalRootNum = 0
|
---|
| 138 | RegisterGlobalRoots()
|
---|
| 139 |
|
---|
[1] | 140 | iAllSize=0
|
---|
| 141 |
|
---|
[144] | 142 | ' スウィープ中かどうか
|
---|
| 143 | isSweeping = False
|
---|
[1] | 144 |
|
---|
[369] | 145 | minPtr = &HFFFFFFFFFFFFFFFF As ULONG_PTR
|
---|
| 146 | maxPtr = 0
|
---|
| 147 |
|
---|
[1] | 148 | 'クリティカルセッションを生成
|
---|
| 149 | InitializeCriticalSection(CriticalSection)
|
---|
| 150 |
|
---|
[18] | 151 |
|
---|
| 152 | '---------------------------
|
---|
| 153 | ' 開始時のスレッドを通知
|
---|
| 154 | '---------------------------
|
---|
[1] | 155 | Dim hTargetThread As HANDLE
|
---|
| 156 | DuplicateHandle(GetCurrentProcess(),
|
---|
| 157 | GetCurrentThread(),
|
---|
| 158 | GetCurrentProcess(),
|
---|
| 159 | hTargetThread, 0, FALSE, DUPLICATE_SAME_ACCESS) 'カレントスレッドのハンドルを複製
|
---|
[18] | 160 |
|
---|
[144] | 161 | ' スレッド管理用オブジェクトを生成
|
---|
| 162 | _System_pobj_AllThreads = New _System_CThreadCollection()
|
---|
[18] | 163 |
|
---|
[144] | 164 | ' 自身のThreadオブジェクトを生成
|
---|
| 165 | Dim thread As Thread(hTargetThread,GetCurrentThreadId(),0)
|
---|
[330] | 166 | thread.Name = "main"
|
---|
[18] | 167 |
|
---|
[232] | 168 | _System_pobj_AllThreads->BeginThread(ObjPtr( thread ),_System_gc_StackRoot_StartPtr As *LONG_PTR)
|
---|
[144] | 169 |
|
---|
[1] | 170 | End Sub
|
---|
[360] | 171 |
|
---|
| 172 | /*!
|
---|
| 173 | @brief 終了処理
|
---|
| 174 | @author Daisuke Yamamoto
|
---|
| 175 | @date 2007/10/21
|
---|
| 176 | */
|
---|
[144] | 177 | Sub Finish()
|
---|
[361] | 178 | If pMemoryObjects = NULL Then Exit Sub
|
---|
[1] | 179 |
|
---|
[144] | 180 | isFinish = True
|
---|
[79] | 181 |
|
---|
[144] | 182 | ' スレッド管理用オブジェクトを破棄
|
---|
| 183 | Delete _System_pobj_AllThreads
|
---|
[1] | 184 |
|
---|
[144] | 185 | ' 自分以外のスレッドを一時停止
|
---|
| 186 | '_System_pobj_AllThreads->SuspendAnotherThread()
|
---|
| 187 |
|
---|
[259] | 188 | _System_DebugOnly_OutputDebugString( Ex"garbage colletion sweeping all memory objects!\r\n" )
|
---|
[144] | 189 | DeleteAllGarbageMemories()
|
---|
| 190 |
|
---|
| 191 | ' 未解放のメモリオブジェクトをトレース
|
---|
| 192 | DumpMemoryLeaks()
|
---|
| 193 |
|
---|
| 194 | ' 自分以外のスレッドを再開
|
---|
| 195 | '_System_pobj_AllThreads->ResumeAnotherThread()
|
---|
| 196 |
|
---|
[361] | 197 | _System_free( pMemoryObjects )
|
---|
| 198 | pMemoryObjects = NULL
|
---|
[1] | 199 |
|
---|
[214] | 200 | _System_free( pGlobalRoots )
|
---|
| 201 | pGlobalRoots = NULL
|
---|
| 202 |
|
---|
[1] | 203 | 'クリティカルセッションを破棄
|
---|
| 204 | DeleteCriticalSection(CriticalSection)
|
---|
[144] | 205 |
|
---|
[79] | 206 | End Sub
|
---|
[1] | 207 |
|
---|
[360] | 208 | /*!
|
---|
[361] | 209 | @brief メモリオブジェクトからインデックスを取得する
|
---|
| 210 | @param new_ptr メモリオブジェクトへのポインタ
|
---|
| 211 | @author Daisuke Yamamoto
|
---|
| 212 | @date 2007/10/21
|
---|
| 213 | */
|
---|
| 214 | Function GetMemoryObjectPtr( ptr As VoidPtr ) As *_System_MemoryObject
|
---|
| 215 | ' メモリオブジェクトの先頭部分からインデックスを取得する
|
---|
| 216 | Dim index = Get_LONG_PTR( ptr - SizeOf(LONG_PTR) ) As Long
|
---|
| 217 |
|
---|
| 218 | If pMemoryObjects[index].ptr <> ptr Then
|
---|
| 219 | ' メモリイメージが壊れている(先頭に存在するインデックスの整合性が取れない)
|
---|
| 220 | Dim temporary[1024] As Char
|
---|
[364] | 221 | #ifdef _WIN64
|
---|
| 222 | 'wsprintfでは、Windows 2000以降でしか%pが使えない。
|
---|
| 223 | wsprintf( temporary, Ex"indexOfMemoryObjects: %d\r\npMemoryObjects[index].ptr: &H%p\r\nptr: &H%p\r\n",
|
---|
| 224 | index,
|
---|
| 225 | pMemoryObjects[index].ptr,
|
---|
| 226 | ptr )
|
---|
| 227 | #else
|
---|
[361] | 228 | wsprintf( temporary, Ex"indexOfMemoryObjects: %d\r\npMemoryObjects[index].ptr: &H%08x\r\nptr: &H%08x\r\n",
|
---|
| 229 | index,
|
---|
| 230 | pMemoryObjects[index].ptr,
|
---|
| 231 | ptr )
|
---|
[364] | 232 | #endif
|
---|
[361] | 233 | _System_DebugOnly_OutputDebugString( temporary )
|
---|
| 234 | debug
|
---|
| 235 | End If
|
---|
| 236 |
|
---|
| 237 | Return VarPtr( pMemoryObjects[index] )
|
---|
| 238 | End Function
|
---|
| 239 |
|
---|
| 240 | /*!
|
---|
[360] | 241 | @brief メモリオブジェクトを追加する
|
---|
| 242 | @param new_ptr メモリオブジェクトへのポインタ
|
---|
| 243 | size メモリオブジェクトのサイズ
|
---|
| 244 | flags メモリオブジェクトの属性
|
---|
| 245 | @author Daisuke Yamamoto
|
---|
| 246 | @date 2007/10/21
|
---|
| 247 | */
|
---|
[170] | 248 | Sub add(new_ptr As VoidPtr, size As SIZE_T, flags As DWord)
|
---|
[144] | 249 | EnterCriticalSection(CriticalSection)
|
---|
[361] | 250 | iAllSize+=size
|
---|
[1] | 251 |
|
---|
[361] | 252 | ' メモリオブジェクトインスタンスの先頭にインデックスをセットする
|
---|
| 253 | Set_LONG_PTR( new_ptr - SizeOf( LONG_PTR ), countOfMemoryObjects )
|
---|
[1] | 254 |
|
---|
[361] | 255 | pMemoryObjects = _System_realloc( pMemoryObjects, (countOfMemoryObjects+1)*SizeOf(_System_MemoryObject) )
|
---|
| 256 | pMemoryObjects[countOfMemoryObjects].ptr = new_ptr
|
---|
| 257 | pMemoryObjects[countOfMemoryObjects].size = size
|
---|
| 258 | pMemoryObjects[countOfMemoryObjects].flags = flags
|
---|
| 259 | pMemoryObjects[countOfMemoryObjects].generationCount = 0
|
---|
[360] | 260 |
|
---|
[369] | 261 | If minPtr > new_ptr As ULONG_PTR Then
|
---|
| 262 | minPtr = new_ptr As ULONG_PTR
|
---|
| 263 | End If
|
---|
| 264 | If maxPtr < ( new_ptr + size ) As ULONG_PTR Then
|
---|
| 265 | maxPtr = ( new_ptr + size ) As ULONG_PTR
|
---|
| 266 | End If
|
---|
| 267 |
|
---|
[361] | 268 | countOfMemoryObjects++
|
---|
[144] | 269 | LeaveCriticalSection(CriticalSection)
|
---|
[1] | 270 |
|
---|
[330] | 271 | /*
|
---|
| 272 | ' デバッグ用
|
---|
[361] | 273 | If countOfMemoryObjects = 1996 Then
|
---|
[330] | 274 | debug
|
---|
| 275 | End If
|
---|
| 276 | */
|
---|
[1] | 277 | End Sub
|
---|
| 278 |
|
---|
| 279 |
|
---|
[360] | 280 | /*!
|
---|
| 281 | @brief メモリオブジェクトを確保する
|
---|
| 282 | @param size メモリオブジェクトのサイズ
|
---|
| 283 | flags メモリオブジェクトの属性
|
---|
| 284 | @author Daisuke Yamamoto
|
---|
| 285 | @date 2007/10/21
|
---|
| 286 | */
|
---|
[1] | 287 | Function __malloc(size As SIZE_T,flags As Byte) As VoidPtr
|
---|
[361] | 288 | Dim dwFlags As DWord
|
---|
| 289 | If flags and _System_GC_FLAG_INITZERO Then
|
---|
| 290 | dwFlags=HEAP_ZERO_MEMORY
|
---|
| 291 | Else
|
---|
| 292 | dwFlags=0
|
---|
| 293 | End If
|
---|
[1] | 294 |
|
---|
[361] | 295 | ' 実際のメモリバッファはインデックスの分だけ多めに確保する
|
---|
[369] | 296 | Dim ptr = HeapAlloc( hHeap, dwFlags, size + SizeOf( LONG_PTR ) ) + SizeOf( LONG_PTR )
|
---|
[1] | 297 |
|
---|
[361] | 298 | ' 管理対象のメモリオブジェクトとして追加
|
---|
| 299 | add( ptr, size, flags )
|
---|
| 300 |
|
---|
[144] | 301 | Return ptr
|
---|
[1] | 302 | End Function
|
---|
| 303 |
|
---|
[360] | 304 | /*!
|
---|
| 305 | @brief メモリオブジェクトを再確保する
|
---|
| 306 | @param lpMem メモリオブジェクトへのポインタ
|
---|
| 307 | size メモリオブジェクトのサイズ
|
---|
| 308 | flags メモリオブジェクトの属性
|
---|
| 309 | @author Daisuke Yamamoto
|
---|
| 310 | @date 2007/10/21
|
---|
| 311 | */
|
---|
[1] | 312 | Function __realloc(lpMem As VoidPtr, size As SIZE_T) As VoidPtr
|
---|
[18] | 313 | EnterCriticalSection(CriticalSection)
|
---|
[1] | 314 |
|
---|
[361] | 315 | ' メモリオブジェクトを取得
|
---|
| 316 | Dim pTempMemoryObject = GetMemoryObjectPtr( lpMem )
|
---|
[18] | 317 |
|
---|
[361] | 318 | iAllSize += size - pTempMemoryObject->size
|
---|
| 319 |
|
---|
| 320 | pTempMemoryObject->size = size
|
---|
[369] | 321 | pTempMemoryObject->ptr = HeapReAlloc( hHeap, HEAP_ZERO_MEMORY, pTempMemoryObject->ptr - SizeOf(LONG_PTR), size + SizeOf(LONG_PTR) ) + SizeOf(LONG_PTR)
|
---|
[361] | 322 |
|
---|
[369] | 323 | If minPtr > pTempMemoryObject->ptr As ULONG_PTR Then
|
---|
| 324 | minPtr = pTempMemoryObject->ptr As ULONG_PTR
|
---|
| 325 | End If
|
---|
| 326 | If maxPtr < ( pTempMemoryObject->ptr + size ) As ULONG_PTR Then
|
---|
| 327 | maxPtr = ( pTempMemoryObject->ptr + size ) As ULONG_PTR
|
---|
| 328 | End If
|
---|
| 329 |
|
---|
[18] | 330 | LeaveCriticalSection(CriticalSection)
|
---|
[361] | 331 | Return pTempMemoryObject->ptr
|
---|
[1] | 332 | End Function
|
---|
| 333 |
|
---|
[360] | 334 | /*!
|
---|
| 335 | @brief メモリオブジェクトを解放する
|
---|
| 336 | @param lpMem メモリオブジェクトへのポインタ
|
---|
| 337 | isSweeping スウィープ中にこのメソッドが呼ばれるときはTrue、それ以外はFalse
|
---|
| 338 | @author Daisuke Yamamoto
|
---|
| 339 | @date 2007/10/21
|
---|
| 340 | */
|
---|
[144] | 341 | Sub __free_ex(lpMem As VoidPtr, isSweeping As Boolean)
|
---|
[18] | 342 | EnterCriticalSection(CriticalSection)
|
---|
[1] | 343 |
|
---|
[361] | 344 | ' メモリオブジェクトを取得
|
---|
| 345 | Dim pTempMemoryObject = GetMemoryObjectPtr( lpMem )
|
---|
| 346 |
|
---|
| 347 | If (pTempMemoryObject->flags and _System_GC_FLAG_NEEDFREE)<>0 or isSweeping Then
|
---|
| 348 | iAllSize -= pTempMemoryObject->size
|
---|
| 349 |
|
---|
[369] | 350 | HeapFree( hHeap, 0, pTempMemoryObject->ptr - SizeOf(LONG_PTR) )
|
---|
[361] | 351 | pTempMemoryObject->ptr = NULL
|
---|
| 352 | pTempMemoryObject->size = 0
|
---|
| 353 | Else
|
---|
| 354 | If isFinish = False Then
|
---|
| 355 | _System_DebugOnly_OutputDebugString( Ex"heap free missing!\r\n" )
|
---|
[1] | 356 | End If
|
---|
[361] | 357 | End If
|
---|
[18] | 358 | LeaveCriticalSection(CriticalSection)
|
---|
[1] | 359 | End Sub
|
---|
| 360 |
|
---|
[360] | 361 | /*!
|
---|
| 362 | @brief メモリオブジェクトを解放する
|
---|
| 363 | @param lpMem メモリオブジェクトへのポインタ
|
---|
| 364 | @author Daisuke Yamamoto
|
---|
| 365 | @date 2007/10/21
|
---|
| 366 | */
|
---|
[144] | 367 | Sub __free(lpMem As VoidPtr)
|
---|
| 368 | __free_ex( lpMem, False )
|
---|
| 369 | End Sub
|
---|
| 370 |
|
---|
[360] | 371 | /*!
|
---|
[368] | 372 | @brief 必要であればスウィープする
|
---|
[360] | 373 | @author Daisuke Yamamoto
|
---|
| 374 | @date 2007/10/21
|
---|
| 375 | */
|
---|
[368] | 376 | Sub TrySweep()
|
---|
[361] | 377 | If isSweeping <> False or (iAllSize<limitMemorySize and countOfMemoryObjects<limitMemoryObjectNum) Then
|
---|
[171] | 378 | 'メモリ使用量が上限値を超えていないとき
|
---|
| 379 | Exit Sub
|
---|
| 380 | End If
|
---|
[144] | 381 |
|
---|
[368] | 382 | Sweep()
|
---|
| 383 | End Sub
|
---|
| 384 |
|
---|
| 385 | /*!
|
---|
| 386 | @brief スウィープする
|
---|
| 387 | @author Daisuke Yamamoto
|
---|
| 388 | @date 2007/10/21
|
---|
| 389 | */
|
---|
| 390 | Sub Sweep()
|
---|
[171] | 391 | Dim hThread As HANDLE
|
---|
| 392 | Dim ThreadId As DWord
|
---|
| 393 | hThread=_beginthreadex(NULL,0,AddressOf(SweepOnOtherThread),VarPtr(This),0,ThreadId)
|
---|
| 394 | WaitForSingleObject(hThread,INFINITE)
|
---|
| 395 | CloseHandle(hThread)
|
---|
[202] | 396 | isSweeping = False
|
---|
[1] | 397 | End Sub
|
---|
| 398 |
|
---|
[144] | 399 | Private
|
---|
[1] | 400 |
|
---|
[214] | 401 | Static Function IsNull( object As Object ) As Boolean
|
---|
[237] | 402 | Return Object.ReferenceEquals(object, Nothing)
|
---|
[214] | 403 | End Function
|
---|
| 404 |
|
---|
[360] | 405 | /*!
|
---|
| 406 | @brief メモリオブジェクトの生存検地
|
---|
| 407 | @param pSample メモリオブジェクトへのポインタ
|
---|
| 408 | @author Daisuke Yamamoto
|
---|
| 409 | @date 2007/10/21
|
---|
| 410 | */
|
---|
[144] | 411 | Function HitTest(pSample As VoidPtr) As Long
|
---|
[368] | 412 | If pSample = NULL Then
|
---|
| 413 | Return -1
|
---|
| 414 | End If
|
---|
[369] | 415 | If not( minPtr <= pSample and pSample <= maxPtr ) Then
|
---|
| 416 | Return -1
|
---|
| 417 | End If
|
---|
[368] | 418 |
|
---|
[144] | 419 | Dim i As Long
|
---|
[361] | 420 | For i=0 To ELM(countOfMemoryObjects)
|
---|
| 421 | If (pMemoryObjects[i].ptr As LONG_PTR)<=(pSample As LONG_PTR) and (pSample As LONG_PTR)<((pMemoryObjects[i].ptr As LONG_PTR)+pMemoryObjects[i].size) Then
|
---|
[144] | 422 | Return i
|
---|
| 423 | End If
|
---|
| 424 | Next
|
---|
| 425 | Return -1
|
---|
| 426 | End Function
|
---|
[1] | 427 |
|
---|
[360] | 428 | /*!
|
---|
| 429 | @brief オブジェクトのスキャン
|
---|
| 430 | @param pObject オブジェクトへのポインタ
|
---|
| 431 | pbMark マークリスト
|
---|
| 432 | @author Daisuke Yamamoto
|
---|
| 433 | @date 2007/10/21
|
---|
| 434 | */
|
---|
[214] | 435 | Function ScanObject(pObject As *Object, pbMark As *Byte) As Boolean
|
---|
[275] | 436 | Dim classTypeInfo = Nothing As ActiveBasic.Core._System_TypeForClass
|
---|
| 437 | classTypeInfo = pObject->GetType() As ActiveBasic.Core._System_TypeForClass
|
---|
[214] | 438 |
|
---|
| 439 | If IsNull( classTypeInfo ) Then
|
---|
| 440 | Return False
|
---|
| 441 | End If
|
---|
| 442 |
|
---|
[330] | 443 | /*
|
---|
| 444 | _System_DebugOnly_OutputDebugString( " (scanning object)" )
|
---|
| 445 | _System_DebugOnly_OutputDebugString( classTypeInfo.Name )
|
---|
| 446 | _System_DebugOnly_OutputDebugString( Ex"\r\n" )
|
---|
| 447 | */
|
---|
| 448 |
|
---|
[214] | 449 | Dim i As Long
|
---|
| 450 | For i = 0 To ELM(classTypeInfo.numOfReference)
|
---|
| 451 | Scan( (pObject + classTypeInfo.referenceOffsets[i]) As *LONG_PTR, 1, pbMark )
|
---|
| 452 | Next
|
---|
| 453 |
|
---|
| 454 | Return True
|
---|
| 455 | End Function
|
---|
| 456 |
|
---|
[360] | 457 | /*!
|
---|
| 458 | @brief メモリオブジェクトのスキャン
|
---|
| 459 | @param pStartPtr メモリオブジェクトへのポインタ
|
---|
| 460 | maxNum スキャンするメモリオブジェクトの個数
|
---|
| 461 | pbMark マークリスト
|
---|
| 462 | @author Daisuke Yamamoto
|
---|
| 463 | @date 2007/10/21
|
---|
| 464 | */
|
---|
[214] | 465 | Sub Scan(pStartPtr As *LONG_PTR, maxNum As Long, pbMark As *Byte)
|
---|
| 466 | Dim i As Long, index As Long
|
---|
| 467 |
|
---|
| 468 | For i=0 To ELM(maxNum)
|
---|
[144] | 469 | index=HitTest(pStartPtr[i] As VoidPtr)
|
---|
| 470 | If index<>-1 Then
|
---|
| 471 | If pbMark[index]=0 Then
|
---|
| 472 | pbMark[index]=1
|
---|
[170] | 473 |
|
---|
[361] | 474 | ' ジェネレーションカウントを増やす
|
---|
| 475 | pMemoryObjects[index].generationCount ++
|
---|
| 476 |
|
---|
| 477 | If pMemoryObjects[index].flags and _System_GC_FLAG_OBJECT Then
|
---|
[214] | 478 | ' オブジェクトの場合
|
---|
[361] | 479 | If ScanObject( (pMemoryObjects[index].ptr + 4*SizeOf(LONG_PTR)) As *Object, pbMark) = False Then
|
---|
| 480 | Dim maxNum = (pMemoryObjects[index].size\SizeOf(LONG_PTR)) As Long
|
---|
| 481 | Scan(pMemoryObjects[index].ptr As *LONG_PTR, maxNum, pbMark)
|
---|
[214] | 482 | End If
|
---|
| 483 |
|
---|
[361] | 484 | ElseIf (pMemoryObjects[index].flags and _System_GC_FLAG_ATOMIC)=0 Then
|
---|
[214] | 485 | ' ヒープ領域がポインタ値を含む可能性があるとき
|
---|
[361] | 486 | If pMemoryObjects[index].ptr = NULL Then
|
---|
[144] | 487 | 'エラー
|
---|
[170] | 488 |
|
---|
[144] | 489 | End If
|
---|
[214] | 490 |
|
---|
[361] | 491 | Dim maxNum = (pMemoryObjects[index].size\SizeOf(LONG_PTR)) As Long
|
---|
| 492 | Scan(pMemoryObjects[index].ptr As *LONG_PTR, maxNum, pbMark)
|
---|
[144] | 493 | End If
|
---|
| 494 | End If
|
---|
| 495 | End If
|
---|
| 496 | Next
|
---|
| 497 | End Sub
|
---|
[1] | 498 |
|
---|
[360] | 499 | /*!
|
---|
| 500 | @brief グローバル領域をルートに指定してスキャン
|
---|
| 501 | @param pbMark マークリスト
|
---|
| 502 | @author Daisuke Yamamoto
|
---|
| 503 | @date 2007/10/21
|
---|
| 504 | */
|
---|
| 505 | Sub GlobalScan( pbMark As *Byte )
|
---|
| 506 | Dim i As Long
|
---|
| 507 | For i = 0 To ELM( globalRootNum )
|
---|
| 508 | Scan( pGlobalRoots[i].ptr, pGlobalRoots[i].count, pbMark )
|
---|
| 509 | Next
|
---|
| 510 | End Sub
|
---|
| 511 |
|
---|
| 512 | /*!
|
---|
| 513 | @brief ローカル領域をルートに指定してスキャン
|
---|
| 514 | @param pbMark マークリスト
|
---|
| 515 | @author Daisuke Yamamoto
|
---|
| 516 | @date 2007/10/21
|
---|
| 517 | */
|
---|
[144] | 518 | Sub LocalScan( pbMark As *Byte )
|
---|
[18] | 519 | Dim Context As CONTEXT
|
---|
[1] | 520 | Dim NowSp As *LONG_PTR
|
---|
| 521 | Dim size As LONG_PTR
|
---|
[18] | 522 | Dim i As Long
|
---|
| 523 | For i=0 To ELM(_System_pobj_AllThreads->ThreadNum)
|
---|
[330] | 524 | Dim thread = _System_pobj_AllThreads->ppobj_Thread[i]
|
---|
| 525 | If thread Then
|
---|
[1] | 526 | FillMemory(VarPtr(Context),SizeOf(CONTEXT),0)
|
---|
| 527 | Context.ContextFlags=CONTEXT_CONTROL
|
---|
[330] | 528 | If thread->__GetContext(Context)=0 Then
|
---|
[259] | 529 | _System_DebugOnly_OutputDebugString(Ex"レジスタ情報の取得に失敗しました。\r\n")
|
---|
[1] | 530 | End If
|
---|
| 531 |
|
---|
| 532 | #ifdef _WIN64
|
---|
| 533 | NowSp=Context.Rsp As *LONG_PTR
|
---|
| 534 | #else
|
---|
| 535 | NowSp=Context.Esp As *LONG_PTR
|
---|
| 536 | #endif
|
---|
| 537 |
|
---|
[214] | 538 | Dim size=(_System_pobj_AllThreads->pStackBase[i] As LONG_PTR)-(NowSp As LONG_PTR)
|
---|
| 539 | Dim maxNum = (size\SizeOf(LONG_PTR)) As Long
|
---|
[1] | 540 |
|
---|
[144] | 541 | If NowSp = 0 Then
|
---|
| 542 | debug
|
---|
| 543 | Exit Sub
|
---|
| 544 | End If
|
---|
| 545 |
|
---|
[330] | 546 | /*
|
---|
| 547 | _System_DebugOnly_OutputDebugString( "(scanning thread local)" )
|
---|
| 548 | _System_DebugOnly_OutputDebugString( thread.Name )
|
---|
| 549 | _System_DebugOnly_OutputDebugString( Ex"\r\n" )
|
---|
| 550 | */
|
---|
| 551 |
|
---|
[214] | 552 | Scan( NowSp, maxNum, pbMark )
|
---|
[1] | 553 | End If
|
---|
| 554 | Next
|
---|
[144] | 555 | End Sub
|
---|
[1] | 556 |
|
---|
[360] | 557 | /*!
|
---|
| 558 | @brief 生存していないメモリオブジェクトを解放する
|
---|
| 559 | @param pbMark マークリスト
|
---|
| 560 | @author Daisuke Yamamoto
|
---|
| 561 | @date 2007/10/21
|
---|
| 562 | */
|
---|
[144] | 563 | Sub DeleteGarbageMemories( pbMark As *Byte )
|
---|
[1] | 564 |
|
---|
[144] | 565 | Dim isAllDelete = False
|
---|
| 566 | If pbMark = NULL Then
|
---|
| 567 | ' すべてを破棄するとき
|
---|
| 568 | isAllDelete = True
|
---|
[361] | 569 | pbMark = _System_calloc( countOfMemoryObjects )
|
---|
[144] | 570 | End If
|
---|
[1] | 571 |
|
---|
[144] | 572 | Dim i As Long
|
---|
[361] | 573 | For i=0 To ELM(countOfMemoryObjects)
|
---|
| 574 | If pbMark[i]=0 and pMemoryObjects[i].ptr<>0 and (pMemoryObjects[i].flags and _System_GC_FLAG_NEEDFREE)=0 Then
|
---|
| 575 | If pMemoryObjects[i].ptr = NULL Then
|
---|
[144] | 576 | If isAllDelete Then
|
---|
| 577 | Continue
|
---|
| 578 | Else
|
---|
| 579 | debug
|
---|
| 580 | End If
|
---|
| 581 | End If
|
---|
[1] | 582 |
|
---|
[361] | 583 | Dim ptr = pMemoryObjects[i].ptr
|
---|
| 584 | Dim size = pMemoryObjects[i].size
|
---|
[144] | 585 |
|
---|
[361] | 586 | If (pMemoryObjects[i].flags and _System_GC_FLAG_OBJECT) <> 0 Then
|
---|
[144] | 587 | /* ・オブジェクトの個数
|
---|
| 588 | ・オブジェクトのサイズ
|
---|
| 589 | ・デストラクタの関数ポインタ
|
---|
[249] | 590 | ・リザーブ領域
|
---|
[144] | 591 | を考慮 */
|
---|
[249] | 592 | _System_SweepingDelete (ptr + SizeOf( LONG_PTR ) * 4 )
|
---|
[144] | 593 | Else
|
---|
[361] | 594 | __free_ex( ptr, True )
|
---|
[144] | 595 | End If
|
---|
[1] | 596 | End If
|
---|
| 597 | Next
|
---|
| 598 |
|
---|
[144] | 599 | If isAllDelete Then
|
---|
| 600 | _System_free( pbMark )
|
---|
| 601 | End If
|
---|
| 602 |
|
---|
| 603 | End Sub
|
---|
| 604 |
|
---|
[360] | 605 | /*!
|
---|
| 606 | @brief GCが管理するすべてのメモリオブジェクトを解放する
|
---|
| 607 | @author Daisuke Yamamoto
|
---|
| 608 | @date 2007/10/21
|
---|
| 609 | */
|
---|
[144] | 610 | Sub DeleteAllGarbageMemories()
|
---|
| 611 | DeleteGarbageMemories( NULL )
|
---|
| 612 | End Sub
|
---|
| 613 |
|
---|
[360] | 614 | /*!
|
---|
| 615 | @brief コンパクション
|
---|
| 616 | @author Daisuke Yamamoto
|
---|
| 617 | @date 2007/10/21
|
---|
| 618 | */
|
---|
[144] | 619 | Sub Compaction()
|
---|
| 620 | Dim i As Long, i2 = 0 As Long
|
---|
[361] | 621 | For i=0 To ELM(countOfMemoryObjects)
|
---|
| 622 | pMemoryObjects[i2] = pMemoryObjects[i]
|
---|
[144] | 623 |
|
---|
[361] | 624 | If pMemoryObjects[i2].ptr Then
|
---|
| 625 | ' メモリオブジェクトの先頭部分にあるインデックスを書き換える
|
---|
| 626 | Set_LONG_PTR( pMemoryObjects[i2].ptr - SizeOf(LONG_PTR), i2 )
|
---|
| 627 |
|
---|
[144] | 628 | i2++
|
---|
| 629 | End If
|
---|
| 630 | Next
|
---|
[361] | 631 | countOfMemoryObjects = i2
|
---|
[144] | 632 | End Sub
|
---|
| 633 |
|
---|
[360] | 634 | /*!
|
---|
| 635 | @brief スウィープ(新規スレッドで呼び出す必要あり)
|
---|
| 636 | @author Daisuke Yamamoto
|
---|
| 637 | @date 2007/10/21
|
---|
| 638 | */
|
---|
[284] | 639 | Function SweepOnOtherThread() As Long
|
---|
[171] | 640 | EnterCriticalSection(CriticalSection)
|
---|
[144] | 641 |
|
---|
[214] | 642 |
|
---|
| 643 | Dim startTime = GetTickCount()
|
---|
| 644 |
|
---|
[259] | 645 | _System_DebugOnly_OutputDebugString( Ex"garbage colletion sweep start!\r\n" )
|
---|
[214] | 646 |
|
---|
| 647 |
|
---|
[368] | 648 | 'If isSweeping <> False or (iAllSize<limitMemorySize and countOfMemoryObjects<limitMemoryObjectNum) Then
|
---|
| 649 | ' ExitThread(0)
|
---|
| 650 | 'End If
|
---|
[203] | 651 | isSweeping = True
|
---|
[202] | 652 |
|
---|
[144] | 653 | ' すべてのスレッドを一時停止
|
---|
| 654 | _System_pobj_AllThreads->SuspendAllThread()
|
---|
| 655 |
|
---|
| 656 | ' マークリストを生成
|
---|
[369] | 657 | Dim pbMark = _System_calloc(countOfMemoryObjects*SizeOf(Byte)) As *Byte
|
---|
[144] | 658 |
|
---|
| 659 | ' グローバル領域をルートに指定してスキャン
|
---|
[360] | 660 | GlobalScan( pbMark )
|
---|
[144] | 661 |
|
---|
| 662 | ' ローカル領域をルートに指定してスキャン
|
---|
| 663 | LocalScan( pbMark )
|
---|
| 664 |
|
---|
| 665 | ' スウィープ前のメモリサイズを退避
|
---|
| 666 | Dim iBackAllSize = iAllSize
|
---|
| 667 |
|
---|
| 668 | ' スウィープ前のメモリオブジェクトの数
|
---|
[361] | 669 | Dim iBeforeN = countOfMemoryObjects
|
---|
[144] | 670 |
|
---|
| 671 | '使われていないメモリを解放する
|
---|
| 672 | DeleteGarbageMemories(pbMark)
|
---|
| 673 |
|
---|
| 674 | 'コンパクション
|
---|
| 675 | Compaction()
|
---|
| 676 |
|
---|
[1] | 677 | 'マークリストを解放
|
---|
[369] | 678 | _System_free(pbMark)
|
---|
[1] | 679 |
|
---|
[299] | 680 | If iBackAllSize <= iAllSize * 2 Then
|
---|
[144] | 681 | If iAllSize > limitMemorySize Then
|
---|
| 682 | limitMemorySize = iAllSize
|
---|
| 683 | End If
|
---|
| 684 |
|
---|
[1] | 685 | '許容量を拡張する
|
---|
[144] | 686 | limitMemorySize *= 2
|
---|
[214] | 687 | limitMemoryObjectNum *= 2
|
---|
[144] | 688 |
|
---|
[259] | 689 | _System_DebugOnly_OutputDebugString( Ex"memory size is extended for gc!\r\n" )
|
---|
[1] | 690 | End If
|
---|
| 691 |
|
---|
[144] | 692 | Dim temp[100] As Char
|
---|
[361] | 693 | wsprintf(temp,Ex"object items ... %d -> %d ( %d MB -> %d MB )\r\n",iBeforeN,countOfMemoryObjects, iBackAllSize\1024\1024, iAllSize\1024\1024)
|
---|
[259] | 694 | _System_DebugOnly_OutputDebugString( temp )
|
---|
[144] | 695 | wsprintf(temp,Ex"limit size of memory ... %d\r\n",limitMemorySize)
|
---|
[259] | 696 | _System_DebugOnly_OutputDebugString( temp )
|
---|
[214] | 697 | wsprintf(temp,Ex"garbage colletion sweep finish! (%d ms)\r\n\r\n", GetTickCount()-startTime)
|
---|
[259] | 698 | _System_DebugOnly_OutputDebugString( temp )
|
---|
[1] | 699 |
|
---|
[144] | 700 |
|
---|
[1] | 701 | '-------------------------------------
|
---|
| 702 | ' すべてのスレッドを再開
|
---|
| 703 | '-------------------------------------
|
---|
[18] | 704 | _System_pobj_AllThreads->ResumeAllThread()
|
---|
[171] | 705 |
|
---|
| 706 | LeaveCriticalSection(CriticalSection)
|
---|
[1] | 707 | End Function
|
---|
| 708 |
|
---|
[360] | 709 | /*!
|
---|
| 710 | @brief 未解放のメモリオブジェクトをデバッグ出力する
|
---|
| 711 | @author Daisuke Yamamoto
|
---|
| 712 | @date 2007/10/21
|
---|
| 713 | */
|
---|
[144] | 714 | Sub DumpMemoryLeaks()
|
---|
| 715 | Dim isLeak = False
|
---|
[1] | 716 | Dim i As Long
|
---|
[361] | 717 | For i=0 To ELM(countOfMemoryObjects)
|
---|
| 718 | If pMemoryObjects[i].ptr Then
|
---|
| 719 | If (pMemoryObjects[i].flags and _System_GC_FLAG_NEEDFREE)<>0 Then
|
---|
[144] | 720 | If isLeak = False Then
|
---|
[259] | 721 | _System_DebugOnly_OutputDebugString( Ex"Detected memory leaks!\r\n" )
|
---|
[144] | 722 | isLeak = True
|
---|
| 723 | End If
|
---|
[1] | 724 |
|
---|
[144] | 725 | Dim temp[100] As Char
|
---|
[259] | 726 | _System_DebugOnly_OutputDebugString( Ex"heap free missing!\r\n" )
|
---|
[364] | 727 | #ifdef _WIN64
|
---|
| 728 | wsprintf(temp,Ex"{%d} normal block at &H%p, %d bytes long.\r\n", i, pMemoryObjects[i].ptr, pMemoryObjects[i].size)
|
---|
| 729 | #else
|
---|
[361] | 730 | wsprintf(temp,Ex"{%d} normal block at &H%08X, %d bytes long.\r\n", i, pMemoryObjects[i].ptr, pMemoryObjects[i].size)
|
---|
[364] | 731 | #endif
|
---|
[259] | 732 | _System_DebugOnly_OutputDebugString( temp )
|
---|
[1] | 733 | End If
|
---|
| 734 | End If
|
---|
| 735 | Next
|
---|
[144] | 736 |
|
---|
| 737 | If isLeak Then
|
---|
[259] | 738 | _System_DebugOnly_OutputDebugString( Ex"Object dump complete.\r\n" )
|
---|
[144] | 739 | End If
|
---|
[170] | 740 |
|
---|
[1] | 741 | End Sub
|
---|
[144] | 742 |
|
---|
[1] | 743 | End Class
|
---|
| 744 |
|
---|
| 745 | 'GC管理用の特殊なシステムオブジェクト(デストラクタは最終のタイミングで呼び出されます)
|
---|
[144] | 746 | Dim _System_pGC As *_System_CGarbageCollection
|
---|
[1] | 747 |
|
---|
| 748 |
|
---|
| 749 |
|
---|
| 750 | Function GC_malloc(size As Long) As VoidPtr
|
---|
| 751 | ' sweep
|
---|
[368] | 752 | _System_pGC->TrySweep()
|
---|
[1] | 753 |
|
---|
| 754 | 'allocate
|
---|
[144] | 755 | Return _System_pGC->__malloc(size,0)
|
---|
[1] | 756 | End Function
|
---|
| 757 |
|
---|
| 758 | Function GC_malloc_atomic(size As Long) As VoidPtr
|
---|
| 759 | ' sweep
|
---|
[368] | 760 | _System_pGC->TrySweep()
|
---|
[1] | 761 |
|
---|
| 762 | 'allocate
|
---|
[144] | 763 | Return _System_pGC->__malloc(size,_System_GC_FLAG_ATOMIC)
|
---|
[1] | 764 | End Function
|
---|
[144] | 765 |
|
---|
| 766 | Function _System_GC_malloc_ForObject(size As Long) As VoidPtr
|
---|
| 767 | ' sweep
|
---|
[368] | 768 | _System_pGC->TrySweep()
|
---|
[144] | 769 |
|
---|
| 770 | 'allocate
|
---|
| 771 | Return _System_pGC->__malloc(size,_System_GC_FLAG_OBJECT or _System_GC_FLAG_INITZERO)
|
---|
| 772 | End Function
|
---|
| 773 |
|
---|
| 774 | Function _System_GC_malloc_ForObjectPtr(size As Long) As VoidPtr
|
---|
| 775 | ' sweep
|
---|
[368] | 776 | _System_pGC->TrySweep()
|
---|
[144] | 777 |
|
---|
| 778 | 'allocate
|
---|
| 779 | Return _System_pGC->__malloc(size,_System_GC_FLAG_OBJECT or _System_GC_FLAG_INITZERO or _System_GC_FLAG_NEEDFREE)
|
---|
| 780 | End Function
|
---|
| 781 |
|
---|
| 782 | Function _System_GC_free_for_SweepingDelete( ptr As *Object )
|
---|
| 783 | ' free
|
---|
| 784 | _System_pGC->__free_ex( ptr, True )
|
---|
| 785 | End Function
|
---|