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