807263b65136f6eea485c5a2c5be9a625f84345c
[occt.git] / src / NCollection / NCollection_IncAllocator.hxx
1 // File:      NCollection_IncAllocator.hxx
2 // Created:   12.04.02 19:45:14
3 // Author:    Alexander GRIGORIEV
4 // Copyright: Open Cascade 2002
5
6
7 #ifndef NCollection_IncAllocator_HeaderFile
8 #define NCollection_IncAllocator_HeaderFile
9
10 #include <NCollection_BaseAllocator.hxx>
11
12 /**
13  *  Class NCollection_IncAllocator - incremental memory  allocator. This class
14  *  allocates  memory  on  request  returning  the  pointer  to  an  allocated
15  *  block. This memory is never returned  to the system until the allocator is
16  *  destroyed.
17  *  
18  *  By comparison with  the standard new() and malloc()  calls, this method is
19  *  faster and consumes very small additional memory to maintain the heap.
20  *  
21  *  All pointers  returned by Allocate() are  aligned to the size  of the data
22  *  type "aligned_t". To  modify the size of memory  blocks requested from the
23  *  OS,  use the parameter  of the  constructor (measured  in bytes);  if this
24  *  parameter is  smaller than  25 bytes on  32bit or  49 bytes on  64bit, the
25  *  block size will be the default 24 kbytes
26  */
27 class NCollection_IncAllocator : public NCollection_BaseAllocator
28 {
29  public:
30   // The type defining the alignement of allocated objects
31   typedef void * aligned_t;
32
33   // ---------- PUBLIC METHODS ----------
34
35   //! Constructor
36   Standard_EXPORT NCollection_IncAllocator (const size_t theBlockSize = 24600);
37
38   //! Allocate memory with given size. Returns NULL on failure
39   Standard_EXPORT virtual void* Allocate        (const size_t size);
40
41   //! Free a previously allocated memory. Does nothing
42   Standard_EXPORT virtual void  Free            (void *anAddress);
43
44   //! Diagnostic method, returns the total allocated size
45   Standard_EXPORT size_t        GetMemSize      () const;
46
47   //! Destructor (calls Clean() internally)
48   Standard_EXPORT ~NCollection_IncAllocator     ();
49
50   //! Reallocation: it is always allowed but is only efficient with the
51   //! last allocated item
52   Standard_EXPORT void *        Reallocate      (void * anAddress,
53                                                  const size_t oldSize,
54                                                  const size_t newSize);
55
56   //! Re-initialize the allocator so that the next Allocate call should
57   //! start allocating in the very begining as though the allocator is just
58   //! constructed. Warning: make sure that all previously allocated data are
59   //! no more used in your code!
60   //! @param doReleaseMem
61   //!   True - release all previously allocated memory, False - preserve it
62   //!   for future allocations.
63   Standard_EXPORT void          Reset           (const Standard_Boolean
64                                                  doReleaseMem=Standard_True);
65
66  protected:
67   struct         IBlock;
68
69   //! Flush all previously allocated data. All pointers returned by
70   //! Allocate() become invalid -- be very careful with this
71   Standard_EXPORT void  Clean                   ();
72
73   //! Allocate a new block and return a pointer to it
74   //! ** only for internal usage **
75   void *                allocateNewBlock        (const size_t cSize);
76
77  private:
78   // Prohibited methods
79   NCollection_IncAllocator (const NCollection_IncAllocator&);
80   NCollection_IncAllocator& operator = (const NCollection_IncAllocator&);
81
82  protected:
83   // ----- PROTECTED CLASS IBlock -------
84   struct IBlock {
85     aligned_t * allocateInBlock (const size_t cSize)
86     {
87       aligned_t * aResult = p_free_space;
88       p_free_space += cSize;
89       return aResult;
90     }
91     aligned_t     * p_free_space;
92     aligned_t     * p_end_block;
93     struct IBlock * p_next;
94   };
95  protected:
96   // --------- PROTECTED FIELDS ---------
97   IBlock        * myFirstBlock;
98   size_t        mySize;
99   size_t        myMemSize;
100
101  public:
102 // Declaration of CASCADE RTTI
103   DEFINE_STANDARD_RTTI (NCollection_IncAllocator)
104 };
105
106 // Definition of HANDLE object using Standard_DefineHandle.hxx
107 DEFINE_STANDARD_HANDLE (NCollection_IncAllocator, NCollection_BaseAllocator)
108
109
110 #endif