home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 7.4 KB | 248 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWMemHlp.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWODEXCE_H
- #include "FWODExce.h"
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemory
- #endif
-
- //========================================================================================
- // class FW_CAcquireLockedSystemHandle
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CAcquireLockedSystemHandle)
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle)
- : fLockedSystemHandle(aSystemHandle)
- {
- #ifdef FW_BUILD_MAC
- fLockState = ::HGetState(aSystemHandle);
- #endif
- fLockedPointer = FW_CMemoryManager::LockSystemHandle(aSystemHandle);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle()
- {
- FW_START_DESTRUCTOR
- #ifdef FW_BUILD_MAC
- ::HSetState(fLockedSystemHandle, fLockState);
- #else
- FW_CMemoryManager::UnlockSystemHandle(fLockedSystemHandle);
- #endif
- }
-
- //========================================================================================
- // CLASS FW_CAcquireTemporarySystemHandle
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CAcquireTemporarySystemHandle)
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle(unsigned long size) :
- fTemporarySystemHandle(FW_CMemoryManager::AllocateSystemHandle(size))
- {
- FW_TRY
- {
- fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
- fFree = TRUE;
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::Orphan
- //----------------------------------------------------------------------------------------
-
- FW_PlatformHandle FW_CAcquireTemporarySystemHandle::Orphan()
- {
- FW_ASSERT(fFree == TRUE);
- fFree = FALSE;
-
- FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
- fMemoryPointer = NULL;
-
- return fTemporarySystemHandle;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle(void)
- {
- FW_START_DESTRUCTOR
-
- if(fFree)
- {
- FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
- FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporarySystemHandle::Resize
- //----------------------------------------------------------------------------------------
-
- void* FW_CAcquireTemporarySystemHandle::Resize(unsigned long newSize)
- {
- // unlock the handle before resizing
- FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
-
- // then resize it
- FW_TRY
- {
- fTemporarySystemHandle = FW_CMemoryManager::ResizeSystemHandle(fTemporarySystemHandle, newSize);
- fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- // relock the handle
- fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- return GetPointer();
- }
-
- //========================================================================================
- // CLASS FW_CAcquireTemporaryMemory
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CAcquireTemporaryMemory)
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporaryMemory::FW_CAcquireTemporaryMemory
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireTemporaryMemory::FW_CAcquireTemporaryMemory(unsigned long size)
- {
- fPointer = FW_CMemoryManager::AllocateBlock(size);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporaryMemory::~FW_CAcquireTemporaryMemory
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireTemporaryMemory::~FW_CAcquireTemporaryMemory()
- {
- FW_START_DESTRUCTOR
- FW_CMemoryManager::FreeBlock(fPointer);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireTemporaryMemory::OrphanPointer
- //----------------------------------------------------------------------------------------
-
- void* FW_CAcquireTemporaryMemory::OrphanPointer()
- {
- FW_ASSERT(fPointer != NULL); // Only orphan once
- void* t = fPointer;
- fPointer = NULL;
- return t;
- }
-
- #ifdef FW_BUILD_MAC
- //========================================================================================
- // CLASS FW_CMacAcquireMultiFinderHeapZone
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CMacAcquireMultiFinderHeapZone)
-
- //----------------------------------------------------------------------------------------
- // FW_CMacAcquireMultiFinderHeapZone::FW_CMacAcquireMultiFinderHeapZone
- //----------------------------------------------------------------------------------------
-
- FW_CMacAcquireMultiFinderHeapZone::FW_CMacAcquireMultiFinderHeapZone() :
- fOldHeap(NULL)
- {
- OSErr heapError;
- Handle heapTestHandle = ::TempNewHandle( 1, &heapError );
-
- if (heapTestHandle != 0)
- {
- THz tempHeap = ::HandleZone( heapTestHandle );
- heapError = ::MemError( );
-
- ::DisposeHandle( heapTestHandle );
- if (heapError == noErr)
- {
- if (tempHeap != 0 ) {
- fOldHeap = ::GetZone( );
- ::SetZone( tempHeap );
- }
- }
- else
- {
- FW_Failure(heapError);
- }
- }
- else
- {
- FW_Failure(FW_xMemoryExhausted);
- }
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMacAcquireMultiFinderHeapZone::~FW_CMacAcquireMultiFinderHeapZone
- //----------------------------------------------------------------------------------------
-
- FW_CMacAcquireMultiFinderHeapZone::~FW_CMacAcquireMultiFinderHeapZone()
- {
- FW_START_DESTRUCTOR
-
- if ( fOldHeap != NULL )
- ::SetZone( fOldHeap );
- }
- #endif
-
-
-