-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from otavepto/patch/timed-alloc
attempt to fix some memory leaks
- Loading branch information
Showing
8 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <mutex> | ||
#include <forward_list> | ||
#include <utility> | ||
#include <algorithm> | ||
|
||
|
||
namespace common_helpers | ||
{ | ||
template<typename Ty> | ||
class ForgettableMemory { | ||
struct ForgettableBlock { | ||
Ty block; | ||
std::chrono::high_resolution_clock::time_point due_time; | ||
|
||
template<typename Rep, typename Period, class... Args> | ||
ForgettableBlock(std::chrono::duration<Rep, Period> duration, Args&&... args) | ||
: due_time(std::chrono::high_resolution_clock::now() + duration), | ||
block( std::forward<Args>(args)... ) | ||
{ } | ||
}; | ||
|
||
std::recursive_mutex mtx{}; | ||
std::forward_list<ForgettableBlock> storage{}; | ||
|
||
|
||
public: | ||
template<typename Rep, typename Period, class... Args> | ||
Ty& create(std::chrono::duration<Rep, Period> duration, Args&&... args) { | ||
std::lock_guard lock(mtx); | ||
|
||
auto& new_ele = storage.emplace_front(duration, std::forward<Args>(args)...); | ||
return new_ele.block; | ||
} | ||
|
||
bool is_alive(const Ty& block) { | ||
std::lock_guard lock(mtx); | ||
|
||
auto ele_it = std::find_if(storage.begin(), storage.end(), [&block](const ForgettableBlock &item){ | ||
return &item.block == █ | ||
}); | ||
return storage.end() != ele_it; | ||
} | ||
|
||
void destroy(const Ty& block) { | ||
std::lock_guard lock(mtx); | ||
|
||
storage.remove_if([&block](const ForgettableBlock &item){ | ||
return &item.block == █ | ||
}); | ||
} | ||
|
||
void destroy_all() { | ||
std::lock_guard lock(mtx); | ||
|
||
storage.clear(); | ||
} | ||
|
||
void cleanup() { | ||
std::lock_guard lock(mtx); | ||
|
||
const auto now = std::chrono::high_resolution_clock::now(); | ||
storage.remove_if([&now](const ForgettableBlock &item){ | ||
return now > item.due_time; | ||
}); | ||
|
||
} | ||
|
||
}; | ||
} |