home *** CD-ROM | disk | FTP | other *** search
- /*
- File: PlatfMem.cpp
-
- Contains: Platform layer implementation
-
- Owned by: Michael Burbidge
-
- Copyright: © 1993-95 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <7> 5/4/95 jpa Leave a min amount of free space in
- platform heap. [1235657]
- <6> 10/24/94 jpa Strip address returned from
- PlatformAllocateBlock. [1193659]
- <5> 9/14/94 jpa Eliminated dependencies on rest of OpenDoc.
- [1186692]
- <4> 8/17/94 jpa Zap segments when freed, and check for
- errors [1181509]
- <3> 8/8/94 jpa OD_DEBUG --> ODDebug
- <2> 6/10/94 MB Make it build
- <1> 6/9/94 MB first checked in
- <3> 5/26/94 MB #1162181: Fixed MMM integration bug
- <2> 5/9/94 MB #1162181: Changes necessary to install MMM.
- <1> 4/29/94 MB first checked in
- To Do:
- In Progress:
-
- */
-
- #ifndef _PLATFMEM_
- #include "PlatfMem.h"
- #endif
-
- #ifndef _MEMMGRPV_
- #include "MemMgrPv.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #include <string.h>
- #include <stdio.h>
- #include <stdarg.h>
-
-
- //========================================================================================
- // Global function definitions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // PlatformZapMem
- //----------------------------------------------------------------------------------------
-
- void PlatformZapMem( void *start, size_t size, MMULong value )
- {
- MM_ASSERT(((size_t)start&3)==0); // Must start on a 4byte boundary
- long *blk = (long *) start;
- for (long i = size>>2; i>0; i--) // (i must be signed!)
- *blk++ = value;
- if( size&3 )
- PlatformCopyMemory(&value,blk, size&3); // Fill remaining 1-3 bytes
- }
-
- //----------------------------------------------------------------------------------------
- // PlatformAllocateBlock
- //----------------------------------------------------------------------------------------
-
- void *PlatformAllocateBlock(size_t size, MMHeapLocation src )
- {
- // Check total free space before trying anything; must leave some amount free.
- size_t free;
- MMSystemFreeSpace(src,&free,kMMNULL);
- if( free-size < kPlatformMinFreeSpace )
- return kMMNULL;
-
- Handle handle = (Handle) MMAllocateHandleIn(size,src);
-
- if ( handle ) {
- if( src==kMMAppMemory )
- ::MoveHHi(handle); // Jeff Crawford sez we don't need MoveHHi for temp/sys heap
- ::HLock(handle);
- OSErr err = MemError();
-
- if (err == noErr)
- return StripAddress(*handle);
- else
- DisposeHandle(handle);
- }
-
- return kMMNULL;
- }
-
- //----------------------------------------------------------------------------------------
- // PlatformFreeBlock
- //----------------------------------------------------------------------------------------
-
- void PlatformFreeBlock(void *ptr)
- {
- Handle handle = ::RecoverHandle((Ptr) ptr);
-
- #if MM_DEBUG
- OSErr err = MemError();
- if( err )
- MM_WARN("PlatformFreeBlock got err %hd on ptr %p",err,ptr);
- #endif
-
- if( handle == NULL )
- return;
-
- #if MM_DEBUG
- // Zap the block as we dispose it:
- if( gValidate > 0 )
- PlatformZapMem(ptr,::GetHandleSize(handle),0xDEADBEEF);
- #endif
-
- ::DisposeHandle(handle);
-
- #if MM_DEBUG
- err = MemError();
- if( err && err != memFullErr )
- MM_WARN("PlatformFreeBlock got err %hd on hdl %p",err,handle);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // BREAK
- //----------------------------------------------------------------------------------------
-
- static void
- BREAK( const char msg[] )
- {
- if( gHaveSOM )
- somPrintf("%s\n",msg);
-
- // Now convert to Pascal string and call DebugStr:
- Str255 pstr;
- long len = strlen(msg);
- pstr[0] = (len>255) ?255 :len;
- for( int i=pstr[0]; i>0; i-- ) {
- char c = msg[i-1];
- if( c==';' ) c=':'; // ";" is bad in DebugStrs
- pstr[i] = c;
- }
- DebugStr(pstr);
- }
-
-
- //----------------------------------------------------------------------------------------
- // MM_ASSERT_FAILED
- //----------------------------------------------------------------------------------------
-
- void MM_ASSERT_FAILED(const char *cond, const char *fil)
- {
- char dbg[256];
- sprintf(dbg,"MM: %s ...NOT! In %s", cond,fil);
- BREAK(dbg);
- }
-
- //----------------------------------------------------------------------------------------
- // MM_SHOW_WARNING
- //----------------------------------------------------------------------------------------
-
- void MM_SHOW_WARNING(const char *msg, ...)
- {
- char buf[512];
- strcpy(buf, "MMWarning: ");
- va_list args;
- va_start(args,msg);
- vsprintf(buf+strlen(buf),msg,args);
- va_end(args);
-
- BREAK(buf);
- }
-