Changeset 399 for trunk/Include


Ignore:
Timestamp:
Jan 20, 2008, 3:31:02 PM (16 years ago)
Author:
イグトランス (egtra)
Message:

alloc, reallocに失敗したとき、例外を投げるようにした

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Include/system/gc.sbp

    r373 r399  
    290290
    291291        ' 実際のメモリバッファはインデックスの分だけ多めに確保する
    292         Dim ptr = HeapAlloc( hHeap, dwFlags, size + SizeOf( LONG_PTR ) ) + SizeOf( LONG_PTR )
     292        __malloc = HeapAlloc( hHeap, dwFlags, size + SizeOf( LONG_PTR ) )
     293        throwIfAllocationFailed( __malloc, size )
     294        __malloc += SizeOf( LONG_PTR )
    293295
    294296        ' 管理対象のメモリオブジェクトとして追加
    295         add( ptr, size, flags )
    296 
    297         Return ptr
     297        add( __malloc, size, flags )
    298298    End Function
    299299
     
    315315
    316316            pTempMemoryObject->size = size
    317             pTempMemoryObject->ptr = HeapReAlloc( hHeap, HEAP_ZERO_MEMORY, pTempMemoryObject->ptr - SizeOf(LONG_PTR), size + SizeOf(LONG_PTR) ) + SizeOf(LONG_PTR)
     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
     324
    318325
    319326            If minPtr > pTempMemoryObject->ptr As ULONG_PTR Then
     
    323330                maxPtr = ( pTempMemoryObject->ptr + size ) As ULONG_PTR
    324331            End If
    325 
    326332        LeaveCriticalSection(CriticalSection)
    327         Return pTempMemoryObject->ptr
    328333    End Function
     334
     335    /*!
     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
     358            Throw New System.OutOfMemoryException(ActiveBasic.Strings.SPrintf("malloc: Failed to allocate %zu byte(s) memory.", New System.UInt64(size)))
     359        End If
     360    End Sub
    329361
    330362    /*!
Note: See TracChangeset for help on using the changeset viewer.