global: implemented rudimentary memory manager to use available system memory
without forcing the OS to page swap
This commit is contained in:
@@ -26,6 +26,8 @@ set(OLIVE_SOURCES
|
||||
render/colorprocessor.cpp
|
||||
render/diskmanager.h
|
||||
render/diskmanager.cpp
|
||||
render/memorymanager.h
|
||||
render/memorymanager.cpp
|
||||
render/pixelformat.h
|
||||
render/pixelformat.cpp
|
||||
render/pixelservice.h
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "memorymanager.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#ifdef Q_OS_WINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
MemoryManager* MemoryManager::instance_ = nullptr;
|
||||
|
||||
const uint64_t MemoryManager::minimum_available_memory_ = 2147483648;
|
||||
|
||||
MemoryManager::MemoryManager(QObject* parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void MemoryManager::CreateInstance()
|
||||
{
|
||||
instance_ = new MemoryManager();
|
||||
}
|
||||
|
||||
MemoryManager *MemoryManager::instance()
|
||||
{
|
||||
return instance_;
|
||||
}
|
||||
|
||||
void MemoryManager::DestroyInstance()
|
||||
{
|
||||
delete instance_;
|
||||
instance_ = nullptr;
|
||||
}
|
||||
|
||||
bool MemoryManager::ShouldFreeMemory()
|
||||
{
|
||||
return (GetAvailableMemory() < minimum_available_memory_);
|
||||
}
|
||||
|
||||
void MemoryManager::ConsumedMemory()
|
||||
{
|
||||
if (ShouldFreeMemory()) {
|
||||
emit FreeMemory();
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t MemoryManager::GetAvailableMemory()
|
||||
{
|
||||
#ifdef Q_OS_WINDOWS
|
||||
MEMORYSTATUSEX status;
|
||||
status.dwLength = sizeof(status);
|
||||
GlobalMemoryStatusEx(&status);
|
||||
return status.ullAvailPhys;
|
||||
#else
|
||||
long pages = sysconf(_SC_PHYS_PAGES);
|
||||
long page_size = sysconf(_SC_PAGE_SIZE);
|
||||
return pages * page_size;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef MEMORYMANAGER_H
|
||||
#define MEMORYMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class MemoryManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MemoryManager(QObject* parent = nullptr);
|
||||
|
||||
static void CreateInstance();
|
||||
static MemoryManager* instance();
|
||||
static void DestroyInstance();
|
||||
|
||||
static bool ShouldFreeMemory();
|
||||
|
||||
public slots:
|
||||
void ConsumedMemory();
|
||||
|
||||
signals:
|
||||
void FreeMemory();
|
||||
|
||||
private:
|
||||
static uint64_t GetAvailableMemory();
|
||||
|
||||
static MemoryManager* instance_;
|
||||
|
||||
static const uint64_t minimum_available_memory_;
|
||||
|
||||
};
|
||||
|
||||
#endif // MEMORYMANAGER_H
|
||||
Reference in New Issue
Block a user