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 / Storage / StorPriv.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  8.5 KB  |  293 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StorPriv.cpp
  3.  
  4.     Contains:    Implementation for ODStorageSystem class.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     3/15/96    DM        1295410: create list iterators on stack
  13.                                     (avoid mem thrash during purge)
  14.         <14>     8/22/95    VL        1255362: Removed XMP from header.
  15.         <13>     8/21/95    VL        1278330, 1278315: Error code cleanup.
  16.         <12>     7/21/95    VL        1270320: Dispose ba._buffer from
  17.                                     container->GetID.
  18.         <11>     5/26/95    VL        1251403: Multithreading naming support.
  19.         <10>     5/25/95    jpa        List.h --> LinkList.h [1253324]
  20.          <9>     4/25/95    VL        1210982: Removed 5$.
  21.          <8>     4/15/95    VL        1240014: ContainerList::Add gets the real
  22.                                     ID from container instead of through a
  23.                                     parameter.
  24.          <7>    10/19/94    VL        Turned off DEBUG_STORPRIV to avoid
  25.                                     generating unnecessary debug strs.
  26.          <6>     9/23/94    VL        1184272: ContainerID is now a sequence of
  27.                                     octets.
  28.          <5>      7/7/94    VL        Commented out use of ODRecoverHeapID.
  29.          <4>     6/28/94    VL        Use ODRecoverHeapID.
  30.          <3>     6/20/94    CC        ODMemoryHeap* changed to ODMemoryHeapID.
  31.          <2>     6/15/94    CC        ODHeap -> ODMemoryHeap.
  32.          <1>      6/1/94    VL        first checked in
  33.  
  34.     To Do:
  35.     In Progress:
  36. */
  37.  
  38. #ifndef _STORPRIV_
  39. #include "StorPriv.h"
  40. #endif
  41.  
  42. #ifndef SOM_ODContainer_xh
  43. #include "ODCtr.xh"
  44. #endif
  45.  
  46. #ifndef _LINKLIST_
  47. #include "LinkList.h"
  48. #endif
  49.  
  50. #ifndef _EXCEPT_
  51. #include "Except.h"
  52. #endif
  53.  
  54. #ifndef _ODMEMORY_
  55. #include "ODMemory.h"
  56. #endif
  57.  
  58. #ifndef _ODNEW_
  59. #include "ODNew.h"
  60. #endif
  61.  
  62. #ifndef _BARRAY_
  63. #include <BArray.h>
  64. #endif
  65.  
  66. #ifndef som_xh
  67. #include "som.xh"
  68. #endif
  69.  
  70. #pragma segment Storage
  71.  
  72. //==============================================================================
  73. // Constants
  74. //==============================================================================
  75. #define kODErrContainerExistsWithDifferentID 923
  76.  
  77. #if ODDebug
  78. // #define DEBUG_STORPRIV 1
  79. #endif
  80.  
  81. //==============================================================================
  82. // ContainerList
  83. //==============================================================================
  84.  
  85. //------------------------------------------------------------------------------
  86. // ContainerList::ContainerList
  87. //------------------------------------------------------------------------------
  88.  
  89. ContainerList::ContainerList()
  90. {
  91.     fHeap = kODNULL;
  92. }
  93.  
  94. //------------------------------------------------------------------------------
  95. // ContainerList::~ContainerList
  96. //------------------------------------------------------------------------------
  97.  
  98. ContainerList::~ContainerList()
  99. {
  100.     delete fLinkedList;
  101. }
  102.  
  103. //------------------------------------------------------------------------------
  104. // ContainerList::Initialize
  105. //------------------------------------------------------------------------------
  106.  
  107. void ContainerList::Initialize()
  108. {
  109.     // Using default heap because the Storage System also resides in Default heap.
  110.     fHeap = kDefaultHeapID;
  111.     
  112.     fLinkedList = new(GetHeap()) LinkedList;
  113. }
  114.  
  115. //------------------------------------------------------------------------------
  116. // ContainerList::Get
  117. //------------------------------------------------------------------------------
  118.  
  119. ODContainer* ContainerList::Get(ODContainerID* containerID)
  120. {    
  121.     ContainerLink* link = this->GetLink(containerID);
  122.     ODContainer*    container = kODNULL;
  123.     
  124.     if (link != kODNULL)
  125.         container = link->fContainer;
  126.         
  127. #ifdef DEBUG_STORPRIV
  128.     somPrintf("ContainerList:Get containerID %x length %x container %x\n", containerID, containerID->_length, container);
  129.     for (ODULong i = 0; i < containerID->_length; i++) 
  130.         somPrintf("%x ", (containerID->_buffer)[i]);
  131.     somPrintf("\n");
  132. #endif
  133.  
  134.     return container;
  135. }
  136.  
  137. //------------------------------------------------------------------------------
  138. // ContainerList::Add
  139. //------------------------------------------------------------------------------
  140.  
  141. void ContainerList::Add(ODContainer* container)
  142. {    
  143.     ODContainer* existingContainer;
  144.     ContainerLink* link;
  145.  
  146. #ifdef DEBUG_STORPRIV
  147.     somPrintf("ContainerList:Add containerID %x length %x container %x\n", containerID, containerID->_length, container);
  148.     for (ODULong i = 0; i < containerID->_length; i++) 
  149.         somPrintf("%x ", (containerID->_buffer)[i]);
  150.     somPrintf("\n");
  151. #endif
  152.     Environment* ev = somGetGlobalEnvironment();
  153.     
  154.     ODContainerID containerID = container->GetID(ev);
  155.     if ((existingContainer = this->Get(&containerID)) == kODNULL) {
  156.         link = new(GetHeap()) ContainerLink(container);
  157.         fLinkedList->AddLast(link);
  158.     }
  159.     ODDisposePtr(containerID._buffer);
  160. }
  161.         
  162. //------------------------------------------------------------------------------
  163. // ContainerList::Remove
  164. //------------------------------------------------------------------------------
  165.  
  166. void ContainerList::Remove(ODContainerID* containerID)
  167. {
  168.     ContainerLink* link = this->GetLink(containerID);
  169.  
  170. #ifdef DEBUG_STORPRIV
  171.     somPrintf("ContainerList:Remove containerID %x length %x\n", containerID, containerID->_length, *((ODULong*) containerID->_buffer));
  172.     for (ODULong i = 0; i < containerID->_length; i++) 
  173.         somPrintf("%x ", (containerID->_buffer)[i]);
  174.     somPrintf("\n");
  175. #endif
  176.     
  177.     if (link == kODNULL) {
  178.         THROW(kODErrContainerDoesNotExist);
  179.     }
  180.     else {
  181.         fLinkedList->Remove(*link);
  182.         delete link;
  183.     }
  184. }
  185.  
  186. //------------------------------------------------------------------------------
  187. // ContainerList::GetLink
  188. //------------------------------------------------------------------------------
  189.  
  190. ContainerLink* ContainerList::GetLink(ODContainerID* containerID)
  191. {
  192.     Environment*    ev = somGetGlobalEnvironment();
  193.     LinkedListIterator    iter(fLinkedList);
  194.     ODBoolean    found = kODFalse;
  195.     
  196.     ContainerLink* link = (ContainerLink*) iter.Last();
  197.     while ((link != kODNULL) && (found == kODFalse))
  198.     {
  199.         ODContainerID id = link->fContainer->GetID(ev);
  200.         if (AreByteArraysEqual(&id, containerID))
  201.             found = kODTrue;
  202.         else
  203.             link = (ContainerLink*) iter.Previous();
  204.         ODDisposePtr(id._buffer);
  205.     }
  206.     return link;
  207. }
  208.  
  209. //------------------------------------------------------------------------------
  210. // ContainerList::GetHeap
  211. //------------------------------------------------------------------------------
  212.  
  213. ODMemoryHeapID ContainerList::GetHeap()
  214. {
  215.     return fHeap;
  216. }
  217.  
  218. //------------------------------------------------------------------------------
  219. // ContainerList::GetLinkedList
  220. //------------------------------------------------------------------------------
  221.  
  222. LinkedList* ContainerList::GetLinkedList()
  223. {
  224.     return fLinkedList;
  225. }
  226.  
  227. //==============================================================================
  228. // ContainerListIterator
  229. //==============================================================================
  230.  
  231. //------------------------------------------------------------------------------
  232. // ContainerListIterator::ContainerListIterator
  233. //------------------------------------------------------------------------------
  234.  
  235. ContainerListIterator::ContainerListIterator(ContainerList* containerList)
  236.     : fContainerList(containerList), fIterator( containerList->GetLinkedList() )
  237. {
  238.     // fContainerList = containerList;
  239.     // fIterator = kODNULL;
  240. }
  241.     
  242. //------------------------------------------------------------------------------
  243. // ContainerListIterator::~ContainerListIterator
  244. //------------------------------------------------------------------------------
  245.  
  246. ContainerListIterator::~ContainerListIterator()
  247. {
  248.     // delete fIterator;
  249. }
  250.  
  251. //------------------------------------------------------------------------------
  252. // ContainerListIterator::Initialize
  253. //------------------------------------------------------------------------------
  254.  
  255. void ContainerListIterator::Initialize()
  256. {
  257.     // fIterator = new(fContainerList->GetHeap()) LinkedListIterator(fContainerList->GetLinkedList());
  258. }
  259.  
  260. //------------------------------------------------------------------------------
  261. // ContainerListIterator::Last
  262. //------------------------------------------------------------------------------
  263.  
  264. ODContainer* ContainerListIterator::Last()
  265. {
  266.     ContainerLink* link = (ContainerLink*) fIterator.Last();
  267.     if (link == kODNULL)
  268.         return kODNULL;
  269.     return link->fContainer;
  270. }
  271.  
  272. //------------------------------------------------------------------------------
  273. // ContainerListIterator::Previous
  274. //------------------------------------------------------------------------------
  275.  
  276. ODContainer* ContainerListIterator::Previous()
  277. {
  278.     ContainerLink* link = (ContainerLink*) fIterator.Previous();
  279.     if (link == kODNULL)
  280.         return kODNULL;
  281.     return link->fContainer;
  282. }
  283.         
  284. //------------------------------------------------------------------------------
  285. // ContainerListIterator::IsNotComplete
  286. //------------------------------------------------------------------------------
  287.  
  288. ODBoolean ContainerListIterator::IsNotComplete()
  289. {
  290.     return fIterator.IsNotComplete();
  291. }
  292.  
  293.