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 / Utilities / ODMemory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  11.3 KB  |  398 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODMemory.cpp
  3.  
  4.     Contains:    Procedural implementation to the Memory component
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     6/19/96    jpa        Fixed header comment
  13.          <2>     6/19/96    jpa        1357406: Added COverridingMacOSMemory.
  14.  
  15.     In Progress:
  16.         
  17. */
  18.  
  19. #ifndef _ODMEMORY_
  20. #include "ODMemory.h"
  21. #endif
  22.  
  23. #ifndef _MEMMGR_
  24. #include "MemMgr.h"
  25. #endif
  26.  
  27. #ifndef __CODEFRAGMENTS__
  28. #include <CodeFragments.h>
  29. #endif
  30.  
  31. #ifndef __ERRORS__
  32. #include <Errors.h>
  33. #endif
  34.  
  35. #ifndef _EXCEPT_
  36. #include "Except.h"
  37. #endif
  38.  
  39. #ifndef _ODDEBUG_
  40. #include "ODDebug.h"
  41. #endif
  42.  
  43. #ifndef _UTILERRS_
  44. #include "UtilErrs.h"
  45. #endif
  46.  
  47. //========================================================================================
  48. // Constants
  49. //========================================================================================
  50.  
  51. const size_t kSystemHeapInitialSize = 20 * 1024;
  52. const size_t kSystemHeapGrowBySize  = 32 * 1024;
  53.  
  54. const ODSize kMinAppHeapSpace        = 16 * 1024;
  55. const ODSize kMinContigAppHeapSpace =  6 * 1024;
  56.  
  57. //========================================================================================
  58. // Global variables
  59. //========================================================================================
  60.  
  61. static ODMemoryHeapID    gDefaultHeap;
  62. #if 0
  63. #pragma lib_export on                // Need to tell compiler that gSystemHeap is imported
  64. extern void* gSystemHeap;            // Lives in Shared Globals library, MemShard.cpp.
  65. #pragma lib_export off
  66. #endif /* 0 */
  67. //========================================================================================
  68. // Initialization
  69. //========================================================================================
  70.  
  71.  
  72. OSErr InitODMemory( )
  73. {
  74.     gDefaultHeap = MMGetDefaultHeap();
  75.     return gDefaultHeap ?noErr :memFullErr;
  76. }
  77.  
  78. // Note: CFMInit stuff is now in UtilInit.cpp
  79.  
  80. //extern "C" pascal OSErr UtilitiesCFMInit( CFragInitBlockPtr );
  81.  
  82. // For use inside OpenDoc only. Parts won't need to use this but
  83. // should call InitODMemory from their own CFMInit routines.
  84. //pascal OSErr UtilitiesCFMInit (CFragInitBlockPtr /*initBlkPtr*/)
  85. //{
  86. //    return InitODMemory();
  87. //}
  88.  
  89.  
  90. //========================================================================================
  91. // Function declarations for operations on pointer based blocks
  92. //========================================================================================
  93.  
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // ODGetDefaultHeap
  97. //----------------------------------------------------------------------------------------
  98.  
  99. ODMemoryHeapID ODGetDefaultHeap()
  100. {
  101.     ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  102.     return gDefaultHeap;
  103. }
  104. #if 0
  105. //----------------------------------------------------------------------------------------
  106. // ODGetSystemHeap
  107. //----------------------------------------------------------------------------------------
  108.  
  109. ODMemoryHeapID ODGetSystemHeap()
  110. {
  111.     // The system heap should be created only by the OD system process.
  112.     
  113.     if( !gSystemHeap ) {
  114.         gSystemHeap = ODCreateHeap(    kSystemHeapInitialSize,
  115.                                     kSystemHeapGrowBySize,
  116.                                     kODTrue,"OpenDoc System Heap" );
  117.         THROW_IF_NULL(gSystemHeap);
  118.     }
  119.     return (ODMemoryHeapID)gSystemHeap;
  120. }
  121. #endif /* 0 */
  122. //----------------------------------------------------------------------------------------
  123. // ODCreateHeap
  124. //----------------------------------------------------------------------------------------
  125.  
  126. ODMemoryHeapID ODCreateHeap(unsigned long sizeInitial, unsigned long sizeIncrement,
  127.                             Boolean fromSysMemory, const char *name)
  128. {
  129.     MemHeap *heap = MMNewHeap(fromSysMemory ?kMMSysMemory :kMMTempMemory,
  130.                               sizeInitial, sizeIncrement, name);
  131.     if( !heap )
  132.         THROW(kODErrOutOfMemory);
  133.     return heap;
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // ODDestroyHeap
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void ODDestroyHeap(ODMemoryHeapID heapID)
  141. {
  142.     MMDisposeHeap(heapID);
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // ODNewPtr
  147. //----------------------------------------------------------------------------------------
  148.  
  149. void *ODNewPtr(ODBlockSize blkSize, ODMemoryHeapID heapID)
  150. {
  151.     if( heapID == kODNULL ) {
  152.         ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  153.         heapID = gDefaultHeap;
  154.     }
  155.     void *block = MMAllocateIn(blkSize,heapID);
  156.     if( !block )
  157.         THROW(kODErrOutOfMemory);
  158.     return block;
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // ODNewPtrClear
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void *ODNewPtrClear(ODBlockSize blkSize, ODMemoryHeapID heapID)
  166. {
  167.     if( heapID == kODNULL ) {
  168.         ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  169.         heapID = gDefaultHeap;
  170.     }
  171.     void *block = MMAllocateClearIn(blkSize,heapID);
  172.     if( !block )
  173.         THROW(kODErrOutOfMemory);
  174.     return block;
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // ODReallocate
  179. //----------------------------------------------------------------------------------------
  180.  
  181. void *ODReallocate(void *block, ODBlockSize newSize)
  182. {
  183.     block = MMReallocate(block,newSize);
  184.     if( !block && newSize>0 )
  185.         THROW(kODErrOutOfMemory);
  186.     return block;
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190. // ODDisposePtr
  191. //------------------------------------------------------------------------------
  192.  
  193. void ODDisposePtr(void *pointer)
  194. {
  195.     MMFree(pointer);
  196. }
  197.  
  198. //----------------------------------------------------------------------------------------
  199. // ODRecoverHeapID
  200. //----------------------------------------------------------------------------------------
  201.  
  202. ODMemoryHeapID ODRecoverHeapID(const void *block)
  203. {
  204.     ODMemoryHeapID heap = MMGetHeap(block);
  205.     if( !heap )
  206.         THROW(kODErrInvalidBlock);
  207.     return heap;
  208. }
  209.  
  210.  
  211.  
  212. //----------------------------------------------------------------------------------------
  213. // ODBlockIsObject
  214. //----------------------------------------------------------------------------------------
  215.  
  216. void ODBlockIsObject( void *block, ODBoolean isObject )
  217. {
  218.     MMSetIsObject(block,isObject);
  219. }
  220.  
  221.  
  222. //----------------------------------------------------------------------------------------
  223. // ODIsBlockAnObject
  224. //----------------------------------------------------------------------------------------
  225.  
  226. ODBoolean ODIsBlockAnObject( const void *block )
  227. {
  228.     return MMIsObject(block);
  229. }
  230.  
  231.  
  232. //========================================================================================
  233. // Function declarations for operations on handle based blocks
  234. //========================================================================================
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // ODNewHandle
  238. //----------------------------------------------------------------------------------------
  239.  
  240. ODHandle ODNewHandle(ODULong howBig)
  241. {
  242.     ODHandle h = (ODHandle) MMAllocateHandleIn(howBig,kMMTempMemory);
  243.     if( !h )
  244.         THROW(kODErrOutOfMemory);
  245.     return h;
  246.         
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. // ODDisposeHandle
  251. //----------------------------------------------------------------------------------------
  252.  
  253. void ODDisposeHandle(ODHandle handle)
  254. {
  255.     MMFreeHandle((MMHandle)handle);
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. // ODCopyHandle
  260. //----------------------------------------------------------------------------------------
  261.  
  262. ODHandle ODCopyHandle(ODHandle handle)
  263. {
  264.     ODHandle h = (ODHandle) MMCopyHandle((MMHandle)handle);
  265.     if( !h )
  266.         THROW(kODErrOutOfMemory);
  267.     return h;
  268. }
  269.  
  270. //----------------------------------------------------------------------------------------
  271. // ODGetHandleSize(ODHandle handle)
  272. //----------------------------------------------------------------------------------------
  273.  
  274. ODULong ODGetHandleSize(ODHandle handle)
  275. {
  276.     return MMGetHandleSize((MMHandle)handle);
  277. }
  278.  
  279. //----------------------------------------------------------------------------------------
  280. // ODSetHandleSize(ODHandle handle, ODULong blkSize)
  281. //----------------------------------------------------------------------------------------
  282.  
  283. void ODSetHandleSize(ODHandle handle, ODULong blkSize)
  284. {
  285.     if( !MMSetHandleSize((MMHandle)handle,blkSize) )
  286.         THROW(kODErrOutOfMemory);
  287.  
  288. }
  289.  
  290. //----------------------------------------------------------------------------------------
  291. // ODLockHandle(ODHandle handle)
  292. //----------------------------------------------------------------------------------------
  293.  
  294. void* ODLockHandle(ODHandle handle)
  295. {
  296.     void *p = MMLockHandle((MMHandle)handle);
  297.     if( !p )
  298.         THROW(kODErrOutOfMemory);
  299.     return p;
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. // ODUnlockPtr(void* ptr)
  304. //----------------------------------------------------------------------------------------
  305.  
  306. void ODUnlockPtr(void* ptr)
  307. {
  308.     MMUnlockPtr(ptr);
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. // ODUnlockHandle(ODHandle handle)
  313. //----------------------------------------------------------------------------------------
  314.  
  315. void ODUnlockHandle(ODHandle handle)
  316. {
  317.     MMUnlockHandle((MMHandle)handle);
  318. }
  319.  
  320.  
  321. //========================================================================================
  322. // Function declarations utility functions
  323. //========================================================================================
  324.  
  325. //------------------------------------------------------------------------------
  326. // ODBlockMove
  327. //------------------------------------------------------------------------------
  328.  
  329.  
  330. void ODBlockMove( const void *from, void *to, ODULong size)
  331. {
  332.     MMMove(to,from,size);
  333. }
  334.  
  335.  
  336. //------------------------------------------------------------------------------
  337. // ODNewRgn
  338. //------------------------------------------------------------------------------
  339.  
  340. RgnHandle ODNewRgn( )
  341. {
  342.     RgnHandle r = (RgnHandle) ODNewHandle(sizeof(Region));
  343.     (**r).rgnSize = sizeof(Region);
  344.     SetRect(&(**r).rgnBBox, 0,0,0,0);
  345.     return r;
  346. }
  347.  
  348. //------------------------------------------------------------------------------
  349. // ODRequireFreeSpace
  350. //------------------------------------------------------------------------------
  351.  
  352.  
  353. ODBoolean
  354. ODHaveFreeSpace( ODSize haveTotal, ODSize haveContig /*=0*/,
  355.                  ODBoolean appHeap /*=false*/ )
  356. {
  357.     size_t total, contig;
  358.     if( appHeap )
  359.         MMSystemFreeSpace(kMMAppMemory, &total,&contig);
  360.     else
  361.         MMGetFreeSpace(gDefaultHeap,&total,&contig );
  362.     return total>=haveTotal && contig>=haveContig;
  363. }
  364.  
  365. void
  366. ODRequireFreeSpace( ODSize total, ODSize contig, ODBoolean appHeap )
  367. {
  368.     if( !ODHaveFreeSpace(total,contig,appHeap) )
  369.         THROW(appHeap ?memFullErr :kODErrOutOfMemory);
  370.     
  371.     // I decided it made sense to throw a Mac mem mgr error if the
  372.     // app heap is full, since this is the error you'd get if you
  373.     // tried to d
  374. }
  375.  
  376. void
  377. ODCheckAppHeapSpace( )
  378. {
  379.     if( !ODHaveFreeSpace(kMinAppHeapSpace,kMinContigAppHeapSpace,kODTrue) )
  380.         THROW(memFullErr);
  381. }
  382.  
  383.  
  384. //========================================================================================
  385. // MacOS NewHandle override
  386. //========================================================================================
  387.  
  388. COverridingMacOSMemory::COverridingMacOSMemory( )
  389. {
  390.     MMOverridePlatform(kODTrue,kODTrue);
  391. }
  392.  
  393.  
  394. COverridingMacOSMemory::~COverridingMacOSMemory( )
  395. {
  396.     MMEndOverridePlatform(kODTrue,kODTrue);
  397. }
  398.