1 // File: NCollection_IncAllocator.hxx
2 // Created: 12.04.02 19:45:14
3 // Author: Alexander GRIGORIEV
4 // Copyright: Open Cascade 2002
7 #ifndef NCollection_IncAllocator_HeaderFile
8 #define NCollection_IncAllocator_HeaderFile
10 #include <NCollection_BaseAllocator.hxx>
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
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.
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
27 class NCollection_IncAllocator : public NCollection_BaseAllocator
30 // The type defining the alignement of allocated objects
31 typedef void * aligned_t;
33 // ---------- PUBLIC METHODS ----------
36 Standard_EXPORT NCollection_IncAllocator (const size_t theBlockSize = 24600);
38 //! Allocate memory with given size. Returns NULL on failure
39 Standard_EXPORT virtual void* Allocate (const size_t size);
41 //! Free a previously allocated memory. Does nothing
42 Standard_EXPORT virtual void Free (void *anAddress);
44 //! Diagnostic method, returns the total allocated size
45 Standard_EXPORT size_t GetMemSize () const;
47 //! Destructor (calls Clean() internally)
48 Standard_EXPORT ~NCollection_IncAllocator ();
50 //! Reallocation: it is always allowed but is only efficient with the
51 //! last allocated item
52 Standard_EXPORT void * Reallocate (void * anAddress,
54 const size_t newSize);
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);
69 //! Flush all previously allocated data. All pointers returned by
70 //! Allocate() become invalid -- be very careful with this
71 Standard_EXPORT void Clean ();
73 //! Allocate a new block and return a pointer to it
74 //! ** only for internal usage **
75 void * allocateNewBlock (const size_t cSize);
79 NCollection_IncAllocator (const NCollection_IncAllocator&);
80 NCollection_IncAllocator& operator = (const NCollection_IncAllocator&);
83 // ----- PROTECTED CLASS IBlock -------
85 aligned_t * allocateInBlock (const size_t cSize)
87 aligned_t * aResult = p_free_space;
88 p_free_space += cSize;
91 aligned_t * p_free_space;
92 aligned_t * p_end_block;
93 struct IBlock * p_next;
96 // --------- PROTECTED FIELDS ---------
97 IBlock * myFirstBlock;
102 // Declaration of CASCADE RTTI
103 DEFINE_STANDARD_RTTI (NCollection_IncAllocator)
106 // Definition of HANDLE object using Standard_DefineHandle.hxx
107 DEFINE_STANDARD_HANDLE (NCollection_IncAllocator, NCollection_BaseAllocator)