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 / Messaging / OSL / OSLGSExm.c < prev    next >
Encoding:
Text File  |  1996-08-28  |  6.6 KB  |  244 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OSLGSExm.c (Orignal name: OSLGetSetExmn.c)
  3.  
  4.     Contains:    OSL Globals
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1992 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <7>     6/27/95    NP        1262792: Fix stack dynamic allocation
  13.                                     problem
  14.          <6>     6/24/95    TJ        Changed kNumStaticContextStackEntries from
  15.                                     a const to a #define so it would compile
  16.                                     with SC.
  17.          <5>     6/23/95    NP        1195474: Make Resolve reentrant.
  18.          <4>     2/22/95    eeh        1222904: fix use of SetCurrentContext
  19.          <3>      2/8/95    NP        1218550: Don't allocate OSLContexts
  20.                                     dynamically.
  21.          <2>     8/19/94    NP        1181622: Ownership fix.
  22.          <6>      5/5/94    eeh        bug #1160654: fix for SCPP
  23.          <5>      5/2/94    eeh        bug #1160654: various PPC native changes
  24.          <4>     8/18/93    NP        Added get and set current context.
  25.          <3>     7/29/93    NP        Removed compiler warning for unused
  26.                                     parameters.
  27.          <2>     7/28/93    NP        Mods for new token type, OSLToken.
  28.          <1>     7/21/93    NP        first checked in
  29.  
  30.     To Do:
  31. */
  32.  
  33. // ASSUMPTION: This code will be used in a library that has per-context globals!
  34.  
  35. #include "OSLPriv.h"
  36.  
  37.  
  38. #define kExmnKey1 'eone'
  39. #define kExmnKey2 'etwo'
  40.  
  41. #define    UNUSED(x) ((void) &x)
  42.  
  43. // this guy should go away, and be replaced by calls to GetAccessor or 
  44. // GetEventHandler using the keys defined above.  This should allow a
  45. // more general storage mechanism if it becomes necessary.
  46. // static AEDesc theGlobal ;
  47.  
  48. OSLToken    theGlobalToken;
  49.  
  50. static OSErr
  51. GetGlobalDesc( unsigned long key1, unsigned long key2, AEDesc* desc )
  52. {
  53. //    return iAEGetObjectAccessor( key1, key2, (accessorProcPtr *)&desc->descriptorType,
  54. //            (long *)&desc->dataHandle, false, 0) ;
  55. //    *desc = theGlobalDesc;
  56.     UNUSED( key1 );
  57.     UNUSED( key2 );
  58.     UNUSED( desc );
  59.     return noErr;
  60. }
  61.  
  62. static OSErr
  63. SetGlobalDesc( unsigned long key1, unsigned long key2, AEDesc* desc )
  64. {
  65. //    return iAEInstallObjectAccessor( key1, key2, (accessorProcPtr)desc->descriptorType,
  66. //            (long)desc->dataHandle, false, 0) ;
  67. //    theGlobalDesc = *desc;
  68.     UNUSED( key1 );
  69.     UNUSED( key2 );
  70.     UNUSED( desc );
  71.     return noErr;
  72. }
  73.  
  74.  
  75. // Get the currently available "global" exmn token
  76. OSErr
  77. GetExmn( OSLToken *result )
  78. {
  79.     *result = theGlobalToken;
  80.     return noErr;
  81. //    return GetGlobalDesc( kExmnKey1, kExmnKey2, result ) ;
  82. }
  83.  
  84. // Set the "global" exmn token to this one
  85. OSErr
  86. SetExmn( OSLToken *newDesc )
  87. {
  88.     theGlobalToken = *newDesc;
  89.     return noErr;
  90. //    return SetGlobalDesc( kExmnKey1, kExmnKey2, newDesc ) ;
  91. }
  92.  
  93.  
  94. // Replace the currently available "global" exmn token with
  95. // the one provided here.  And return the old value in its
  96. // place.
  97. OSErr
  98. SwapExmn( AEDesc *oldAndNewDesc )
  99. {
  100.     AEDesc temp ;
  101.     OSErr err = GetGlobalDesc( kExmnKey1, kExmnKey2, &temp ) ;
  102.     if ( err == noErr )
  103.     {
  104.         err = SetGlobalDesc( kExmnKey1, kExmnKey2, oldAndNewDesc ) ;
  105.         if ( err == noErr )
  106.             *oldAndNewDesc = temp ;
  107.     }
  108.     return err ;
  109. }
  110.  
  111. //==============================================================================
  112. // Context stack
  113. //
  114. //    Maintain stack of current contexts, one for each time OSLResolve is called.
  115. //==============================================================================
  116.  
  117. #define    kNumStaticContextStackEntries 5
  118.  
  119. struct ContextStack
  120. {
  121.     unsigned short    numEntries;
  122.     OSLContext            contextArray[kNumStaticContextStackEntries];
  123.     unsigned short    bufferSize;
  124.     OSLContext*            buffer;
  125. };
  126. typedef struct ContextStack ContextStack;
  127.  
  128. ContextStack    gContextStack =
  129.     {false, {{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}, 0, NULL};
  130.  
  131. //------------------------------------------------------------------------------
  132. // AddContextToTopOfStack
  133. //
  134. //    Use static entries in structure until they are filled up and then do
  135. //    dynamic allocation. We don't ever shrink the dynamic structure, we only
  136. //    grow it.
  137. //------------------------------------------------------------------------------
  138.  
  139. OSErr AddContextToTopOfStack(OSLContext* context)
  140. {
  141.     OSErr    error = noErr;
  142.  
  143. #if ODDEBUG
  144.     // WE ALWAYS HAVE AT LEAST ONE THAT'S PUT ON THE STACK BY iAEObjectInit
  145.     if (gContextStack.numEntries == 2)
  146.         DebugStr("\pSomeone is actually calling Resolve reentrantly!");
  147. #endif
  148.  
  149.     if (gContextStack.numEntries < kNumStaticContextStackEntries)
  150.         gContextStack.contextArray[gContextStack.numEntries] = *context;
  151.     else
  152.     {
  153.         // THE FIRST TIME WE ARE CALLED TO ALLOCATE ONE MORE CONTEXT THAN
  154.         //    kNumStaticContextStackEntries
  155.         if (gContextStack.buffer == NULL)
  156.         {
  157.             gContextStack.buffer = (OSLContext*)NewPtr(sizeof(OSLContext));
  158.             if (error == noErr)
  159.             {
  160.                 if (gContextStack.buffer == NULL)
  161.                     error = memFullErr;
  162.             }
  163.             if (error == noErr)
  164.                 gContextStack.bufferSize = sizeof(OSLContext);
  165.         }
  166.         else
  167.         {
  168.             Size  newBufferSize =
  169.                     (gContextStack.numEntries - kNumStaticContextStackEntries
  170.                         + 1) * sizeof(OSLContext);
  171.             if (gContextStack.bufferSize < newBufferSize)
  172.             {
  173.                 OSLContext*    newBuffer = (OSLContext*)NewPtr(newBufferSize);
  174.                 error = MemError();
  175.                 if (error == noErr)
  176.                 {
  177.                     BlockMoveData(gContextStack.buffer, newBuffer,
  178.                                     gContextStack.bufferSize);
  179.                     DisposePtr((Ptr)gContextStack.buffer);
  180.                     gContextStack.buffer = newBuffer;
  181.                     gContextStack.bufferSize = newBufferSize;
  182.                 }
  183.             }
  184.         }
  185.         if (error == noErr)
  186.             *(gContextStack.buffer
  187.                 + (gContextStack.numEntries - kNumStaticContextStackEntries)) = *context;
  188.     }
  189.  
  190.     if (error == noErr)
  191.         ++gContextStack.numEntries;
  192.  
  193.     return error;
  194. }
  195.  
  196. //------------------------------------------------------------------------------
  197. // RemoveTopOfContextStack
  198. //------------------------------------------------------------------------------
  199.  
  200. OSErr RemoveTopOfContextStack()
  201. {
  202.     --gContextStack.numEntries;
  203.     return noErr;
  204. }
  205.  
  206. //OSLContext    theGlobalContext;
  207.  
  208. //------------------------------------------------------------------------------
  209. // GetCurrentContext
  210. //------------------------------------------------------------------------------
  211.  
  212. OSErr GetCurrentContext( OSLContext* curContext )
  213. {
  214. //    *curContext = theGlobalContext;
  215.     if (gContextStack.numEntries <= kNumStaticContextStackEntries)
  216.         *curContext = gContextStack.contextArray[gContextStack.numEntries - 1];
  217.     else
  218.     {
  219.         *curContext =
  220.             *(gContextStack.buffer
  221.             + (gContextStack.numEntries - kNumStaticContextStackEntries - 1));
  222.     }
  223.     return noErr;
  224. }
  225.  
  226. //------------------------------------------------------------------------------
  227. // SetCurrentContext
  228. //------------------------------------------------------------------------------
  229.  
  230. OSErr SetCurrentContext( OSLContext* curContext )
  231. {
  232. //    theGlobalContext = *curContext;
  233.     if (gContextStack.numEntries <= kNumStaticContextStackEntries)
  234.         gContextStack.contextArray[gContextStack.numEntries - 1] = *curContext;
  235.     else
  236.     {
  237.         *(gContextStack.buffer
  238.             + (gContextStack.numEntries - kNumStaticContextStackEntries - 1))
  239.                     = *curContext;
  240.     }
  241.     return noErr;
  242. }
  243.  
  244.