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

Last change on this file since 413 was 413, checked in by dai, 16 years ago

ポインタ型の型情報取得に対応した。

File size: 22.1 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 ' スレッド管理用オブジェクトを生成
[400]158 _System_pobj_AllThreads = New Detail._System_CThreadCollection()
[18]159
[144]160 ' 自身のThreadオブジェクトを生成
[400]161 Dim thread = New Thread(hTargetThread, GetCurrentThreadId(), 0)
[330]162 thread.Name = "main"
[18]163
[400]164 _System_pobj_AllThreads->BeginThread(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 ' 実際のメモリバッファはインデックスの分だけ多めに確保する
[399]292 __malloc = HeapAlloc( hHeap, dwFlags, size + SizeOf( LONG_PTR ) )
293 throwIfAllocationFailed( __malloc, size )
294 __malloc += SizeOf( LONG_PTR )
[1]295
[361]296 ' 管理対象のメモリオブジェクトとして追加
[399]297 add( __malloc, size, flags )
[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
[399]317 __realloc = HeapReAlloc( hHeap, HEAP_ZERO_MEMORY, pTempMemoryObject->ptr - SizeOf(LONG_PTR), size + SizeOf(LONG_PTR) )
318 If __realloc = 0 Then
319 LeaveCriticalSection(CriticalSection)
320 throwIfAllocationFailed(0, size)
321 End If
322 __realloc += SizeOf(LONG_PTR)
323 pTempMemoryObject->ptr = __realloc
[361]324
[399]325
[369]326 If minPtr > pTempMemoryObject->ptr As ULONG_PTR Then
327 minPtr = pTempMemoryObject->ptr As ULONG_PTR
328 End If
329 If maxPtr < ( pTempMemoryObject->ptr + size ) As ULONG_PTR Then
330 maxPtr = ( pTempMemoryObject->ptr + size ) As ULONG_PTR
331 End If
[18]332 LeaveCriticalSection(CriticalSection)
[1]333 End Function
334
[360]335 /*!
[399]336 @brief メモリ確保に失敗したか(NULLかどうか)を調べ、失敗していたら例外を投げる。
337 @param[in] p メモリへのポインタ
338 @param[in] size 確保しようとした大きさ
339 @exception OutOfMemoryException pがNULLだったとき
340 @author Egtra
341 @date 2007/12/24
342 ただし、sizeがあまりにも小さい場合は、例外を投げず、即座に終了する。
343 */
344 Sub throwIfAllocationFailed(p As VoidPtr, size As SIZE_T)
345 If p = 0 Then
346 If size < 256 Then
347 /*
348  これだけのメモリも確保できない状況では、OutOfMemoryException
349 のインスタンスすら作成できないかもしれないし、例え作成できても、
350 その後、結局メモリ不足でろくなことを行えないはず。そのため、
351 ここですぐに終了することにする。
352  なお、この値は特に根拠があって定められた値ではない。
353 */
354 HeapDestroy(hHeap)
355 OutputDebugString("AB malloc: Out of memory.")
356 ExitProcess(-1)
357 End If
[400]358 Dim s2 = Nothing As Object '#145
359 s2 = New System.UInt64(size)
360 Throw New System.OutOfMemoryException(ActiveBasic.Strings.SPrintf("malloc: Failed to allocate %zu (%&zx) byte(s) memory.", s2, s2))
[399]361 End If
362 End Sub
363
364 /*!
[360]365 @brief メモリオブジェクトを解放する
366 @param lpMem メモリオブジェクトへのポインタ
367 isSweeping スウィープ中にこのメソッドが呼ばれるときはTrue、それ以外はFalse
368 @author Daisuke Yamamoto
369 @date 2007/10/21
370 */
[144]371 Sub __free_ex(lpMem As VoidPtr, isSweeping As Boolean)
[18]372 EnterCriticalSection(CriticalSection)
[1]373
[361]374 ' メモリオブジェクトを取得
375 Dim pTempMemoryObject = GetMemoryObjectPtr( lpMem )
376
377 If (pTempMemoryObject->flags and _System_GC_FLAG_NEEDFREE)<>0 or isSweeping Then
378 iAllSize -= pTempMemoryObject->size
379
[369]380 HeapFree( hHeap, 0, pTempMemoryObject->ptr - SizeOf(LONG_PTR) )
[361]381 pTempMemoryObject->ptr = NULL
382 pTempMemoryObject->size = 0
383 Else
384 If isFinish = False Then
385 _System_DebugOnly_OutputDebugString( Ex"heap free missing!\r\n" )
[1]386 End If
[361]387 End If
[18]388 LeaveCriticalSection(CriticalSection)
[1]389 End Sub
390
[360]391 /*!
392 @brief メモリオブジェクトを解放する
393 @param lpMem メモリオブジェクトへのポインタ
394 @author Daisuke Yamamoto
395 @date 2007/10/21
396 */
[144]397 Sub __free(lpMem As VoidPtr)
398 __free_ex( lpMem, False )
399 End Sub
400
[360]401 /*!
[368]402 @brief 必要であればスウィープする
[360]403 @author Daisuke Yamamoto
404 @date 2007/10/21
405 */
[368]406 Sub TrySweep()
[361]407 If isSweeping <> False or (iAllSize<limitMemorySize and countOfMemoryObjects<limitMemoryObjectNum) Then
[171]408 'メモリ使用量が上限値を超えていないとき
409 Exit Sub
410 End If
[144]411
[368]412 Sweep()
413 End Sub
414
415 /*!
416 @brief スウィープする
417 @author Daisuke Yamamoto
418 @date 2007/10/21
419 */
420 Sub Sweep()
[171]421 Dim hThread As HANDLE
422 Dim ThreadId As DWord
423 hThread=_beginthreadex(NULL,0,AddressOf(SweepOnOtherThread),VarPtr(This),0,ThreadId)
424 WaitForSingleObject(hThread,INFINITE)
425 CloseHandle(hThread)
[202]426 isSweeping = False
[1]427 End Sub
428
[144]429Private
[1]430
[214]431 Static Function IsNull( object As Object ) As Boolean
[237]432 Return Object.ReferenceEquals(object, Nothing)
[214]433 End Function
434
[360]435 /*!
436 @brief メモリオブジェクトの生存検地
437 @param pSample メモリオブジェクトへのポインタ
438 @author Daisuke Yamamoto
439 @date 2007/10/21
440 */
[144]441 Function HitTest(pSample As VoidPtr) As Long
[368]442 If pSample = NULL Then
443 Return -1
444 End If
[369]445 If not( minPtr <= pSample and pSample <= maxPtr ) Then
446 Return -1
447 End If
[368]448
[144]449 Dim i As Long
[361]450 For i=0 To ELM(countOfMemoryObjects)
451 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]452 Return i
453 End If
454 Next
455 Return -1
456 End Function
[1]457
[360]458 /*!
459 @brief オブジェクトのスキャン
460 @param pObject オブジェクトへのポインタ
461 pbMark マークリスト
462 @author Daisuke Yamamoto
463 @date 2007/10/21
464 */
[214]465 Function ScanObject(pObject As *Object, pbMark As *Byte) As Boolean
[275]466 Dim classTypeInfo = Nothing As ActiveBasic.Core._System_TypeForClass
467 classTypeInfo = pObject->GetType() As ActiveBasic.Core._System_TypeForClass
[214]468
469 If IsNull( classTypeInfo ) Then
470 Return False
471 End If
472
[413]473 If Object.ReferenceEquals( classTypeInfo, ActiveBasic.Core._System_TypeBase.selfTypeInfo ) Then
474 ' TypeInfoクラスの場合はTypeBaseImplクラスとして扱う
475 classTypeInfo = _System_TypeBase_Search( "ActiveBasic.Core.TypeBaseImpl" ) As ActiveBasic.Core._System_TypeForClass
476 End If
477
[330]478 /*
479 _System_DebugOnly_OutputDebugString( " (scanning object)" )
480 _System_DebugOnly_OutputDebugString( classTypeInfo.Name )
481 _System_DebugOnly_OutputDebugString( Ex"\r\n" )
482 */
483
[214]484 Dim i As Long
485 For i = 0 To ELM(classTypeInfo.numOfReference)
486 Scan( (pObject + classTypeInfo.referenceOffsets[i]) As *LONG_PTR, 1, pbMark )
487 Next
488
489 Return True
490 End Function
491
[360]492 /*!
493 @brief メモリオブジェクトのスキャン
494 @param pStartPtr メモリオブジェクトへのポインタ
495 maxNum スキャンするメモリオブジェクトの個数
496 pbMark マークリスト
497 @author Daisuke Yamamoto
498 @date 2007/10/21
499 */
[214]500 Sub Scan(pStartPtr As *LONG_PTR, maxNum As Long, pbMark As *Byte)
501 Dim i As Long, index As Long
502
503 For i=0 To ELM(maxNum)
[144]504 index=HitTest(pStartPtr[i] As VoidPtr)
505 If index<>-1 Then
506 If pbMark[index]=0 Then
507 pbMark[index]=1
[170]508
[361]509 ' ジェネレーションカウントを増やす
510 pMemoryObjects[index].generationCount ++
511
512 If pMemoryObjects[index].flags and _System_GC_FLAG_OBJECT Then
[214]513 ' オブジェクトの場合
[361]514 If ScanObject( (pMemoryObjects[index].ptr + 4*SizeOf(LONG_PTR)) As *Object, pbMark) = False Then
515 Dim maxNum = (pMemoryObjects[index].size\SizeOf(LONG_PTR)) As Long
516 Scan(pMemoryObjects[index].ptr As *LONG_PTR, maxNum, pbMark)
[214]517 End If
518
[361]519 ElseIf (pMemoryObjects[index].flags and _System_GC_FLAG_ATOMIC)=0 Then
[214]520 ' ヒープ領域がポインタ値を含む可能性があるとき
[361]521 If pMemoryObjects[index].ptr = NULL Then
[144]522 'エラー
[170]523
[144]524 End If
[214]525
[361]526 Dim maxNum = (pMemoryObjects[index].size\SizeOf(LONG_PTR)) As Long
527 Scan(pMemoryObjects[index].ptr As *LONG_PTR, maxNum, pbMark)
[144]528 End If
529 End If
530 End If
531 Next
532 End Sub
[1]533
[360]534 /*!
535 @brief グローバル領域をルートに指定してスキャン
536 @param pbMark マークリスト
537 @author Daisuke Yamamoto
538 @date 2007/10/21
539 */
540 Sub GlobalScan( pbMark As *Byte )
541 Dim i As Long
542 For i = 0 To ELM( globalRootNum )
543 Scan( pGlobalRoots[i].ptr, pGlobalRoots[i].count, pbMark )
544 Next
545 End Sub
546
547 /*!
548 @brief ローカル領域をルートに指定してスキャン
549 @param pbMark マークリスト
550 @author Daisuke Yamamoto
551 @date 2007/10/21
552 */
[144]553 Sub LocalScan( pbMark As *Byte )
[18]554 Dim Context As CONTEXT
[1]555 Dim NowSp As *LONG_PTR
556 Dim size As LONG_PTR
[18]557 Dim i As Long
558 For i=0 To ELM(_System_pobj_AllThreads->ThreadNum)
[400]559 Dim thread = _System_pobj_AllThreads->collection[i].thread
560 If Not ActiveBasic.IsNothing(thread) Then
[1]561 FillMemory(VarPtr(Context),SizeOf(CONTEXT),0)
562 Context.ContextFlags=CONTEXT_CONTROL
[400]563 If thread.__GetContext(Context)=0 Then
[259]564 _System_DebugOnly_OutputDebugString(Ex"レジスタ情報の取得に失敗しました。\r\n")
[1]565 End If
566
567#ifdef _WIN64
568 NowSp=Context.Rsp As *LONG_PTR
569#else
570 NowSp=Context.Esp As *LONG_PTR
571#endif
572
[400]573 Dim size=(_System_pobj_AllThreads->collection[i].stackBase As LONG_PTR)-(NowSp As LONG_PTR)
[214]574 Dim maxNum = (size\SizeOf(LONG_PTR)) As Long
[1]575
[144]576 If NowSp = 0 Then
577 debug
578 Exit Sub
579 End If
580
[330]581 /*
582 _System_DebugOnly_OutputDebugString( "(scanning thread local)" )
583 _System_DebugOnly_OutputDebugString( thread.Name )
584 _System_DebugOnly_OutputDebugString( Ex"\r\n" )
585 */
586
[214]587 Scan( NowSp, maxNum, pbMark )
[1]588 End If
589 Next
[144]590 End Sub
[1]591
[360]592 /*!
593 @brief 生存していないメモリオブジェクトを解放する
594 @param pbMark マークリスト
595 @author Daisuke Yamamoto
596 @date 2007/10/21
597 */
[144]598 Sub DeleteGarbageMemories( pbMark As *Byte )
[1]599
[144]600 Dim isAllDelete = False
601 If pbMark = NULL Then
602 ' すべてを破棄するとき
603 isAllDelete = True
[361]604 pbMark = _System_calloc( countOfMemoryObjects )
[144]605 End If
[1]606
[144]607 Dim i As Long
[361]608 For i=0 To ELM(countOfMemoryObjects)
609 If pbMark[i]=0 and pMemoryObjects[i].ptr<>0 and (pMemoryObjects[i].flags and _System_GC_FLAG_NEEDFREE)=0 Then
610 If pMemoryObjects[i].ptr = NULL Then
[144]611 If isAllDelete Then
612 Continue
613 Else
614 debug
615 End If
616 End If
[1]617
[361]618 Dim ptr = pMemoryObjects[i].ptr
619 Dim size = pMemoryObjects[i].size
[144]620
[361]621 If (pMemoryObjects[i].flags and _System_GC_FLAG_OBJECT) <> 0 Then
[144]622 /* ・オブジェクトの個数
623 ・オブジェクトのサイズ
624 ・デストラクタの関数ポインタ
[249]625 ・リザーブ領域
[144]626 を考慮 */
[249]627 _System_SweepingDelete (ptr + SizeOf( LONG_PTR ) * 4 )
[144]628 Else
[361]629 __free_ex( ptr, True )
[144]630 End If
[1]631 End If
632 Next
633
[144]634 If isAllDelete Then
635 _System_free( pbMark )
636 End If
637
638 End Sub
639
[360]640 /*!
641 @brief GCが管理するすべてのメモリオブジェクトを解放する
642 @author Daisuke Yamamoto
643 @date 2007/10/21
644 */
[144]645 Sub DeleteAllGarbageMemories()
646 DeleteGarbageMemories( NULL )
647 End Sub
648
[360]649 /*!
650 @brief コンパクション
651 @author Daisuke Yamamoto
652 @date 2007/10/21
653 */
[144]654 Sub Compaction()
655 Dim i As Long, i2 = 0 As Long
[361]656 For i=0 To ELM(countOfMemoryObjects)
657 pMemoryObjects[i2] = pMemoryObjects[i]
[144]658
[361]659 If pMemoryObjects[i2].ptr Then
660 ' メモリオブジェクトの先頭部分にあるインデックスを書き換える
661 Set_LONG_PTR( pMemoryObjects[i2].ptr - SizeOf(LONG_PTR), i2 )
662
[144]663 i2++
664 End If
665 Next
[361]666 countOfMemoryObjects = i2
[144]667 End Sub
668
[360]669 /*!
670 @brief スウィープ(新規スレッドで呼び出す必要あり)
671 @author Daisuke Yamamoto
672 @date 2007/10/21
673 */
[284]674 Function SweepOnOtherThread() As Long
[400]675 Imports System.Threading.Detail
[171]676 EnterCriticalSection(CriticalSection)
[144]677
[214]678 Dim startTime = GetTickCount()
679
[259]680 _System_DebugOnly_OutputDebugString( Ex"garbage colletion sweep start!\r\n" )
[214]681
682
[368]683 'If isSweeping <> False or (iAllSize<limitMemorySize and countOfMemoryObjects<limitMemoryObjectNum) Then
684 ' ExitThread(0)
685 'End If
[203]686 isSweeping = True
[202]687
[144]688 ' すべてのスレッドを一時停止
689 _System_pobj_AllThreads->SuspendAllThread()
690
691 ' マークリストを生成
[369]692 Dim pbMark = _System_calloc(countOfMemoryObjects*SizeOf(Byte)) As *Byte
[144]693
694 ' グローバル領域をルートに指定してスキャン
[360]695 GlobalScan( pbMark )
[144]696
697 ' ローカル領域をルートに指定してスキャン
698 LocalScan( pbMark )
699
700 ' スウィープ前のメモリサイズを退避
701 Dim iBackAllSize = iAllSize
702
703 ' スウィープ前のメモリオブジェクトの数
[361]704 Dim iBeforeN = countOfMemoryObjects
[144]705
706 '使われていないメモリを解放する
707 DeleteGarbageMemories(pbMark)
708
709 'コンパクション
710 Compaction()
711
[1]712 'マークリストを解放
[369]713 _System_free(pbMark)
[1]714
[299]715 If iBackAllSize <= iAllSize * 2 Then
[144]716 If iAllSize > limitMemorySize Then
717 limitMemorySize = iAllSize
718 End If
719
[1]720 '許容量を拡張する
[144]721 limitMemorySize *= 2
[214]722 limitMemoryObjectNum *= 2
[144]723
[259]724 _System_DebugOnly_OutputDebugString( Ex"memory size is extended for gc!\r\n" )
[1]725 End If
726
[144]727 Dim temp[100] As Char
[361]728 wsprintf(temp,Ex"object items ... %d -> %d ( %d MB -> %d MB )\r\n",iBeforeN,countOfMemoryObjects, iBackAllSize\1024\1024, iAllSize\1024\1024)
[259]729 _System_DebugOnly_OutputDebugString( temp )
[144]730 wsprintf(temp,Ex"limit size of memory ... %d\r\n",limitMemorySize)
[259]731 _System_DebugOnly_OutputDebugString( temp )
[214]732 wsprintf(temp,Ex"garbage colletion sweep finish! (%d ms)\r\n\r\n", GetTickCount()-startTime)
[259]733 _System_DebugOnly_OutputDebugString( temp )
[1]734
[144]735
[1]736 '-------------------------------------
737 ' すべてのスレッドを再開
738 '-------------------------------------
[18]739 _System_pobj_AllThreads->ResumeAllThread()
[171]740
741 LeaveCriticalSection(CriticalSection)
[1]742 End Function
743
[360]744 /*!
745 @brief 未解放のメモリオブジェクトをデバッグ出力する
746 @author Daisuke Yamamoto
747 @date 2007/10/21
748 */
[144]749 Sub DumpMemoryLeaks()
750 Dim isLeak = False
[1]751 Dim i As Long
[361]752 For i=0 To ELM(countOfMemoryObjects)
753 If pMemoryObjects[i].ptr Then
754 If (pMemoryObjects[i].flags and _System_GC_FLAG_NEEDFREE)<>0 Then
[144]755 If isLeak = False Then
[259]756 _System_DebugOnly_OutputDebugString( Ex"Detected memory leaks!\r\n" )
[144]757 isLeak = True
758 End If
[1]759
[144]760 Dim temp[100] As Char
[259]761 _System_DebugOnly_OutputDebugString( Ex"heap free missing!\r\n" )
[364]762#ifdef _WIN64
763 wsprintf(temp,Ex"{%d} normal block at &H%p, %d bytes long.\r\n", i, pMemoryObjects[i].ptr, pMemoryObjects[i].size)
764#else
[361]765 wsprintf(temp,Ex"{%d} normal block at &H%08X, %d bytes long.\r\n", i, pMemoryObjects[i].ptr, pMemoryObjects[i].size)
[364]766#endif
[259]767 _System_DebugOnly_OutputDebugString( temp )
[1]768 End If
769 End If
770 Next
[144]771
772 If isLeak Then
[259]773 _System_DebugOnly_OutputDebugString( Ex"Object dump complete.\r\n" )
[144]774 End If
[170]775
[1]776 End Sub
[144]777
[1]778End Class
779
780'GC管理用の特殊なシステムオブジェクト(デストラクタは最終のタイミングで呼び出されます)
[144]781Dim _System_pGC As *_System_CGarbageCollection
[1]782
783
784
785Function GC_malloc(size As Long) As VoidPtr
786 ' sweep
[368]787 _System_pGC->TrySweep()
[1]788
789 'allocate
[144]790 Return _System_pGC->__malloc(size,0)
[1]791End Function
792
793Function GC_malloc_atomic(size As Long) As VoidPtr
794 ' sweep
[368]795 _System_pGC->TrySweep()
[1]796
797 'allocate
[144]798 Return _System_pGC->__malloc(size,_System_GC_FLAG_ATOMIC)
[1]799End Function
[144]800
801Function _System_GC_malloc_ForObject(size As Long) As VoidPtr
802 ' sweep
[368]803 _System_pGC->TrySweep()
[144]804
805 'allocate
806 Return _System_pGC->__malloc(size,_System_GC_FLAG_OBJECT or _System_GC_FLAG_INITZERO)
807End Function
808
809Function _System_GC_malloc_ForObjectPtr(size As Long) As VoidPtr
810 ' sweep
[368]811 _System_pGC->TrySweep()
[144]812
813 'allocate
814 Return _System_pGC->__malloc(size,_System_GC_FLAG_OBJECT or _System_GC_FLAG_INITZERO or _System_GC_FLAG_NEEDFREE)
815End Function
816
817Function _System_GC_free_for_SweepingDelete( ptr As *Object )
818 ' free
819 _System_pGC->__free_ex( ptr, True )
820End Function
Note: See TracBrowser for help on using the repository browser.