CVC3
2.4.1
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
src
include
memory_manager_chunks.h
Go to the documentation of this file.
1
/*****************************************************************************/
2
/*!
3
* \file memory_manager_chunks.h
4
*
5
* Author: Sergey Berezin
6
*
7
* Created: Tue Apr 19 14:30:36 2005
8
*
9
* <hr>
10
*
11
* License to use, copy, modify, sell and/or distribute this software
12
* and its documentation for any purpose is hereby granted without
13
* royalty, subject to the terms and conditions defined in the \ref
14
* LICENSE file provided with this distribution.
15
*
16
* <hr>
17
*
18
* Class MemoryManager: allocates/deallocates memory for objects of a
19
* fixed size (the size is a parameter to the constructor). The
20
* actual memory is allocated in big chunks, which (at the moment) are
21
* never released back. However, the deallocated blocks are later reused.
22
*
23
* Typical use of this class is to create
24
* MemoryManager* mm = new MemoryManager(sizeof(YourClass));
25
* where YourClass has operators new and delete redefined:
26
* void* YourClass::operator new(size_t, MemoryManager* mm)
27
* { return mm->newData(); }
28
* void YourClass::delete(void*) { } // do not deallocate memory here
29
* Then, create objects with obj = new(mm) YourClass(), and destroy them with
30
* delete obj; mm->deleteData(obj);
31
*/
32
/*****************************************************************************/
33
34
#ifndef _cvc3__memory_manager_chunks_h
35
#define _cvc3__memory_manager_chunks_h
36
37
#include <vector>
38
#include "
memory_manager.h
"
39
40
namespace
CVC3 {
41
42
class
MemoryManagerChunks
:
public
MemoryManager
{
43
private
:
44
unsigned
d_dataSize
;
// #bytes in each data element
45
unsigned
d_chunkSize
;
// number of data elements
46
unsigned
d_chunkSizeBytes
;
// #bytes in each chunk
47
std::vector<char*>
d_freeList
;
48
std::vector<char*>
d_chunkList
;
// Pointers to the beginning of each chunk
49
// Pointer to the next free block of memory in the current chunk
50
char
*
d_nextFree
;
51
// End of current chunk (1 byte off the end)
52
char
*
d_endChunk
;
53
54
// Private methods
55
void
newChunk
() {
// Allocate new chunk
56
d_nextFree
= (
char
*)malloc(
d_chunkSizeBytes
);
57
FatalAssert
(
d_nextFree
!= NULL,
"Out of memory"
);
58
d_endChunk
=
d_nextFree
+
d_chunkSizeBytes
;
59
d_chunkList
.push_back(
d_nextFree
);
60
}
61
62
public
:
63
// Constructor
64
MemoryManagerChunks
(
unsigned
dataSize,
unsigned
chunkSize = 1024)
65
:
d_dataSize
(dataSize),
d_chunkSize
(chunkSize),
66
d_chunkSizeBytes
(dataSize*chunkSize),
67
d_nextFree
(NULL),
d_endChunk
(NULL) { }
68
// Destructor
69
~MemoryManagerChunks
() {
70
while
(
d_chunkList
.size() > 0) {
71
free(
d_chunkList
.back());
72
d_chunkList
.pop_back();
73
}
74
}
75
76
void
*
newData
(
size_t
size) {
77
DebugAssert
(size ==
d_dataSize
,
78
"MemoryManager::newData: the data size doesn't match"
);
79
void
* res;
80
// Check the free list first
81
if
(
d_freeList
.size() > 0) {
82
res = (
void
*)
d_freeList
.back();
83
d_freeList
.pop_back();
84
return
res;
85
}
86
if
(
d_nextFree
== NULL ||
d_nextFree
==
d_endChunk
)
87
newChunk
();
88
res = (
void
*)
d_nextFree
;
89
d_nextFree
+=
d_dataSize
;
90
return
res;
91
}
92
93
void
deleteData
(
void
* d) {
94
d_freeList
.push_back((
char
*)d);
95
}
96
};
// end of class MemoryManager
97
98
}
99
100
#endif
Generated on Sat Aug 3 2013 07:58:28 for CVC3 by
1.8.4