1 // Created on: 2001-06-26
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef LDOM_MemManager_HeaderFile
17 #define LDOM_MemManager_HeaderFile
18 
19 #include <Standard_Transient.hxx>
20 #include <Standard_Type.hxx>
21 
22 class LDOM_BasicElement;
23 class LDOM_MemManager;
24 class LDOMBasicString;
25 
26 // Define handle class for LDOM_MemManager
27 DEFINE_STANDARD_HANDLE (LDOM_MemManager, Standard_Transient)
28 
29 //  Class LDOM_MemManager (underlying structure of LDOM_Document)
30 //
31 
32 class LDOM_MemManager : public Standard_Transient
33 {
34  public:
35   // ---------- PUBLIC METHODS ----------
36 
37   Standard_EXPORT LDOM_MemManager       (const Standard_Integer aBlockSize);
38   // Constructor
39 
40   Standard_EXPORT ~LDOM_MemManager      ();
41   // Destructor
42 
43   Standard_EXPORT void * Allocate       (const Standard_Integer aSize);
44   // General Memory allocator
45 
46   const char *           HashedAllocate (const char             * aString,
47                                          const Standard_Integer theLen,
48                                          Standard_Integer&      theHash);
49   // Memory allocation with access via hash table. No new allocation
50   // if already present
51 
52   void                   HashedAllocate (const char             * aString,
53                                          const Standard_Integer theLen,
54                                          LDOMBasicString&      theResult);
55   // Memory allocation with access via hash table. No new allocation
56   // if already present
57 
Hash(const char * theString,const Standard_Integer theLen)58   static Standard_Integer Hash          (const char             * theString,
59                                          const Standard_Integer theLen)
60                                 { return HashTable::Hash (theString, theLen); }
61 
62   static Standard_Boolean CompareStrings(const char             * theString,
63                                          const Standard_Integer theHashValue,
64                                          const char             * theHashedStr);
65 
66 //  LDOM_Document           Doc           () const
67 //                                { return LDOM_Document (* this); }
68 
Self() const69   const LDOM_MemManager&  Self          () const
70                                 { return * this; }
71 
RootElement() const72   const LDOM_BasicElement * RootElement   () const
73                                 { return myRootElement; }
74 
75  private:
76   friend class LDOM_Document;
77   friend class LDOMParser;
78 
79   // ---- CLASS MemBlock ----
80   class MemBlock {
81     friend class LDOM_MemManager;
82     inline MemBlock         (const Standard_Integer aSize, MemBlock * aFirst);
83     inline void * Allocate  (const Standard_Integer aSize);
84     void * AllocateAndCheck (const Standard_Integer aSize, const MemBlock *&);
85     ~MemBlock               ();
Next()86     MemBlock * Next         ()           { return myNext; }
87 
88     Standard_Integer    mySize;
89     Standard_Integer    * myBlock;
90     Standard_Integer    * myEndBlock;
91     Standard_Integer    * myFreeSpace;
92     MemBlock            * myNext;
93   };
94 
95   // ---- CLASS HashTable ----
96   class HashTable {
97     friend class LDOM_MemManager;
98     HashTable                   (/* const Standard_Integer theMask, */
99                                  LDOM_MemManager&       theMemManager);
100     const char     * AddString  (const char             * theString,
101                                  const Standard_Integer theLen,
102                                  Standard_Integer&      theHashIndex);
103     static Standard_Integer Hash(const char             * theString,
104                                  const Standard_Integer theLen);
105     struct TableItem {
106       char             * str;
107       struct TableItem * next;
108     }                           * myTable;
109     LDOM_MemManager&            myManager;
110     void operator= (const HashTable&);
111   };
112 
113   // ---- PROHIBITED (PRIVATE) METHODS ----
114   LDOM_MemManager (const LDOM_MemManager& theOther);
115   // Copy constructor
116 
117   LDOM_MemManager& operator = (const LDOM_MemManager& theOther);
118   // Assignment
119 
120   // ---------- PRIVATE FIELDS ----------
121 
122   const LDOM_BasicElement * myRootElement;
123   MemBlock              * myFirstBlock;
124   MemBlock              * myFirstWithoutRoom;
125   Standard_Integer      myBlockSize;
126   HashTable             * myHashTable;
127 
128  public:
129   // CASCADE RTTI
130   DEFINE_STANDARD_RTTIEXT(LDOM_MemManager,Standard_Transient)
131 };
132 
133 
134 #endif
135