home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Memory / MemDebgM.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.5 KB  |  154 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemDebgM.cpp
  3.  
  4.     Contains:    Mac-specific memory management debug routines
  5.  
  6.     Owned by:    Jens Alfke, Vincent Lo, Nick Pilch, Michael Burbidge
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <5>      5/4/95    jpa        Abstracted out highest/lowest possible
  13.                                     address stuff [1246077]
  14.          <4>     1/12/95    jpa        Include LowMem.h [1210936]
  15.          <3>    10/24/94    jpa        Call StripAddress in BasicValidatePtr.
  16.                                     [1193659]
  17.          <2>     9/29/94    RA        1189812: Mods for 68K build.
  18.          <1>     9/14/94    jpa        first checked in
  19.     
  20.     In Progress:
  21. */
  22.  
  23.  
  24. #ifndef _MEMCNFIG_
  25. #include "MemCnfig.h"
  26. #endif
  27.  
  28.  
  29. #if MM_DEBUG
  30.  
  31.  
  32. #ifndef _MEMDEBG_
  33. #include "MemDebg.h"
  34. #endif
  35.  
  36. #ifndef _MEMMGRPV_
  37. #include "MemMgrPv.h"
  38. #endif
  39.  
  40. #ifndef _MEMHOOKS_
  41. #include "MemHooks.h"
  42. #endif
  43.  
  44. #ifndef __MEMORY__
  45. #include <Memory.h>        // Mac memory manager
  46. #endif
  47.  
  48. #ifndef __LOWMEM__
  49. #include <LowMem.h>
  50. #endif
  51.  
  52.  
  53. const size_t kMaxHandleBlockSize = 0x7FFFFFFF;
  54.  
  55.  
  56. //------------------------------------------------------------------------------
  57. // PtrInHeap
  58. //------------------------------------------------------------------------------
  59.  
  60. inline static MMBoolean
  61. PtrInHeap( const void *p, THz heap )
  62. {
  63.     return (p >= &heap->heapData) && (p < heap->bkLim);
  64. }
  65.  
  66.  
  67. //------------------------------------------------------------------------------
  68. // BasicValidatePtr
  69. //------------------------------------------------------------------------------
  70.  
  71. const char*
  72. BasicValidatePtr( const void *p, MMBoolean mustBeInHeap )
  73. {
  74.     // If we could be sure that p were in the app zone, we could be more
  75.     // stringent. But it might be in the System heap or in temp-mem.
  76.     
  77. #if 0
  78.     static THz tempZone = kMMNULL;
  79.     if (tempZone == kMMNULL)
  80.     {
  81.         // Find the temp zone by allocating a handle in it and then getting its zone:
  82.         OSErr err;
  83.         
  84.         Handle handle = ::TempNewHandle(2, &err);
  85.         MM_ASSERT(err == noErr && handle != kMMNULL);
  86.         
  87.         tempZone = ::HandleZone(handle);
  88.         MM_ASSERT(tempZone != kMMNULL);
  89.         
  90.         ::DisposeHandle(handle);
  91.     }
  92. #endif
  93.  
  94.     p = StripAddress((void*)p);
  95.     
  96.     if( p == kMMNULL )
  97.         return "is NULL";
  98. //    else if( (MMULong)p & 1 )
  99. //        return "is odd";
  100.     else if( (MMULong)p & 2 )
  101.         return "isn't 4-byte-aligned";
  102. #if 0
  103.     // This does not appear to work any longer with the Modern Memory Manager...
  104.     else if( mustBeInHeap && !PtrInHeap(p,ApplicationZone())
  105.                             && !PtrInHeap(p,SystemZone())
  106.                               && !PtrInHeap(p,tempZone) )
  107.         return "is not in a known Mac heap zone";
  108. #endif
  109.     else if( /*!mustBeInHeap &&*/ (p<LOWEST_POSSIBLE_ADDRESS() || p>HIGHEST_POSSIBLE_ADDRESS()) )
  110.         return "is outside the known universe";
  111.     else
  112.         return kMMNULL;
  113. }
  114.  
  115.  
  116. //------------------------------------------------------------------------------
  117. // MMValidateHandle
  118. //------------------------------------------------------------------------------
  119.  
  120. MMBoolean
  121. MMValidateHandle( MMHandle MMHandle )
  122. {
  123.     Handle h = (Handle) MMHandle;
  124.     
  125.     // Check handle itself as a pointer:
  126.     const char *err = BasicValidatePtr(h);
  127.     
  128.     if( err == kMMNULL && *h == kMMNULL )
  129.         err = "is purged";
  130.     
  131.     if( err ) {
  132.         MM_WARN("Handle %p %s!",h,err);
  133.         return kMMFalse;
  134.     }
  135.     
  136.     // Check master pointer for validity:
  137.     err = BasicValidatePtr(*h);
  138.     if( err ) {
  139.         MM_WARN("Handle %p's master %p %s!",h,*h,err);
  140.         return kMMFalse;
  141.     }
  142.     
  143.     // Check handle's size:
  144.     size_t size = GetHandleSize(h);
  145.     if( size > kMaxHandleBlockSize )
  146.         MM_WARN("Handle %p has bogus size %08lx!",h,size);
  147.     else if( MemError() != noErr )
  148.         MM_WARN("Handle %p caused err %d on GetHandleSize",h,MemError());
  149.     else
  150.         return kMMTrue;
  151.     return kMMFalse;
  152. }
  153.  
  154. #endif /*MM_DEBUG*/