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