NGSolve  5.3
blockalloc.hpp
1 #ifndef FILE_BLOCKALLOC
2 #define FILE_BLOCKALLOC
3 
4 /**************************************************************************/
5 /* File: blockalloc.hpp */
6 /* Author: Joachim Schoeberl */
7 /* Date: 19. Apr. 2000 */
8 /**************************************************************************/
9 
10 #include <ngstd.hpp>
11 
12 namespace ngstd
13 {
14 
21 {
23  unsigned int size;
25  unsigned int blocks;
27  void * freelist;
29  Array<char*> bablocks;
31  int nels;
32 public:
34  NGS_DLL_HEADER BlockAllocator (unsigned int asize, unsigned int ablocks = 100);
36  NGS_DLL_HEADER ~BlockAllocator ();
37 
39  NGS_DLL_HEADER void * Alloc ();
40  /*
41  {
42  nels++;
43  if (!freelist)
44  Alloc2 ();
45 
46  void * p = freelist;
47  freelist = *(void**)freelist;
48  return p;
49  }
50  */
51 
52 
54  NGS_DLL_HEADER void Free (void * p);
55  /*
56  {
57  nels--;
58  *(void**)p = freelist;
59  freelist = p;
60  }
61  */
62 
64  int NumElements () { return nels; }
65 
66  NGS_DLL_HEADER void Print (ostream * ost) const;
67 private:
68  NGS_DLL_HEADER void * Alloc2 ();
69 };
70 
71 
72 }
73 
74 INLINE void * operator new (size_t size, ngstd::BlockAllocator & ball)
75 {
76  return ball.Alloc();
77 }
78 
79 INLINE void operator delete (void * p, ngstd::BlockAllocator & ball)
80 {
81  ball.Free (p);
82 }
83 
84 
85 
86 #endif
NGS_DLL_HEADER void Free(void *p)
Send memory to free-list.
NGS_DLL_HEADER void * Alloc()
Return pointer to new element.
NGS_DLL_HEADER BlockAllocator(unsigned int asize, unsigned int ablocks=100)
Create BlockAllocator for elements of size asize.
Optimized memory handler.
Definition: blockalloc.hpp:20
int NumElements()
number of allocated elements
Definition: blockalloc.hpp:64
namespace for standard data types and algorithms.
Definition: ngstd.hpp:81
NGS_DLL_HEADER ~BlockAllocator()
Delete all memeory.