home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Memory / PlatfMem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  5.1 KB  |  186 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PlatfMem.cpp
  3.  
  4.     Contains:    Platform layer implementation
  5.  
  6.     Owned by:    Michael Burbidge
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.          <6>     9/19/96    DH        Task: low memory changes,1377922,1377888.
  13.                                     Moved "fudge" for block allocation.
  14.          <5>     8/13/96    RA        1376299: ES crashes in 24-bit mode. We
  15.                                     again call StripAddress in
  16.                                     PlatformAllocateBlock
  17.          <4>     7/31/96    CSL        Fix to previous checking--needed to include
  18.                                     MemDebg.h
  19.          <3>     7/31/96    DH        #1372007: Store handle inside blocks
  20.                                     allocated to provide a more reliable way to
  21.                                     recover it when freeing.
  22.          <7>      5/4/95    jpa        Leave a min amount of free space in
  23.                                     platform heap. [1235657]
  24.          <6>    10/24/94    jpa        Strip address returned from
  25.                                     PlatformAllocateBlock. [1193659]
  26.          <5>     9/14/94    jpa        Eliminated dependencies on rest of OpenDoc.
  27.                                     [1186692]
  28.          <4>     8/17/94    jpa        Zap segments when freed, and check for
  29.                                     errors [1181509]
  30.          <3>      8/8/94    jpa        OD_DEBUG --> ODDebug
  31.          <2>     6/10/94    MB        Make it build
  32.          <1>      6/9/94    MB        first checked in
  33.          <3>     5/26/94    MB        #1162181: Fixed MMM integration bug
  34.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  35.          <1>     4/29/94    MB        first checked in
  36.     To Do:
  37.     In Progress:
  38. */
  39.  
  40. #ifndef _PLATFMEM_
  41. #include "PlatfMem.h"
  42. #endif
  43.  
  44. #ifndef _MEMMGRPV_
  45. #include "MemMgrPv.h"
  46. #endif
  47.  
  48. #ifndef _MEMDEBG_
  49. #include "MemDebg.h"
  50. #endif
  51.  
  52. #ifndef __ERRORS__
  53. #include <Errors.h>
  54. #endif
  55.  
  56. #include <string.h>
  57. #include <stdio.h>
  58. #include <stdarg.h>
  59.  
  60.  
  61. //========================================================================================
  62. // Global function definitions
  63. //========================================================================================
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // PlatformZapMem
  67. //----------------------------------------------------------------------------------------
  68.  
  69. void PlatformZapMem( void *start, size_t size, MMULong value )
  70. {
  71.     MM_ASSERT(((size_t)start&3)==0);                // Must start on a 4byte boundary
  72.     long *blk = (long *) start;
  73.     for (long i = size>>2; i>0; i--)                     // (i must be signed!)
  74.         *blk++ = value;
  75.     if( size&3 )
  76.         PlatformCopyMemory(&value,blk, size&3);        // Fill remaining 1-3 bytes
  77. }
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // PlatformAllocateBlock
  81. //----------------------------------------------------------------------------------------
  82.  
  83. void *PlatformAllocateBlock(size_t size, MMHeapLocation src )
  84. {
  85.     size += sizeof(Handle);
  86.     
  87.     // Check total free space before trying anything; must leave some amount free.
  88.     size_t free;
  89.         
  90.     Handle handle = (Handle) MMAllocateHandleIn(size,src);
  91.             
  92.     if ( handle ) {
  93.         if( src==kMMAppMemory )
  94.             ::MoveHHi(handle);    // Jeff Crawford sez we don't need MoveHHi for temp/sys heap
  95.         ::HLock(handle);
  96.         OSErr err = MemError();
  97.         
  98.         if (err == noErr) {
  99.             Handle* start = (Handle*) StripAddress(*handle);
  100.             *(start++) = handle;        // Store handle in 1st 4 bytes
  101.             return start;                // And return ptr to just past it
  102.         } else
  103.             DisposeHandle(handle);
  104.     }
  105.     
  106.     return kMMNULL;
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // PlatformFreeBlock
  111. //----------------------------------------------------------------------------------------
  112.  
  113. void PlatformFreeBlock(void *ptr)
  114. {
  115.     Handle handle = ((Handle*)ptr)[-1];    // Handle was stored just before block
  116.  
  117. #if MM_DEBUG
  118.     if( !MMValidateHandle(handle) )
  119.         return;
  120.     if( StripAddress(*handle) != StripAddress(ptr)-4 ) {
  121.         MM_WARN("PlatformFreeBlock: Hdl %p doesn't point to %p!",handle,ptr);
  122.         return;
  123.     }
  124.     // Zap the block as we dispose it:
  125.     PlatformZapMem(*handle,::GetHandleSize(handle),0xDEADBEEF);
  126. #endif
  127.  
  128.     ::DisposeHandle(handle);
  129.     
  130. #if MM_DEBUG
  131.     OSErr err = MemError();
  132.     if( err && err != memFullErr )
  133.         MM_WARN("PlatformFreeBlock got err %hd on hdl %p",err,handle);
  134. #endif
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // BREAK
  139. //----------------------------------------------------------------------------------------
  140.  
  141. static void
  142. BREAK( const char msg[] )
  143. {
  144.     if( gHaveSOM )
  145.         somPrintf("%s\n",msg);
  146.     
  147.     // Now convert to Pascal string and call DebugStr:
  148.     Str255 pstr;
  149.     long len = strlen(msg);
  150.     pstr[0] = (len>255) ?255 :len;
  151.     for( int i=pstr[0]; i>0; i-- ) {
  152.         char c = msg[i-1];
  153.         if( c==';' ) c=':';        // ";" is bad in DebugStrs
  154.         pstr[i] = c;
  155.     }
  156.     DebugStr(pstr);
  157. }
  158.  
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // MM_ASSERT_FAILED
  162. //----------------------------------------------------------------------------------------
  163.  
  164. void MM_ASSERT_FAILED(const char *cond, const char *fil)
  165. {
  166.     char dbg[256];
  167.     sprintf(dbg,"MM: %s ...NOT! In %s", cond,fil); 
  168.     BREAK(dbg);
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // MM_SHOW_WARNING
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void MM_SHOW_WARNING(const char *msg, ...)
  176. {
  177.     char buf[512];
  178.     strcpy(buf, "MMWarning: ");
  179.     va_list args;
  180.     va_start(args,msg);
  181.     vsprintf(buf+strlen(buf),msg,args);
  182.     va_end(args);
  183.     
  184.     BREAK(buf);
  185. }
  186.