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 Development Framework / ODFDev / ODF / Found / ODUtils / ODMemory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  12.0 KB  |  414 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; // ----- [HLX] Changed  -----
  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. // ----- [HLX] Changed Begin -----
  75. //    gDefaultHeap = MMGetDefaultHeap();
  76. //    return gDefaultHeap ?noErr :memFullErr;
  77.     return noErr;
  78. // ----- [HLX] Changed End -----
  79. }
  80.  
  81. // Note: CFMInit stuff is now in UtilInit.cpp
  82.  
  83. //extern "C" pascal OSErr UtilitiesCFMInit( CFragInitBlockPtr );
  84.  
  85. // For use inside OpenDoc only. Parts won't need to use this but
  86. // should call InitODMemory from their own CFMInit routines.
  87. //pascal OSErr UtilitiesCFMInit (CFragInitBlockPtr /*initBlkPtr*/)
  88. //{
  89. //    return InitODMemory();
  90. //}
  91.  
  92.  
  93. //========================================================================================
  94. // Function declarations for operations on pointer based blocks
  95. //========================================================================================
  96.  
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // ODGetDefaultHeap
  100. //----------------------------------------------------------------------------------------
  101.  
  102. ODMemoryHeapID ODGetDefaultHeap()
  103. {
  104. // ----- [HLX] Changed Begin -----
  105.     // Tweaked by JEL on 10/31/95
  106.     // Don't use FW_CMemoryTaskGlobals: 
  107.     // 1) It's a circular dependency
  108.     // 2) We may remove soon remove FW_CMemoryTaskGlobals entirely
  109.     // We may want to restore the global gDefaultHeap and use it here.
  110.     return ::MMGetDefaultHeap();
  111. //    ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  112. //    return gDefaultHeap;
  113. // ----- [HLX] Changed End -----
  114. }
  115. #if 0
  116. //----------------------------------------------------------------------------------------
  117. // ODGetSystemHeap
  118. //----------------------------------------------------------------------------------------
  119.  
  120. ODMemoryHeapID ODGetSystemHeap()
  121. {
  122.     // The system heap should be created only by the OD system process.
  123.     
  124.     if( !gSystemHeap ) {
  125.         gSystemHeap = ODCreateHeap(    kSystemHeapInitialSize,
  126.                                     kSystemHeapGrowBySize,
  127.                                     kODTrue,"OpenDoc System Heap" );
  128.         THROW_IF_NULL(gSystemHeap);
  129.     }
  130.     return (ODMemoryHeapID)gSystemHeap;
  131. }
  132. #endif /* 0 */
  133. //----------------------------------------------------------------------------------------
  134. // ODCreateHeap
  135. //----------------------------------------------------------------------------------------
  136.  
  137. ODMemoryHeapID ODCreateHeap(unsigned long sizeInitial, unsigned long sizeIncrement,
  138.                             Boolean fromSysMemory, const char *name)
  139. {
  140.     MemHeap *heap = MMNewHeap(fromSysMemory ?kMMSysMemory :kMMTempMemory,
  141.                               sizeInitial, sizeIncrement, name);
  142.     if( !heap )
  143.         THROW(kODErrOutOfMemory);
  144.     return heap;
  145. }
  146.  
  147. //----------------------------------------------------------------------------------------
  148. // ODDestroyHeap
  149. //----------------------------------------------------------------------------------------
  150.  
  151. void ODDestroyHeap(ODMemoryHeapID heapID)
  152. {
  153.     MMDisposeHeap(heapID);
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. // ODNewPtr
  158. //----------------------------------------------------------------------------------------
  159.  
  160. void *ODNewPtr(ODBlockSize blkSize, ODMemoryHeapID heapID)
  161. {
  162.     if( heapID == kODNULL ) {
  163. // ----- [HLX] Changed Begin -----
  164. //        ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  165. //        heapID = gDefaultHeap;
  166.         heapID = ODGetDefaultHeap();
  167. // ----- [HLX] Changed End -----
  168.     }
  169.     void *block = MMAllocateIn(blkSize,heapID);
  170.     if( !block )
  171.         THROW(kODErrOutOfMemory);
  172.     return block;
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // ODNewPtrClear
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void *ODNewPtrClear(ODBlockSize blkSize, ODMemoryHeapID heapID)
  180. {
  181.     if( heapID == kODNULL ) {
  182. // ----- [HLX] Changed Begin -----
  183. //        ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  184. //        heapID = gDefaultHeap;
  185.         heapID = ODGetDefaultHeap();
  186. // ----- [HLX] Changed End -----
  187.     }
  188.     void *block = MMAllocateClearIn(blkSize,heapID);
  189.     if( !block )
  190.         THROW(kODErrOutOfMemory);
  191.     return block;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // ODReallocate
  196. //----------------------------------------------------------------------------------------
  197.  
  198. void *ODReallocate(void *block, ODBlockSize newSize)
  199. {
  200.     block = MMReallocate(block,newSize);
  201.     if( !block && newSize>0 )
  202.         THROW(kODErrOutOfMemory);
  203.     return block;
  204. }
  205.  
  206. //------------------------------------------------------------------------------
  207. // ODDisposePtr
  208. //------------------------------------------------------------------------------
  209.  
  210. void ODDisposePtr(void *pointer)
  211. {
  212.     MMFree(pointer);
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // ODRecoverHeapID
  217. //----------------------------------------------------------------------------------------
  218.  
  219. ODMemoryHeapID ODRecoverHeapID(const void *block)
  220. {
  221.     ODMemoryHeapID heap = MMGetHeap(block);
  222.     if( !heap )
  223.         THROW(kODErrInvalidBlock);
  224.     return heap;
  225. }
  226.  
  227.  
  228.  
  229. //----------------------------------------------------------------------------------------
  230. // ODBlockIsObject
  231. //----------------------------------------------------------------------------------------
  232.  
  233. void ODBlockIsObject( void *block, ODBoolean isObject )
  234. {
  235.     MMSetIsObject(block,isObject);
  236. }
  237.  
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // ODIsBlockAnObject
  241. //----------------------------------------------------------------------------------------
  242.  
  243. ODBoolean ODIsBlockAnObject( const void *block )
  244. {
  245.     return MMIsObject(block);
  246. }
  247.  
  248.  
  249. //========================================================================================
  250. // Function declarations for operations on handle based blocks
  251. //========================================================================================
  252.  
  253. //----------------------------------------------------------------------------------------
  254. // ODNewHandle
  255. //----------------------------------------------------------------------------------------
  256.  
  257. ODHandle ODNewHandle(ODULong howBig)
  258. {
  259.     ODHandle h = (ODHandle) MMAllocateHandleIn(howBig,kMMTempMemory);
  260.     if( !h )
  261.         THROW(kODErrOutOfMemory);
  262.     return h;
  263.         
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // ODDisposeHandle
  268. //----------------------------------------------------------------------------------------
  269.  
  270. void ODDisposeHandle(ODHandle handle)
  271. {
  272.     MMFreeHandle((MMHandle)handle);
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. // ODCopyHandle
  277. //----------------------------------------------------------------------------------------
  278.  
  279. ODHandle ODCopyHandle(ODHandle handle)
  280. {
  281.     ODHandle h = (ODHandle) MMCopyHandle((MMHandle)handle);
  282.     if( !h )
  283.         THROW(kODErrOutOfMemory);
  284.     return h;
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. // ODGetHandleSize(ODHandle handle)
  289. //----------------------------------------------------------------------------------------
  290.  
  291. ODULong ODGetHandleSize(ODHandle handle)
  292. {
  293.     return MMGetHandleSize((MMHandle)handle);
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. // ODSetHandleSize(ODHandle handle, ODULong blkSize)
  298. //----------------------------------------------------------------------------------------
  299.  
  300. void ODSetHandleSize(ODHandle handle, ODULong blkSize)
  301. {
  302.     if( !MMSetHandleSize((MMHandle)handle,blkSize) )
  303.         THROW(kODErrOutOfMemory);
  304.  
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. // ODLockHandle(ODHandle handle)
  309. //----------------------------------------------------------------------------------------
  310.  
  311. void* ODLockHandle(ODHandle handle)
  312. {
  313.     void *p = MMLockHandle((MMHandle)handle);
  314.     if( !p )
  315.         THROW(kODErrOutOfMemory);
  316.     return p;
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // ODUnlockPtr(void* ptr)
  321. //----------------------------------------------------------------------------------------
  322.  
  323. void ODUnlockPtr(void* ptr)
  324. {
  325.     MMUnlockPtr(ptr);
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // ODUnlockHandle(ODHandle handle)
  330. //----------------------------------------------------------------------------------------
  331.  
  332. void ODUnlockHandle(ODHandle handle)
  333. {
  334.     MMUnlockHandle((MMHandle)handle);
  335. }
  336.  
  337.  
  338. //========================================================================================
  339. // Function declarations utility functions
  340. //========================================================================================
  341.  
  342. //------------------------------------------------------------------------------
  343. // ODBlockMove
  344. //------------------------------------------------------------------------------
  345.  
  346.  
  347. void ODBlockMove( const void *from, void *to, ODULong size)
  348. {
  349.     MMMove(to,from,size);
  350. }
  351.  
  352.  
  353. //------------------------------------------------------------------------------
  354. // ODNewRgn
  355. //------------------------------------------------------------------------------
  356.  
  357. RgnHandle ODNewRgn( )
  358. {
  359.     RgnHandle r = (RgnHandle) ODNewHandle(sizeof(Region));
  360.     (**r).rgnSize = sizeof(Region);
  361.     SetRect(&(**r).rgnBBox, 0,0,0,0);
  362.     return r;
  363. }
  364.  
  365. //------------------------------------------------------------------------------
  366. // ODRequireFreeSpace
  367. //------------------------------------------------------------------------------
  368.  
  369.  
  370. ODBoolean
  371. ODHaveFreeSpace( ODSize haveTotal, ODSize haveContig /*=0*/,
  372.                  ODBoolean appHeap /*=false*/ )
  373. {
  374.     size_t total, contig;
  375.     if( appHeap )
  376.         MMSystemFreeSpace(kMMAppMemory, &total,&contig);
  377.     else
  378.         MMGetFreeSpace(ODGetDefaultHeap(),&total,&contig ); // [HLX] changed
  379.     return total>=haveTotal && contig>=haveContig;
  380. }
  381.  
  382. void
  383. ODRequireFreeSpace( ODSize total, ODSize contig, ODBoolean appHeap )
  384. {
  385.     if( !ODHaveFreeSpace(total,contig,appHeap) )
  386.         THROW(appHeap ?memFullErr :kODErrOutOfMemory);
  387.     
  388.     // I decided it made sense to throw a Mac mem mgr error if the
  389.     // app heap is full, since this is the error you'd get if you
  390.     // tried to d
  391. }
  392.  
  393. void
  394. ODCheckAppHeapSpace( )
  395. {
  396.     if( !ODHaveFreeSpace(kMinAppHeapSpace,kMinContigAppHeapSpace,kODTrue) )
  397.         THROW(memFullErr);
  398. }
  399.  
  400. //========================================================================================
  401. // MacOS NewHandle override
  402. //========================================================================================
  403.  
  404. COverridingMacOSMemory::COverridingMacOSMemory( )
  405. {
  406.     MMOverridePlatform(kODTrue,kODTrue);
  407. }
  408.  
  409.  
  410. COverridingMacOSMemory::~COverridingMacOSMemory( )
  411. {
  412.     MMEndOverridePlatform(kODTrue,kODTrue);
  413. }
  414.