0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[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 class Standard_Mutex;
22
23 /**
24  *  Class NCollection_IncAllocator - incremental memory  allocator. This class
25  *  allocates  memory  on  request  returning  the  pointer  to  an  allocated
26  *  block. This memory is never returned  to the system until the allocator is
27  *  destroyed.
28  *
29  *  By comparison with  the standard new() and malloc()  calls, this method is
30  *  faster and consumes very small additional memory to maintain the heap.
31  *
32  *  All pointers  returned by Allocate() are  aligned to the size  of the data
33  *  type "aligned_t". To  modify the size of memory  blocks requested from the
34  *  OS,  use the parameter  of the  constructor (measured  in bytes);  if this
35  *  parameter is  smaller than  25 bytes on  32bit or  49 bytes on  64bit, the
36  *  block size will be the default 24 kbytes
37  *
38  *  Note that this allocator is most suitable for single-threaded algorithms
39  *  (consider creating dedicated allocators per working thread),
40  *  and thread-safety of allocations is DISABLED by default (see SetThreadSafe()).
41  */
42 class NCollection_IncAllocator : public NCollection_BaseAllocator
43 {
44  public:
45   // The type defining the alignement of allocated objects
46   typedef void * aligned_t;
47
48   // ---------- PUBLIC METHODS ----------
49
50   //! Constructor.
51   //! Note that this constructor does NOT setup mutex for using allocator concurrently from different threads,
52   //! see SetThreadSafe() method.
53   Standard_EXPORT NCollection_IncAllocator (size_t theBlockSize = DefaultBlockSize);
54
55   //! Setup mutex for thread-safe allocations.
56   Standard_EXPORT void SetThreadSafe (bool theIsThreadSafe = true);
57
58   //! Allocate memory with given size. Returns NULL on failure
59   Standard_EXPORT virtual void* Allocate        (const size_t size) Standard_OVERRIDE;
60
61   //! Free a previously allocated memory. Does nothing
62   Standard_EXPORT virtual void  Free            (void *anAddress) Standard_OVERRIDE;
63
64   //! Diagnostic method, returns the total allocated size
65   Standard_EXPORT size_t        GetMemSize      () const;
66
67   //! Destructor (calls Clean() internally)
68   Standard_EXPORT ~NCollection_IncAllocator     ();
69
70   //! Reallocation: it is always allowed but is only efficient with the
71   //! last allocated item
72   Standard_EXPORT void *        Reallocate      (void * anAddress,
73                                                  const size_t oldSize,
74                                                  const size_t newSize);
75
76   //! Re-initialize the allocator so that the next Allocate call should
77   //! start allocating in the very begining as though the allocator is just
78   //! constructed. Warning: make sure that all previously allocated data are
79   //! no more used in your code!
80   //! @param doReleaseMem
81   //!   True - release all previously allocated memory, False - preserve it
82   //!   for future allocations.
83   Standard_EXPORT void          Reset           (const Standard_Boolean
84                                                  doReleaseMem=Standard_True);
85
86   static const size_t DefaultBlockSize = 24600;
87
88  protected:
89   struct         IBlock;
90
91   //! Flush all previously allocated data. All pointers returned by
92   //! Allocate() become invalid -- be very careful with this
93   Standard_EXPORT void  Clean                   ();
94
95   //! Allocate a new block and return a pointer to it
96   //! ** only for internal usage **
97   void *                allocateNewBlock        (const size_t cSize);
98
99  private:
100   // Prohibited methods
101   NCollection_IncAllocator (const NCollection_IncAllocator&);
102   NCollection_IncAllocator& operator = (const NCollection_IncAllocator&);
103
104  protected:
105   // ----- PROTECTED CLASS IBlock -------
106   struct IBlock {
107     aligned_t * allocateInBlock (const size_t cSize)
108     {
109       aligned_t * aResult = p_free_space;
110       p_free_space += cSize;
111       return aResult;
112     }
113     aligned_t     * p_free_space;
114     aligned_t     * p_end_block;
115     struct IBlock * p_next;
116   };
117  protected:
118   // --------- PROTECTED FIELDS ---------
119   Standard_Mutex* myMutex;
120   IBlock        * myFirstBlock;
121   size_t        mySize;
122   size_t        myMemSize;
123
124  public:
125 // Declaration of CASCADE RTTI
126   DEFINE_STANDARD_RTTIEXT(NCollection_IncAllocator,NCollection_BaseAllocator)
127 };
128
129 // Definition of HANDLE object using Standard_DefineHandle.hxx
130 DEFINE_STANDARD_HANDLE (NCollection_IncAllocator, NCollection_BaseAllocator)
131
132 #endif