KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVMemoryPool.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Fri Oct 1 16:50:02 2010
2 //Author: John Frankland,,,,
3 
4 #include "KVMemoryPool.h"
5 
7 
8 
9 
10 
13 KVMemoryPool::KVMemoryPool(int nchunks, size_t bytes)
14 {
15  // Create nchunks chunks each of size 'bytes'
16  fFirst = fLast = fLastChunkUsed = 0;
17  for (int i = 0; i < nchunks; i++) {
18  KVMemoryChunk* chunk = new KVMemoryChunk(bytes);
19  if (!fFirst) fFirst = chunk;
20  if (fLast) fLast->SetNext(chunk);
21  fLast = chunk;
22  }
23  fLastChunkUsed = fFirst;
24  fChunkSize = bytes;
25 }
26 
27 
28 
31 
32 void* KVMemoryPool::GetMemory(size_t bytes)
33 {
34  // return pointer to memory of size 'bytes'
35  void* p = fFirst->GetMemory(bytes);
37  if (!p) {
38  // search for first available chunk which can provide memory
39  do {
41  if (fLastChunkUsed) p = fLastChunkUsed->GetMemory(bytes);
42  }
43  while (!p && fLastChunkUsed);
44  }
45  if (!p) {
46  // there are no chunks big enough to provide memory
47  // add a bigger chunk
48  size_t new_chunk = fChunkSize;
49  while (new_chunk < bytes) new_chunk *= 2;
50  fChunkSize = new_chunk;
52  fLast->SetNext(chunk);
53  fLast = chunk;
54  fLastChunkUsed = chunk;
55  p = fLastChunkUsed->GetMemory(bytes);
56  }
57  return p;
58 }
59 
60 
61 
64 
66 {
67  // Destructor
68  KVMemoryChunk* p = fFirst;
69  KVMemoryChunk* next;
70  while (p) {
71  next = p->Next();
72  delete p;
73  p = next;
74  }
75 }
76 
77 
78 
80 
82 {
83  KVMemoryChunk* p = fFirst;
84  while (p) {
85  p->Print();
86  p = p->Next();
87  }
88 }
89 
90 
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
T1 fFirst
Memory handled by KVMemoryPool.
Definition: KVMemoryChunk.h:14
KVMemoryChunk * Next() const
Definition: KVMemoryChunk.h:31
void SetNext(KVMemoryChunk *n)
Definition: KVMemoryChunk.h:27
void * GetMemory(size_t)
Managed pool of memory.
Definition: KVMemoryPool.h:14
void * GetMemory(size_t bytes)
return pointer to memory of size 'bytes'
size_t fChunkSize
size of chunks in bytes
Definition: KVMemoryPool.h:18
KVMemoryChunk * fLastChunkUsed
Definition: KVMemoryPool.h:17
virtual ~KVMemoryPool()
Destructor.
KVMemoryChunk * fLast
first chunk in pool
Definition: KVMemoryPool.h:16
KVMemoryChunk * fFirst
first chunk in pool
Definition: KVMemoryPool.h:15