Skip to content

Commit

Permalink
Extend resource lifetime to guarantee process handoff
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemanga committed Aug 27, 2024
1 parent 490ffc4 commit a0fb9ac
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions data/plugins/parsepng/settings.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[parsepng]
reserve-ram-max = 5 # max RAM in megabytes
speed = 1
runInBackground = 1
3 changes: 3 additions & 0 deletions plugins/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ extern "C" int _fstat(int fd, struct stat *st) {
return -1;
}

extern "C" int __ssputws_r (FILE *fp, const wchar_t *buf, size_t len) {return 0;}
extern "C" int _getentropy(void *buf, size_t buflen){return -1;}

extern "C" void __libc_init_array(void);
extern uint32_t __data_section_table;
extern uint32_t __data_section_table_end;
Expand Down
1 change: 1 addition & 0 deletions plugins/parsepng/settings.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[parsepng]
reserve-ram-max = 5 # max RAM in megabytes
speed = 1
runInBackground = 1
2 changes: 1 addition & 1 deletion src/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "GLComponentRenderData.hpp"
#include <cstdint>

#include <algorithm>
#include <fmt/format.h>

void GLRenderer::init(uint32_t major, uint32_t minor, const std::string profile) {
Expand Down
15 changes: 9 additions & 6 deletions src/Index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class Index {
static inline Index<Type>* _ptr;
};

inline Shared<std::vector<std::shared_ptr<void>>> heldResources;
inline auto heldResources = std::make_unique<Shared<std::vector<std::shared_ptr<void>>>>();
inline auto nextHeldResources = std::make_unique<Shared<std::vector<std::shared_ptr<void>>>>();

template<typename T>
class Y : public T {
Expand All @@ -89,13 +90,13 @@ class AutoIndex : public std::enable_shared_from_this<Derived> {
LOG("Creating ", typeid(Derived).name());
auto ptr = std::make_shared<Y<T>>(std::forward<Args>(args)...);
ptr->_key = Index<Derived>::_ptr->add(ptr);
heldResources.write([&](auto& heldResources){heldResources.push_back(ptr);});
heldResources->write()->push_back(ptr);
return ptr;
}

~AutoIndex() {
LOG("Destroying ", typeid(Derived).name());
if (_key)
if (_key && Index<Derived>::_ptr)
Index<Derived>::_ptr->remove(_key);
}

Expand All @@ -105,7 +106,9 @@ class AutoIndex : public std::enable_shared_from_this<Derived> {
};

inline void gc() {
heldResources.write([](auto& hr) {
hr.clear();
});
auto hr = heldResources->write();
auto nhr = nextHeldResources->write();
hr->clear();
hr->insert(hr->end(), nhr->begin(), nhr->end());
nhr->clear();
}
6 changes: 6 additions & 0 deletions src/VMImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ class VMImpl : public VM, public AutoIndex<VMImpl> {

public:
~VMImpl() {
mainThread([resources=std::move(*heldResources.write())]{
if (nextHeldResources) {
auto out = nextHeldResources->write();
out->insert(out->end(), resources.begin(), resources.end());
}
});
eventListeners.write([&](auto& eventListeners) {
for (std::size_t id = 0; id < static_cast<uint32_t>(EventId::MaxEvent); ++id) {
for (auto& slot : eventListeners[id]) {
Expand Down

0 comments on commit a0fb9ac

Please sign in to comment.