1 // Created on: 2002-04-12
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_IncAllocator_HeaderFile
17 #define NCollection_IncAllocator_HeaderFile
19 #include <NCollection_BaseAllocator.hxx>
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
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.
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
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()).
42 class NCollection_IncAllocator : public NCollection_BaseAllocator
45 // The type defining the alignement of allocated objects
46 typedef void * aligned_t;
48 // ---------- PUBLIC METHODS ----------
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);
55 //! Setup mutex for thread-safe allocations.
56 Standard_EXPORT void SetThreadSafe (bool theIsThreadSafe = true);
58 //! Allocate memory with given size. Returns NULL on failure
59 Standard_EXPORT virtual void* Allocate (const size_t size) Standard_OVERRIDE;
61 //! Free a previously allocated memory. Does nothing
62 Standard_EXPORT virtual void Free (void *anAddress) Standard_OVERRIDE;
64 //! Diagnostic method, returns the total allocated size
65 Standard_EXPORT size_t GetMemSize () const;
67 //! Destructor (calls Clean() internally)
68 Standard_EXPORT ~NCollection_IncAllocator ();
70 //! Reallocation: it is always allowed but is only efficient with the
71 //! last allocated item
72 Standard_EXPORT void * Reallocate (void * anAddress,
74 const size_t newSize);
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);
86 static const size_t DefaultBlockSize = 24600;
91 //! Flush all previously allocated data. All pointers returned by
92 //! Allocate() become invalid -- be very careful with this
93 Standard_EXPORT void Clean ();
95 //! Allocate a new block and return a pointer to it
96 //! ** only for internal usage **
97 void * allocateNewBlock (const size_t cSize);
100 // Prohibited methods
101 NCollection_IncAllocator (const NCollection_IncAllocator&);
102 NCollection_IncAllocator& operator = (const NCollection_IncAllocator&);
105 // ----- PROTECTED CLASS IBlock -------
107 aligned_t * allocateInBlock (const size_t cSize)
109 aligned_t * aResult = p_free_space;
110 p_free_space += cSize;
113 aligned_t * p_free_space;
114 aligned_t * p_end_block;
115 struct IBlock * p_next;
118 // --------- PROTECTED FIELDS ---------
119 Standard_Mutex* myMutex;
120 IBlock * myFirstBlock;
125 // Declaration of CASCADE RTTI
126 DEFINE_STANDARD_RTTIEXT(NCollection_IncAllocator,NCollection_BaseAllocator)
129 // Definition of HANDLE object using Standard_DefineHandle.hxx
130 DEFINE_STANDARD_HANDLE (NCollection_IncAllocator, NCollection_BaseAllocator)