home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Memory / MemoryHe.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  9.9 KB  |  321 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemoryHe.h
  3.  
  4.     Contains:    MemoryHeap class interface
  5.  
  6.     Owned by:    Michael Burbidge, Jens Alfke
  7.     Owned by:    Jens Alfke
  8.  
  9.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     
  13.         <11>    10/24/95    jpa        1293441: Added slush-fund.
  14.         <10>      8/4/95    DM        Leak checking [1267956]
  15.          <9>      5/4/95    jpa        Support for finding largest free block
  16.                                     [1235657] and validating memory ranges
  17.                                     [1246077]
  18.          <8>    10/24/94    jpa        Constness [1194286].
  19.          <7>     9/29/94    RA        1189812: Mods for 68K build.
  20.          <6>     9/14/94    jpa        Eliminated dependencies on rest of OpenDoc.
  21.                                     Added ability to get heap of a block.
  22.                                     [1186692]
  23.          <5>     8/17/94    jpa        Added support for walking heaps [1179567].
  24.          <4>      8/8/94    jpa        Added oldBlk param to DidRealloc hook
  25.                                     [1179567]
  26.          <3>     6/28/94    jpa        Added no-op new/delete to MemoryHookList to
  27.                                     avoid refs to ODDisposePtr.
  28.          <2>     6/13/94    MB        Some more initial fixes
  29.          <2>     6/10/94    MB        Make it build
  30.          <1>      6/9/94    MB        first checked in
  31.          <3>     5/26/94    MB        #1162181: Fixed MMM integration bug
  32.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  33.          <1>     4/29/94    MB        first checked in
  34.     To Do:
  35.     In Progress:
  36.         
  37. */
  38.  
  39. #ifndef _MEMORYHE_
  40. #define _MEMORYHE_
  41.  
  42. #ifndef _PLATFMEM_
  43. #include "PlatfMem.h"
  44. #endif
  45.  
  46. #ifndef __STDDEF__
  47. #include <stddef.h>
  48. #endif
  49.  
  50. #ifndef __TYPES__
  51. #include <Types.h>
  52. #endif
  53.  
  54.  
  55. //========================================================================================
  56. // Forward class declarations
  57. //========================================================================================
  58.  
  59. class MemoryHookList;
  60.  
  61.  
  62. //========================================================================================
  63. // Type definitions
  64. //========================================================================================
  65.  
  66. typedef unsigned long ODBytePtr;
  67. typedef unsigned long ODBlockSize;
  68.  
  69. #if MM_DEBUG
  70. //========================================================================================
  71. // ODMemoryHook (MM_DEBUG only)
  72. //
  73. //        A Memory hook that can be used to track heap operations by subclassing ODMemoryHook
  74. //        and overriding the methods of interest. Your subclass is then registered with a
  75. //        MemoryHeap.
  76. //
  77. //========================================================================================
  78.  
  79. class ODMemoryHook
  80. {
  81. public:
  82.     // These methods should all be abstract, but MemoryHookList instantiates a
  83.     // ODMemoryHook to use as the head of its linked list, so here we just use empty
  84.     // inlines. A ODMemoryHook still cannot be instantiated because the constructor
  85.     // is protected.
  86.  
  87.     virtual ODBlockSize GetHeaderSize();
  88.     
  89.     virtual ODBlockSize AboutToAllocate(ODBlockSize size) const;
  90.     virtual void* DidAllocate(void* blk, ODBlockSize);
  91.     virtual const void* AboutToBlockSize(const void* blk);
  92.     virtual void* AboutToFree(void* blk);
  93.     virtual void AboutToRealloc(void*& , ODBlockSize&);
  94.     virtual void* DidRealloc(void* oldBlk, void* newBlk, ODBlockSize);
  95.     virtual void AboutToReset();
  96.     virtual void Comment(const char*);
  97.         
  98.     virtual long GetType( ) const;            // Returns type of hook
  99.     
  100.     enum{ kNoType = 0 };
  101.  
  102.     virtual ~ODMemoryHook();
  103.  
  104.     void* operator new(SIZE_T size, MMHeapLocation = kMMTempMemory);
  105.     void operator delete(void* ptr);
  106.  
  107. protected:
  108.  
  109.     ODMemoryHook();
  110.  
  111. private:
  112.     ODMemoryHook* fNextHook, * fPreviousHook;
  113.  
  114.     friend MemoryHookList;
  115.     
  116.     ODMemoryHook(const ODMemoryHook& blk);
  117.     ODMemoryHook& operator=(const ODMemoryHook& blk);
  118.         // This class shouldn't be copied.
  119. };
  120. #endif
  121.  
  122.  
  123. #if MM_DEBUG
  124. //========================================================================================
  125. // MemoryHookList (MM_DEBUG only)
  126. //
  127. //        A list of memory hooks. This is a special linked list that assumes the links for
  128. //        the list are fields of ODMemoryHook. This avoids endless recursion were we to
  129. //        allocate nodes for the MemoryHooks here.
  130. //
  131. //========================================================================================
  132.  
  133. class MemoryHookList
  134. {
  135. public:
  136.     MemoryHookList();
  137.  
  138.     void Add(ODMemoryHook* aMemoryHook);
  139.     void Remove(ODMemoryHook* aMemoryHook);
  140.     ODMemoryHook* First()                        const;
  141.     ODMemoryHook* After( ODMemoryHook* )        const;
  142.     ODMemoryHook* Before( ODMemoryHook* )        const;
  143.     ODMemoryHook* Last()                        const;
  144.  
  145.     ~MemoryHookList();
  146.  
  147. private:
  148.     ODMemoryHook fHead;
  149.     
  150.     MemoryHookList(const MemoryHookList& blk);
  151.     MemoryHookList& operator=(const MemoryHookList& blk);
  152.         // This class shouldn't be copied.
  153.         
  154.     void* operator new( size_t )    {return 0;}    // Will never be allocated from the heap
  155.     void  operator delete( void* )    { }
  156. };
  157. #endif
  158.  
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // HeapWalkProc (proc-ptr type)
  162. //----------------------------------------------------------------------------------------
  163.  
  164. typedef Boolean (*HeapWalkProc)( const void *blk, unsigned long size, Boolean isObject,
  165.                                  void *refCon );
  166.  
  167.  
  168. class StackCrawl;        // For GetBlockStackCrawl
  169.     
  170.  
  171. //========================================================================================
  172. // MemoryHeap
  173. //
  174. //        Abstract base class for memory heaps.
  175. //
  176. //========================================================================================
  177.  
  178. class MemoryHeap
  179. {
  180. public:
  181.     enum { kBlockTypeId = 0 };
  182.     enum { kMagicNumber = 0x8765FEDC };
  183.  
  184.     // Keep a list of all heaps created. Very useful for debugging.
  185.     
  186.     static MemoryHeap* fHeapList;
  187.     static MemoryHeap* GetFirstHeap();
  188.  
  189.     void* Allocate(ODBlockSize size);
  190.     ODBlockSize BlockSize(const void* block) const;
  191.     virtual unsigned long BytesAllocated() const;
  192.     virtual unsigned long BytesFree() const = 0;
  193.     virtual unsigned long LargestFreeBlock() const;
  194.     
  195.     void Free(void*);
  196.     virtual MemoryHeap* GetNextHeap() const;
  197.     virtual Boolean GetZapOnAllocate() const;
  198.     virtual Boolean GetZapOnFree() const;
  199.     virtual unsigned long HeapSize() const = 0;
  200.     virtual unsigned long NumberAllocatedBlocks() const;
  201.     void Reset();
  202.     void* Reallocate(void* block, ODBlockSize newSize);
  203.     virtual void SetZapOnAllocate(Boolean = false);
  204.     virtual void SetZapOnFree(Boolean = false);
  205.  
  206.     inline MMHeapLocation GetLocation( )        {return fMemSource;}
  207.  
  208.     void* operator new(SIZE_T size, MMHeapLocation =kMMTempMemory);
  209.     void operator delete(void* ptr);
  210.  
  211.     virtual ~MemoryHeap();
  212.     
  213.     static const char* kDefaultDescription;
  214.     static const char* kDeadHeapDescription;
  215.     virtual const char* GetDescription() const;
  216.     virtual void SetDescription(const char* description = kDefaultDescription);
  217.  
  218.     // Access to the isObject flag of a block. The intended use is that 'operator new'
  219.     // will set the flag for all SOM objects, giving us a way to tell whether any block
  220.     // is an object. This can help a lot in debugging.    --jpa
  221.     void SetBlockIsObject( void* blk, Boolean isObject );
  222.     Boolean BlockIsObject( const void* blk ) const;
  223.  
  224.     MemoryHeap* GetBlockHeap( const void* ) const;    // Should be static but can't be made so
  225.  
  226.     MMBoolean    AllocateSlushFund( size_t size, size_t allocSizeLimit );
  227.     size_t        GetSlushFundSize( )                {return fSlushFundSize;}
  228.     size_t        FreeSlushFund( );                // returns size freed
  229.  
  230. #if MM_DEBUG
  231.     virtual void AdoptHook(ODMemoryHook* ODMemoryHook);
  232.     virtual void DeleteHook(ODMemoryHook* ODMemoryHook);
  233.  
  234.     virtual Boolean GetAutoValidation() const;
  235.     virtual void SetAutoValidation(Boolean = false);
  236.     Boolean IsValidBlock(const void* blk) const;
  237.     virtual Boolean IsMyBlock(const void* blk) const = 0;
  238.     MMBoolean FindBlockContaining( const void *start, const void *end,
  239.                         const void* &blockStart, const void* &blockEnd ) const;
  240.  
  241.     virtual void Check( HeapWalkProc proc =NULL, void *refCon =NULL ) const = 0;
  242.     virtual void Print(char* msg = "") const = 0;
  243.  
  244.     // Access to block StackCrawl.
  245.     void SetBlockStackCrawl( const void* blk, StackCrawl* );
  246.     StackCrawl* GetBlockStackCrawl( const void* blk ) const;
  247. #endif
  248.  
  249.     MemoryHeap(Boolean autoValidation = false,
  250.                    Boolean zapOnAllocate = true,
  251.                    Boolean zapOnFree = false,
  252.                    MMHeapLocation =kMMTempMemory);
  253.  
  254.     virtual void* AllocateRawMemory(ODBlockSize size);
  255.     virtual void* DoAllocate(ODBlockSize size, ODBlockSize& allocatedSize) = 0;
  256.     virtual ODBlockSize DoBlockSize(const void* block) const  = 0;
  257.     virtual void DoFree(void*) = 0;
  258.     virtual void DoReset() = 0;
  259.     virtual void* DoReallocate(void* block, ODBlockSize newSize, ODBlockSize& allocatedSize);
  260.     virtual void FreeRawMemory(void* ptr);
  261.     virtual unsigned long DoLargestFreeBlock() const = 0;
  262.     
  263.     virtual void DoSetBlockIsObject( void* ptr, Boolean isObject ) = 0;
  264.     virtual Boolean DoBlockIsObject( const void* ptr ) const = 0;
  265.     virtual MemoryHeap* DoGetBlockHeap( const void* ) const = 0;
  266.  
  267. #if MM_DEBUG
  268.     virtual Boolean DoIsValidBlock(const void* blk) const  = 0;
  269.     virtual MMBoolean DoFindBlockContaining( const void *start, const void *end,
  270.                         const void* &blockStart, const void* &blockEnd ) const = 0;
  271.     virtual void CompilerCheck();
  272. #endif
  273.  
  274. #if MM_DEBUG
  275.     MMBoolean ValidateMagicNumber( ) const;
  276. #else
  277.     inline void ValidateMagicNumber( ) const{ }
  278. #endif
  279.  
  280. private:
  281.     MemoryHeap* fNextHeap;
  282.     MMHeapLocation fMemSource;
  283.     Boolean fZapOnAllocate;
  284.     Boolean fZapOnFree;
  285.     Boolean fAutoValidation;
  286.     unsigned long fBytesAllocated;
  287.     unsigned long fNumberAllocatedBlocks;
  288.     long fMagicNumber;
  289.     MMBlock fSlushFund;
  290.     size_t    fSlushFundSize;
  291.     size_t    fSlushFundAllocSizeLimit;    // Max request size to use slush fund for
  292.  
  293.     enum { kDescriptionLength = 64 };
  294.     char fDescription[kDescriptionLength];
  295.     
  296. #if MM_DEBUG
  297.     MemoryHookList fMemoryHookList;
  298.  
  299. protected:
  300.     ODBlockSize CallGetHeaderSize() const;
  301.     
  302.     ODBlockSize CallAboutToAllocateHooks(ODBlockSize size) const;
  303.     void* CallDidAllocateHooks(void* blk, ODBlockSize size);
  304.     const void* CallAboutToBlockSizeHooks(const void* blk) const;
  305.     void* CallAboutToFreeHooks(void* blk);
  306.     void CallAboutToReallocHooks(void*& blk, ODBlockSize& size);
  307.     void* CallDidReallocHooks(void* oldBlk, void* blk, ODBlockSize size);
  308.     void CallAboutToResetHooks();
  309.     void CallCommentHooks(const char* comment);
  310.     void ValidateAndReport(void* blk) const;
  311. #endif
  312.  
  313. private:
  314.     MemoryHeap(const MemoryHeap& blk);
  315.     MemoryHeap& operator=(const MemoryHeap& blk);
  316.         // This class shouldn't be copied.
  317. };
  318.  
  319.  
  320. #endif
  321.