Skip to content

Commit b071819

Browse files
committed
added custom release callback
1 parent f8afbc4 commit b071819

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/MemoryBank.cxx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ MemoryBank::MemoryBank(std::string v_description){
1111
description=v_description;
1212
}
1313

14+
MemoryBank::MemoryBank(void* v_baseAddress, std::size_t v_size, ReleaseCallback v_callback, std::string v_description)
15+
:baseAddress(v_baseAddress), size(v_size), description(v_description), releaseCallback(v_callback)
16+
{
17+
}
18+
1419
MemoryBank::~MemoryBank(){
20+
if (releaseCallback!=nullptr) {
21+
releaseCallback();
22+
}
1523
}
1624

1725
void *MemoryBank::getBaseAddress() {

src/MemoryBank.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string>
55
#include <cstddef>
66
#include <memory>
7-
7+
#include <functional>
88

99
// a class to handle a big block of contiguous memory.
1010
// constructor/destructor to be overloaded for different types of support.
@@ -13,6 +13,10 @@
1313
class MemoryBank {
1414
public:
1515
MemoryBank(std::string description=""); // constructor
16+
17+
using ReleaseCallback = std::function<void(void)>;
18+
MemoryBank(void* baseAddress, std::size_t size, ReleaseCallback callback, std::string description); // constructor, given a memory chunk and mean to release it
19+
1620
virtual ~MemoryBank(); // destructor
1721

1822
void *getBaseAddress(); // get the (virtual) base address of this memory bank
@@ -25,6 +29,7 @@ class MemoryBank {
2529
void* baseAddress; // base address (virtual) of buffer
2630
std::size_t size; // size of buffer, in bytes
2731
std::string description; // description of the memory bank (type/sypport, etc)
32+
ReleaseCallback releaseCallback; // an optional user-callback to be called in destructor, when overloaded constructor has been used
2833
};
2934

3035

0 commit comments

Comments
 (0)