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>
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
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.
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
36 class NCollection_IncAllocator : public NCollection_BaseAllocator
39 // The type defining the alignement of allocated objects
40 typedef void * aligned_t;
42 // ---------- PUBLIC METHODS ----------
45 Standard_EXPORT NCollection_IncAllocator (const size_t theBlockSize = DefaultBlockSize);
47 //! Allocate memory with given size. Returns NULL on failure
48 Standard_EXPORT virtual void* Allocate (const size_t size) Standard_OVERRIDE;
50 //! Free a previously allocated memory. Does nothing
51 Standard_EXPORT virtual void Free (void *anAddress) Standard_OVERRIDE;
53 //! Diagnostic method, returns the total allocated size
54 Standard_EXPORT size_t GetMemSize () const;
56 //! Destructor (calls Clean() internally)
57 Standard_EXPORT ~NCollection_IncAllocator ();
59 //! Reallocation: it is always allowed but is only efficient with the
60 //! last allocated item
61 Standard_EXPORT void * Reallocate (void * anAddress,
63 const size_t newSize);
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);
75 static const size_t DefaultBlockSize = 24600;
80 //! Flush all previously allocated data. All pointers returned by
81 //! Allocate() become invalid -- be very careful with this
82 Standard_EXPORT void Clean ();
84 //! Allocate a new block and return a pointer to it
85 //! ** only for internal usage **
86 void * allocateNewBlock (const size_t cSize);
90 NCollection_IncAllocator (const NCollection_IncAllocator&);
91 NCollection_IncAllocator& operator = (const NCollection_IncAllocator&);
94 // ----- PROTECTED CLASS IBlock -------
96 aligned_t * allocateInBlock (const size_t cSize)
98 aligned_t * aResult = p_free_space;
99 p_free_space += cSize;
102 aligned_t * p_free_space;
103 aligned_t * p_end_block;
104 struct IBlock * p_next;
107 // --------- PROTECTED FIELDS ---------
108 IBlock * myFirstBlock;
113 // Declaration of CASCADE RTTI
114 DEFINE_STANDARD_RTTIEXT(NCollection_IncAllocator,NCollection_BaseAllocator)
117 // Definition of HANDLE object using Standard_DefineHandle.hxx
118 DEFINE_STANDARD_HANDLE (NCollection_IncAllocator, NCollection_BaseAllocator)