1 /*========================================================================= 2 3 Program: Visualization Toolkit 4 Module: vtkHeap.h 5 6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 7 All rights reserved. 8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 9 10 This software is distributed WITHOUT ANY WARRANTY; without even 11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 PURPOSE. See the above copyright notice for more information. 13 14 =========================================================================*/ 15 /** 16 * @class vtkHeap 17 * @brief replacement for malloc/free and new/delete 18 * 19 * This class is a replacement for malloc/free and new/delete for software 20 * that has inherent memory leak or performance problems. For example, 21 * external software such as the PLY library (vtkPLY) and VRML importer 22 * (vtkVRMLImporter) are often written with lots of malloc() calls but 23 * without the corresponding free() invocations. The class 24 * vtkOrderedTriangulator may create and delete millions of new/delete calls. 25 * This class allows the overloading of the C++ new operator (or other memory 26 * allocation requests) by using the method AllocateMemory(). Memory is 27 * deleted with an invocation of CleanAll() (which deletes ALL memory; any 28 * given memory allocation cannot be deleted). Note: a block size can be used 29 * to control the size of each memory allocation. Requests for memory are 30 * fulfilled from the block until the block runs out, then a new block is 31 * created. 32 * 33 * @warning 34 * Do not use this class as a general replacement for system memory 35 * allocation. This class should be used only as a last resort if memory 36 * leaks cannot be tracked down and eliminated by conventional means. Also, 37 * deleting memory from vtkHeap is not supported. Only the deletion of 38 * the entire heap is. (A Reset() method allows you to reuse previously 39 * allocated memory.) 40 * 41 * @sa 42 * vtkVRMLImporter vtkPLY vtkOrderedTriangulator 43 */ 44 45 #ifndef vtkHeap_h 46 #define vtkHeap_h 47 48 #include "vtkCommonMiscModule.h" // For export macro 49 #include "vtkObject.h" 50 51 class vtkHeapBlock; //forward declaration 52 53 class VTKCOMMONMISC_EXPORT vtkHeap : public vtkObject 54 { 55 public: 56 static vtkHeap *New(); 57 vtkTypeMacro(vtkHeap,vtkObject); 58 void PrintSelf(ostream& os, vtkIndent indent) override; 59 60 /** 61 * Allocate the memory requested. 62 */ 63 void* AllocateMemory(size_t n); 64 65 //@{ 66 /** 67 * Set/Get the size at which blocks are allocated. If a memory 68 * request is bigger than the block size, then that size 69 * will be allocated. 70 */ 71 virtual void SetBlockSize(size_t); GetBlockSize()72 virtual size_t GetBlockSize() { return this->BlockSize;}; 73 //@} 74 75 //@{ 76 /** 77 * Get the number of allocations thus far. 78 */ 79 vtkGetMacro(NumberOfBlocks,int); 80 vtkGetMacro(NumberOfAllocations,int); 81 //@} 82 83 /** 84 * This methods resets the current allocation location 85 * back to the beginning of the heap. This allows 86 * reuse of previously allocated memory which may be 87 * beneficial to performance in many cases. 88 */ 89 void Reset(); 90 91 /** 92 * Convenience method performs string duplication. 93 */ 94 char* StringDup(const char* str); 95 96 protected: 97 vtkHeap(); 98 ~vtkHeap() override; 99 100 void Add(size_t blockSize); 101 void CleanAll(); 102 vtkHeapBlock* DeleteAndNext(); 103 104 size_t BlockSize; 105 int NumberOfAllocations; 106 int NumberOfBlocks; 107 size_t Alignment; 108 109 // Manage the blocks 110 vtkHeapBlock* First; 111 vtkHeapBlock* Last; 112 vtkHeapBlock* Current; 113 // Manage the memory in the block 114 size_t Position; //the position in the Current block 115 116 private: 117 vtkHeap(const vtkHeap&) = delete; 118 void operator=(const vtkHeap&) = delete; 119 }; 120 121 #endif 122