home *** CD-ROM | disk | FTP | other *** search
- /*
- File: ODMemory.cpp
-
- Contains: Procedural implementation to the Memory component
-
- Owned by: Jens Alfke
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <3> 6/19/96 jpa Fixed header comment
- <2> 6/19/96 jpa 1357406: Added COverridingMacOSMemory.
-
- In Progress:
-
- */
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h"
- #endif
-
- #ifndef _MEMMGR_
- #include "MemMgr.h"
- #endif
-
- #ifndef __CODEFRAGMENTS__
- #include <CodeFragments.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- #ifndef _UTILERRS_
- #include "UtilErrs.h"
- #endif
-
- //========================================================================================
- // Constants
- //========================================================================================
-
- const size_t kSystemHeapInitialSize = 20 * 1024;
- const size_t kSystemHeapGrowBySize = 32 * 1024;
-
- const ODSize kMinAppHeapSpace = 16 * 1024;
- const ODSize kMinContigAppHeapSpace = 6 * 1024;
-
- //========================================================================================
- // Global variables
- //========================================================================================
-
- //static ODMemoryHeapID gDefaultHeap; // ----- [HLX] Changed -----
- #if 0
- #pragma lib_export on // Need to tell compiler that gSystemHeap is imported
- extern void* gSystemHeap; // Lives in Shared Globals library, MemShard.cpp.
- #pragma lib_export off
- #endif /* 0 */
- //========================================================================================
- // Initialization
- //========================================================================================
-
-
- OSErr InitODMemory( )
- {
- // ----- [HLX] Changed Begin -----
- // gDefaultHeap = MMGetDefaultHeap();
- // return gDefaultHeap ?noErr :memFullErr;
- return noErr;
- // ----- [HLX] Changed End -----
- }
-
- // Note: CFMInit stuff is now in UtilInit.cpp
-
- //extern "C" pascal OSErr UtilitiesCFMInit( CFragInitBlockPtr );
-
- // For use inside OpenDoc only. Parts won't need to use this but
- // should call InitODMemory from their own CFMInit routines.
- //pascal OSErr UtilitiesCFMInit (CFragInitBlockPtr /*initBlkPtr*/)
- //{
- // return InitODMemory();
- //}
-
-
- //========================================================================================
- // Function declarations for operations on pointer based blocks
- //========================================================================================
-
-
- //----------------------------------------------------------------------------------------
- // ODGetDefaultHeap
- //----------------------------------------------------------------------------------------
-
- ODMemoryHeapID ODGetDefaultHeap()
- {
- // ----- [HLX] Changed Begin -----
- // Tweaked by JEL on 10/31/95
- // Don't use FW_CMemoryTaskGlobals:
- // 1) It's a circular dependency
- // 2) We may remove soon remove FW_CMemoryTaskGlobals entirely
- // We may want to restore the global gDefaultHeap and use it here.
- return ::MMGetDefaultHeap();
- // ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
- // return gDefaultHeap;
- // ----- [HLX] Changed End -----
- }
- #if 0
- //----------------------------------------------------------------------------------------
- // ODGetSystemHeap
- //----------------------------------------------------------------------------------------
-
- ODMemoryHeapID ODGetSystemHeap()
- {
- // The system heap should be created only by the OD system process.
-
- if( !gSystemHeap ) {
- gSystemHeap = ODCreateHeap( kSystemHeapInitialSize,
- kSystemHeapGrowBySize,
- kODTrue,"OpenDoc System Heap" );
- THROW_IF_NULL(gSystemHeap);
- }
- return (ODMemoryHeapID)gSystemHeap;
- }
- #endif /* 0 */
- //----------------------------------------------------------------------------------------
- // ODCreateHeap
- //----------------------------------------------------------------------------------------
-
- ODMemoryHeapID ODCreateHeap(unsigned long sizeInitial, unsigned long sizeIncrement,
- Boolean fromSysMemory, const char *name)
- {
- MemHeap *heap = MMNewHeap(fromSysMemory ?kMMSysMemory :kMMTempMemory,
- sizeInitial, sizeIncrement, name);
- if( !heap )
- THROW(kODErrOutOfMemory);
- return heap;
- }
-
- //----------------------------------------------------------------------------------------
- // ODDestroyHeap
- //----------------------------------------------------------------------------------------
-
- void ODDestroyHeap(ODMemoryHeapID heapID)
- {
- MMDisposeHeap(heapID);
- }
-
- //----------------------------------------------------------------------------------------
- // ODNewPtr
- //----------------------------------------------------------------------------------------
-
- void *ODNewPtr(ODBlockSize blkSize, ODMemoryHeapID heapID)
- {
- if( heapID == kODNULL ) {
- // ----- [HLX] Changed Begin -----
- // ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
- // heapID = gDefaultHeap;
- heapID = ODGetDefaultHeap();
- // ----- [HLX] Changed End -----
- }
- void *block = MMAllocateIn(blkSize,heapID);
- if( !block )
- THROW(kODErrOutOfMemory);
- return block;
- }
-
- //----------------------------------------------------------------------------------------
- // ODNewPtrClear
- //----------------------------------------------------------------------------------------
-
- void *ODNewPtrClear(ODBlockSize blkSize, ODMemoryHeapID heapID)
- {
- if( heapID == kODNULL ) {
- // ----- [HLX] Changed Begin -----
- // ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
- // heapID = gDefaultHeap;
- heapID = ODGetDefaultHeap();
- // ----- [HLX] Changed End -----
- }
- void *block = MMAllocateClearIn(blkSize,heapID);
- if( !block )
- THROW(kODErrOutOfMemory);
- return block;
- }
-
- //----------------------------------------------------------------------------------------
- // ODReallocate
- //----------------------------------------------------------------------------------------
-
- void *ODReallocate(void *block, ODBlockSize newSize)
- {
- block = MMReallocate(block,newSize);
- if( !block && newSize>0 )
- THROW(kODErrOutOfMemory);
- return block;
- }
-
- //------------------------------------------------------------------------------
- // ODDisposePtr
- //------------------------------------------------------------------------------
-
- void ODDisposePtr(void *pointer)
- {
- MMFree(pointer);
- }
-
- //----------------------------------------------------------------------------------------
- // ODRecoverHeapID
- //----------------------------------------------------------------------------------------
-
- ODMemoryHeapID ODRecoverHeapID(const void *block)
- {
- ODMemoryHeapID heap = MMGetHeap(block);
- if( !heap )
- THROW(kODErrInvalidBlock);
- return heap;
- }
-
-
-
- //----------------------------------------------------------------------------------------
- // ODBlockIsObject
- //----------------------------------------------------------------------------------------
-
- void ODBlockIsObject( void *block, ODBoolean isObject )
- {
- MMSetIsObject(block,isObject);
- }
-
-
- //----------------------------------------------------------------------------------------
- // ODIsBlockAnObject
- //----------------------------------------------------------------------------------------
-
- ODBoolean ODIsBlockAnObject( const void *block )
- {
- return MMIsObject(block);
- }
-
-
- //========================================================================================
- // Function declarations for operations on handle based blocks
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // ODNewHandle
- //----------------------------------------------------------------------------------------
-
- ODHandle ODNewHandle(ODULong howBig)
- {
- ODHandle h = (ODHandle) MMAllocateHandleIn(howBig,kMMTempMemory);
- if( !h )
- THROW(kODErrOutOfMemory);
- return h;
-
- }
-
- //----------------------------------------------------------------------------------------
- // ODDisposeHandle
- //----------------------------------------------------------------------------------------
-
- void ODDisposeHandle(ODHandle handle)
- {
- MMFreeHandle((MMHandle)handle);
- }
-
- //----------------------------------------------------------------------------------------
- // ODCopyHandle
- //----------------------------------------------------------------------------------------
-
- ODHandle ODCopyHandle(ODHandle handle)
- {
- ODHandle h = (ODHandle) MMCopyHandle((MMHandle)handle);
- if( !h )
- THROW(kODErrOutOfMemory);
- return h;
- }
-
- //----------------------------------------------------------------------------------------
- // ODGetHandleSize(ODHandle handle)
- //----------------------------------------------------------------------------------------
-
- ODULong ODGetHandleSize(ODHandle handle)
- {
- return MMGetHandleSize((MMHandle)handle);
- }
-
- //----------------------------------------------------------------------------------------
- // ODSetHandleSize(ODHandle handle, ODULong blkSize)
- //----------------------------------------------------------------------------------------
-
- void ODSetHandleSize(ODHandle handle, ODULong blkSize)
- {
- if( !MMSetHandleSize((MMHandle)handle,blkSize) )
- THROW(kODErrOutOfMemory);
-
- }
-
- //----------------------------------------------------------------------------------------
- // ODLockHandle(ODHandle handle)
- //----------------------------------------------------------------------------------------
-
- void* ODLockHandle(ODHandle handle)
- {
- void *p = MMLockHandle((MMHandle)handle);
- if( !p )
- THROW(kODErrOutOfMemory);
- return p;
- }
-
- //----------------------------------------------------------------------------------------
- // ODUnlockPtr(void* ptr)
- //----------------------------------------------------------------------------------------
-
- void ODUnlockPtr(void* ptr)
- {
- MMUnlockPtr(ptr);
- }
-
- //----------------------------------------------------------------------------------------
- // ODUnlockHandle(ODHandle handle)
- //----------------------------------------------------------------------------------------
-
- void ODUnlockHandle(ODHandle handle)
- {
- MMUnlockHandle((MMHandle)handle);
- }
-
-
- //========================================================================================
- // Function declarations utility functions
- //========================================================================================
-
- //------------------------------------------------------------------------------
- // ODBlockMove
- //------------------------------------------------------------------------------
-
-
- void ODBlockMove( const void *from, void *to, ODULong size)
- {
- MMMove(to,from,size);
- }
-
-
- //------------------------------------------------------------------------------
- // ODNewRgn
- //------------------------------------------------------------------------------
-
- RgnHandle ODNewRgn( )
- {
- RgnHandle r = (RgnHandle) ODNewHandle(sizeof(Region));
- (**r).rgnSize = sizeof(Region);
- SetRect(&(**r).rgnBBox, 0,0,0,0);
- return r;
- }
-
- //------------------------------------------------------------------------------
- // ODRequireFreeSpace
- //------------------------------------------------------------------------------
-
-
- ODBoolean
- ODHaveFreeSpace( ODSize haveTotal, ODSize haveContig /*=0*/,
- ODBoolean appHeap /*=false*/ )
- {
- size_t total, contig;
- if( appHeap )
- MMSystemFreeSpace(kMMAppMemory, &total,&contig);
- else
- MMGetFreeSpace(ODGetDefaultHeap(),&total,&contig ); // [HLX] changed
- return total>=haveTotal && contig>=haveContig;
- }
-
- void
- ODRequireFreeSpace( ODSize total, ODSize contig, ODBoolean appHeap )
- {
- if( !ODHaveFreeSpace(total,contig,appHeap) )
- THROW(appHeap ?memFullErr :kODErrOutOfMemory);
-
- // I decided it made sense to throw a Mac mem mgr error if the
- // app heap is full, since this is the error you'd get if you
- // tried to d
- }
-
- void
- ODCheckAppHeapSpace( )
- {
- if( !ODHaveFreeSpace(kMinAppHeapSpace,kMinContigAppHeapSpace,kODTrue) )
- THROW(memFullErr);
- }
-
- //========================================================================================
- // MacOS NewHandle override
- //========================================================================================
-
- COverridingMacOSMemory::COverridingMacOSMemory( )
- {
- MMOverridePlatform(kODTrue,kODTrue);
- }
-
-
- COverridingMacOSMemory::~COverridingMacOSMemory( )
- {
- MMEndOverridePlatform(kODTrue,kODTrue);
- }
-