aboutsummaryrefslogtreecommitdiff
path: root/lib/thread.cc
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2020-10-22 20:13:38 +0200
committerjohannst <johannes.stoelp@gmail.com>2020-10-22 20:13:38 +0200
commit1121f5a8b93016992e122150ea3c867204ab407b (patch)
treeeea70053287689f266428fc9ed2c2e425079078b /lib/thread.cc
parent3b4a6097dee760d22eecd97ebe041c69da6b056a (diff)
downloadmatcha-threads-1121f5a8b93016992e122150ea3c867204ab407b.tar.gz
matcha-threads-1121f5a8b93016992e122150ea3c867204ab407b.zip
fix memory leak when destroying Thread objectsfix-mem-leak-stack
Diffstat (limited to 'lib/thread.cc')
-rw-r--r--lib/thread.cc16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/thread.cc b/lib/thread.cc
index 424041d..304091d 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -18,7 +18,7 @@ namespace {
} // namespace
namespace nMatcha {
- Thread::Thread() : mStackPtr(nullptr), mFinished(false), mExecutor(nullptr) {
+ Thread::Thread() : mStack{}, mFinished(false), mExecutor(nullptr) {
const long PAGE_SIZE = get_pagesize();
const long STACK_SIZE = 8 * PAGE_SIZE;
@@ -35,10 +35,18 @@ namespace nMatcha {
assert(ret == 0);
// Adjust stack pointer, as stack grows downwards.
- mStackPtr = static_cast<uint8_t*>(stack) + STACK_SIZE;
+ mStack.mPtr = static_cast<uint8_t*>(stack) + STACK_SIZE;
+ mStack.mBottom = stack;
+ mStack.mSize= STACK_SIZE;
+
// Arch specific stack initialization.
- mStackPtr = init_stack(mStackPtr, Thread::entry, static_cast<void*>(this));
+ mStack.mPtr = init_stack(mStack.mPtr, Thread::entry, static_cast<void*>(this));
+ }
+
+ Thread::~Thread() {
+ int ret = ::munmap(mStack.mBottom, mStack.mSize);
+ assert(ret == 0);
}
bool Thread::isFinished() const {
@@ -60,7 +68,7 @@ namespace nMatcha {
void Thread::yield() {
assert(mExecutor);
- ::yield(mExecutor->getStackPtr(), &mStackPtr);
+ ::yield(mExecutor->getStackPtr(), &mStack.mPtr);
}
std::unique_ptr<Thread> FnThread::make(UserFn f) {