File: vtkHeap.h

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (108 lines) | stat: -rw-r--r-- 3,688 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkHeap.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
// .NAME vtkHeap - replacement for malloc/free and new/delete
// .SECTION Description
// This class is a replacement for malloc/free and new/delete for software
// that has inherent memory leak or performance problems. For example,
// external software such as the PLY library (vtkPLY) and VRML importer
// (vtkVRMLImporter) are often written with lots of malloc() calls but
// without the corresponding free() invocations. The class
// vtkOrderedTriangulator may create and delete millions of new/delete calls.
// This class allows the overloading of the C++ new operator (or other memory
// allocation requests) by using the method AllocateMemory(). Memory is
// deleted with an invocation of CleanAll() (which deletes ALL memory; any
// given memory allocation cannot be deleted). Note: a block size can be used
// to control the size of each memory allocation. Requests for memory are
// fulfilled from the block until the block runs out, then a new block is
// created.
//
// .SECTION Caveats
// Do not use this class as a general replacement for system memory
// allocation.  This class should be used only as a last resort if memory
// leaks cannot be tracked down and eliminated by conventional means. Also,
// deleting memory from vtkHeap is not supported. Only the deletion of
// the entire heap is. (A Reset() method allows you to reuse previously
// allocated memory.)

// .SECTION See Also
// vtkVRMLImporter vtkPLY vtkOrderedTriangulator

#ifndef __vtkHeap_h
#define __vtkHeap_h

#include "vtkObject.h"

class vtkHeapBlock; //forward declaration

class VTK_COMMON_EXPORT vtkHeap : public vtkObject
{
public:
  static vtkHeap *New();
  vtkTypeMacro(vtkHeap,vtkObject);
  void PrintSelf(ostream& os, vtkIndent indent);

  // Description:
  // Allocate the memory requested.
  void* AllocateMemory(size_t n);
  
  // Description:
  // Set/Get the size at which blocks are allocated. If a memory
  // request is bigger than the block size, then that size
  // will be allocated.
  virtual void SetBlockSize(size_t);
  virtual size_t GetBlockSize() { return this->BlockSize;};

  // Description:
  // Get the number of allocations thus far.
  vtkGetMacro(NumberOfBlocks,int);
  vtkGetMacro(NumberOfAllocations,int);

  // Description:
  // This methods resets the current allocation location
  // back to the beginning of the heap. This allows
  // reuse of previously allocated memory which may be
  // beneficial to performance in many cases.
  void Reset();
  
  // Description:
  // Convenience method performs string duplication.
  char* StringDup(const char* str);

protected:  
  vtkHeap();
  ~vtkHeap();

  void Add(size_t blockSize);
  void CleanAll();
  vtkHeapBlock* DeleteAndNext();

  size_t BlockSize;
  int    NumberOfAllocations;
  int    NumberOfBlocks;
  int    Alignment;

  // Manage the blocks
  vtkHeapBlock* First;
  vtkHeapBlock* Last;
  vtkHeapBlock* Current;
  // Manage the memory in the block
  size_t Position; //the position in the Current block
  
private:
  vtkHeap(const vtkHeap&); // Not implemented.
  void operator=(const vtkHeap&);  // Not implemented.
};

#endif