source: trunk/Include/system/gc.sbp@ 383

Last change on this file since 383 was 373, checked in by dai, 17 years ago

Try-Catchを試験的に実装。
(まだ下記の動作しか実装していません)
・Try
・Catch(パラメータ無し)
・Throw(パラメータ無し)

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