kwave
18.07.70
|
#include <MemoryManager.h>
Classes | |
struct | physical_memory_t |
Public Member Functions | |
MemoryManager () | |
virtual | ~MemoryManager () |
void | close () |
Kwave::Handle | allocate (size_t size) Q_DECL_EXPORT |
bool | resize (Kwave::Handle handle, size_t size) Q_DECL_EXPORT |
size_t | sizeOf (Kwave::Handle handle) Q_DECL_EXPORT |
void | free (Kwave::Handle &handle) Q_DECL_EXPORT |
void | setPhysicalLimit (quint64 mb) Q_DECL_EXPORT |
void | setVirtualLimit (quint64 mb) Q_DECL_EXPORT |
void | setSwapDirectory (const QString &dir) Q_DECL_EXPORT |
void | setUndoLimit (quint64 mb) Q_DECL_EXPORT |
quint64 | undoLimit () const Q_DECL_EXPORT |
quint64 | totalPhysical () Q_DECL_EXPORT |
void * | map (Kwave::Handle handle) Q_DECL_EXPORT |
void | unmap (Kwave::Handle handle) Q_DECL_EXPORT |
int | readFrom (Kwave::Handle handle, unsigned int offset, void *buffer, unsigned int length) Q_DECL_EXPORT |
int | writeTo (Kwave::Handle handle, unsigned int offset, const void *buffer, unsigned int length) Q_DECL_EXPORT |
Static Public Member Functions | |
static MemoryManager & | instance () Q_DECL_EXPORT |
Protected Member Functions | |
quint64 | physicalUsed () |
quint64 | virtualUsed () |
QString | nextSwapFileName (Kwave::Handle handle) |
bool | convertToVirtual (Kwave::Handle handle, size_t new_size) |
bool | convertToPhysical (Kwave::Handle handle, size_t new_size) |
Kwave::Handle | allocatePhysical (size_t size) |
void | tryToMakePhysical (Kwave::Handle handle) |
Kwave::Handle | allocateVirtual (size_t size) |
Private Types | |
typedef struct Kwave::MemoryManager::physical_memory_t | physical_memory_t |
Private Member Functions | |
Kwave::Handle | newHandle () |
bool | freePhysical (size_t size) |
void | unmapFromCache (Kwave::Handle handle) |
void | dump (const char *function) |
Private Attributes | |
quint64 | m_physical_limit |
quint64 | m_physical_max |
quint64 | m_virtual_limit |
QDir | m_swap_dir |
quint64 | m_undo_limit |
Kwave::LRU_Cache< Kwave::Handle, physical_memory_t > | m_physical |
QHash< Kwave::Handle, Kwave::SwapFile * > | m_unmapped_swap |
QHash< Kwave::Handle, Kwave::SwapFile * > | m_mapped_swap |
QHash< Kwave::Handle, Kwave::SwapFile * > | m_cached_swap |
QMutex | m_lock |
Static Private Attributes | |
static Kwave::Handle | m_last_handle = 0 |
Definition at line 39 of file MemoryManager.h.
|
private |
Kwave::MemoryManager::MemoryManager | ( | ) |
Constructor
Definition at line 56 of file MemoryManager.cpp.
References m_physical_limit, m_physical_max, and m_virtual_limit.
|
virtual |
Destructor
Definition at line 145 of file MemoryManager.cpp.
References close().
Kwave::Handle Kwave::MemoryManager::allocate | ( | size_t | size | ) |
Gets a block of memory, either in physical memory or in a swap file if there is not enough physical memory. If there is not enough memory at all, the return value will be a null pointer.
size | number of bytes to allocate |
Definition at line 291 of file MemoryManager.cpp.
References allocatePhysical(), allocateVirtual(), dump(), freePhysical(), m_lock, and Kwave::toUint().
Referenced by Kwave::Stripe::StripeStorage::StripeStorage(), and Kwave::MimeData::Buffer::writeData().
|
protected |
tries to allocate physical memory
Definition at line 321 of file MemoryManager.cpp.
References Kwave::MemoryManager::physical_memory_t::m_data, Kwave::MemoryManager::physical_memory_t::m_mapcount, m_physical, m_physical_limit, Kwave::MemoryManager::physical_memory_t::m_size, newHandle(), physicalUsed(), and totalPhysical().
Referenced by allocate(), and convertToPhysical().
|
protected |
Tries to allocate virtual memory
size | number of bytes to allocate |
Definition at line 396 of file MemoryManager.cpp.
References Kwave::SwapFile::allocate(), dump(), m_unmapped_swap, m_virtual_limit, newHandle(), nextSwapFileName(), Kwave::toUint(), and virtualUsed().
Referenced by allocate(), and convertToVirtual().
void Kwave::MemoryManager::close | ( | ) |
Closes the memory manager and does cleanups at program shutdown
Definition at line 151 of file MemoryManager.cpp.
References m_cached_swap, m_lock, m_mapped_swap, m_physical, and m_unmapped_swap.
Referenced by ~MemoryManager().
|
protected |
convert a swapfile into a physical memory block
Definition at line 489 of file MemoryManager.cpp.
References allocatePhysical(), dump(), Kwave::MemoryManager::physical_memory_t::m_data, m_physical, Kwave::MemoryManager::physical_memory_t::m_size, m_unmapped_swap, Kwave::SwapFile::read(), Kwave::SwapFile::size(), and Kwave::toUint().
Referenced by resize(), and tryToMakePhysical().
|
protected |
convert a physical memory block into a new larger pagefile
Definition at line 445 of file MemoryManager.cpp.
References allocateVirtual(), dump(), free(), Kwave::MemoryManager::physical_memory_t::m_data, m_physical, Kwave::MemoryManager::physical_memory_t::m_size, m_unmapped_swap, Kwave::toUint(), and Kwave::SwapFile::write().
Referenced by freePhysical(), and resize().
|
private |
dump current state (for debugging)
Definition at line 1042 of file MemoryManager.cpp.
References m_cached_swap, m_mapped_swap, m_physical, m_unmapped_swap, physicalUsed(), and virtualUsed().
Referenced by allocate(), allocateVirtual(), convertToPhysical(), convertToVirtual(), free(), resize(), and unmapFromCache().
void Kwave::MemoryManager::free | ( | Kwave::Handle & | handle | ) |
Frees a block of memory that has been previously allocated with the allocate() function.
handle | reference to the handle to the block to be freed. The handle will be set to zero afterwards. |
Definition at line 717 of file MemoryManager.cpp.
References dump(), m_lock, m_mapped_swap, m_physical, m_unmapped_swap, Kwave::SwapFile::mapCount(), Kwave::SwapFile::size(), unmap(), and unmapFromCache().
Referenced by Kwave::MimeData::Buffer::close(), convertToVirtual(), Kwave::MimeData::Buffer::mapToByteArray(), Kwave::Stripe::resizeStorage(), and Kwave::Stripe::StripeStorage::~StripeStorage().
|
private |
try to make some room in the physical memory area by kicking out the oldest entries to swap if possible.
size | number of bytes to free |
Definition at line 255 of file MemoryManager.cpp.
References convertToVirtual(), Kwave::MemoryManager::physical_memory_t::m_mapcount, m_physical, Kwave::MemoryManager::physical_memory_t::m_size, and Kwave::toUint().
Referenced by allocate(), resize(), and tryToMakePhysical().
|
static |
Returns the global instance of the memory manager from the KwaveApp.
Definition at line 165 of file MemoryManager.cpp.
References g_instance.
Referenced by Kwave::Stripe::append(), Kwave::MemoryPlugin::applySettings(), Kwave::MimeData::Buffer::close(), Kwave::SignalManager::freeUndoMemory(), Kwave::Stripe::StripeStorage::map(), Kwave::MimeData::Buffer::mapToByteArray(), Kwave::MemoryDialog::MemoryDialog(), Kwave::Stripe::overwrite(), Kwave::Stripe::read(), Kwave::MimeData::Buffer::readData(), Kwave::SignalManager::redo(), Kwave::SignalManager::registerUndoAction(), Kwave::Stripe::resizeStorage(), Kwave::Stripe::StripeStorage::StripeStorage(), Kwave::SignalManager::undo(), Kwave::Stripe::StripeStorage::unmap(), Kwave::MimeData::Buffer::writeData(), and Kwave::Stripe::StripeStorage::~StripeStorage().
void * Kwave::MemoryManager::map | ( | Kwave::Handle | handle | ) |
Map a portion of memory and return the physical address.
handle | handle of the object that identifies the memory block |
Definition at line 774 of file MemoryManager.cpp.
References Kwave::SwapFile::address(), m_cached_swap, m_lock, m_mapped_swap, m_physical, m_unmapped_swap, Kwave::SwapFile::map(), Kwave::SwapFile::mapCount(), Kwave::SwapFile::size(), tryToMakePhysical(), and unmapFromCache().
Referenced by Kwave::Stripe::StripeStorage::map(), Kwave::MimeData::Buffer::mapToByteArray(), and Kwave::Stripe::StripeStorage::StripeStorage().
|
private |
get a new handle.
Definition at line 228 of file MemoryManager.cpp.
References m_cached_swap, m_last_handle, m_mapped_swap, m_physical, and m_unmapped_swap.
Referenced by allocatePhysical(), and allocateVirtual().
|
protected |
returns a new swap file name
Definition at line 381 of file MemoryManager.cpp.
References _, and m_swap_dir.
Referenced by allocateVirtual().
|
protected |
returns the currently allocated physical memory
Definition at line 355 of file MemoryManager.cpp.
References m_physical, and Kwave::MemoryManager::physical_memory_t::m_size.
Referenced by allocatePhysical(), dump(), resize(), and tryToMakePhysical().
int Kwave::MemoryManager::readFrom | ( | Kwave::Handle | handle, |
unsigned int | offset, | ||
void * | buffer, | ||
unsigned int | length | ||
) |
Read from a memory block into a buffer
handle | handle of the object that identifies the memory block |
offset | offset within the object [bytes] |
buffer | pointer to a buffer that is to be filled |
length | number of bytes to read |
Definition at line 941 of file MemoryManager.cpp.
References Kwave::SwapFile::address(), m_cached_swap, m_lock, m_mapped_swap, m_physical, m_unmapped_swap, Kwave::SwapFile::mapCount(), MEMCPY, Kwave::SwapFile::read(), and tryToMakePhysical().
Referenced by Kwave::Stripe::read(), Kwave::MimeData::Buffer::readData(), and Kwave::Stripe::StripeStorage::StripeStorage().
bool Kwave::MemoryManager::resize | ( | Kwave::Handle | handle, |
size_t | size | ||
) |
Resizes a block of memory to a new size. If the block will no longer fit in physical memory, the block will be swapped out to a page file.
handle | handle of the existing block |
size | new size of the block in bytes |
Definition at line 575 of file MemoryManager.cpp.
References convertToPhysical(), convertToVirtual(), dump(), freePhysical(), Kwave::MemoryManager::physical_memory_t::m_data, m_lock, Kwave::MemoryManager::physical_memory_t::m_mapcount, m_mapped_swap, m_physical, m_physical_limit, Kwave::MemoryManager::physical_memory_t::m_size, m_unmapped_swap, m_virtual_limit, physicalUsed(), Kwave::SwapFile::resize(), Kwave::SwapFile::size(), Kwave::toUint(), and unmapFromCache().
Referenced by Kwave::MimeData::Buffer::writeData().
void Kwave::MemoryManager::setPhysicalLimit | ( | quint64 | mb | ) |
Sets the limit of physical memory that can be used.
mb | number of whole megabytes of the limit |
Definition at line 171 of file MemoryManager.cpp.
References m_lock, m_physical_limit, and totalPhysical().
Referenced by Kwave::MemoryPlugin::applySettings().
void Kwave::MemoryManager::setSwapDirectory | ( | const QString & | dir | ) |
Sets the directory where swap files should be stored
dir | directory |
Definition at line 199 of file MemoryManager.cpp.
References m_lock, and m_swap_dir.
Referenced by Kwave::MemoryPlugin::applySettings().
void Kwave::MemoryManager::setUndoLimit | ( | quint64 | mb | ) |
Sets the limit of memory that can be used for undo/redo.
mb | number of whole megabytes of the limit |
Definition at line 206 of file MemoryManager.cpp.
References m_lock, m_undo_limit, and totalPhysical().
Referenced by Kwave::MemoryPlugin::applySettings().
void Kwave::MemoryManager::setVirtualLimit | ( | quint64 | mb | ) |
Sets the limit of virtual memory that can be used.
mb | number of whole megabytes of the limit |
Definition at line 184 of file MemoryManager.cpp.
References m_lock, and m_virtual_limit.
Referenced by Kwave::MemoryPlugin::applySettings().
size_t Kwave::MemoryManager::sizeOf | ( | Kwave::Handle | handle | ) |
Returns the allocated size of the block
Definition at line 684 of file MemoryManager.cpp.
References m_cached_swap, m_lock, m_mapped_swap, m_physical, Kwave::MemoryManager::physical_memory_t::m_size, m_unmapped_swap, and Kwave::SwapFile::size().
Referenced by Kwave::MimeData::Buffer::writeData().
quint64 Kwave::MemoryManager::totalPhysical | ( | ) |
Returns the total amount of theoretically available physical memory, as the minimum of the totally installed memory and ulimit settings.
Definition at line 222 of file MemoryManager.cpp.
References m_physical_max.
Referenced by allocatePhysical(), Kwave::MemoryDialog::MemoryDialog(), setPhysicalLimit(), setUndoLimit(), and tryToMakePhysical().
|
protected |
tries to convert to physical RAM
Definition at line 543 of file MemoryManager.cpp.
References convertToPhysical(), freePhysical(), m_cached_swap, m_mapped_swap, m_physical, m_physical_limit, m_unmapped_swap, physicalUsed(), Kwave::SwapFile::size(), and totalPhysical().
Referenced by map(), readFrom(), and writeTo().
quint64 Kwave::MemoryManager::undoLimit | ( | ) | const |
Returns the limit of memory that can be used for undo/redo in units of whole megabytes
Definition at line 216 of file MemoryManager.cpp.
References m_undo_limit.
Referenced by Kwave::SignalManager::freeUndoMemory(), Kwave::SignalManager::redo(), Kwave::SignalManager::registerUndoAction(), and Kwave::SignalManager::undo().
void Kwave::MemoryManager::unmap | ( | Kwave::Handle | handle | ) |
Unmap a memory area, previously mapped with map()
handle | handle of a mapped block mapped with map() |
Definition at line 882 of file MemoryManager.cpp.
References CACHE_SIZE, m_cached_swap, m_lock, m_mapped_swap, m_physical, m_unmapped_swap, Kwave::SwapFile::mapCount(), Kwave::SwapFile::size(), Kwave::SwapFile::unmap(), and unmapFromCache().
Referenced by Kwave::MimeData::Buffer::close(), free(), Kwave::Stripe::StripeStorage::StripeStorage(), and Kwave::Stripe::StripeStorage::unmap().
|
private |
Makes sure that the object is not a swapfile in cache. If so, it will be unmapped and moved to the m_unmapped_swap list.
handle | handle of a block in m_cached_swap |
Definition at line 860 of file MemoryManager.cpp.
References dump(), m_cached_swap, m_unmapped_swap, Kwave::SwapFile::mapCount(), Kwave::SwapFile::size(), and Kwave::SwapFile::unmap().
Referenced by free(), map(), resize(), unmap(), and writeTo().
|
protected |
returns the currently allocated virtual memory
Definition at line 364 of file MemoryManager.cpp.
References m_cached_swap, m_mapped_swap, m_unmapped_swap, and Kwave::SwapFile::size().
Referenced by allocateVirtual(), and dump().
int Kwave::MemoryManager::writeTo | ( | Kwave::Handle | handle, |
unsigned int | offset, | ||
const void * | buffer, | ||
unsigned int | length | ||
) |
Write a buffer into a memory block
handle | handle of the object that identifies the memory block |
offset | offset within the object [bytes] |
buffer | pointer to a buffer that is to be written |
length | number of bytes to write |
Definition at line 998 of file MemoryManager.cpp.
References Kwave::MemoryManager::physical_memory_t::m_data, m_lock, m_mapped_swap, m_physical, Kwave::MemoryManager::physical_memory_t::m_size, m_unmapped_swap, MEMCPY, tryToMakePhysical(), unmapFromCache(), and Kwave::SwapFile::write().
Referenced by Kwave::Stripe::append(), Kwave::Stripe::overwrite(), Kwave::Stripe::StripeStorage::StripeStorage(), and Kwave::MimeData::Buffer::writeData().
|
private |
cache for swapfiles that have been recently used, are mapped and get unmapped if the queue is full. The queue will be used as a FIFO with fixed size.
Definition at line 308 of file MemoryManager.h.
Referenced by close(), dump(), map(), newHandle(), readFrom(), sizeOf(), tryToMakePhysical(), unmap(), unmapFromCache(), and virtualUsed().
|
staticprivate |
|
private |
mutex for ensuring exclusive access
Definition at line 311 of file MemoryManager.h.
Referenced by allocate(), close(), free(), map(), readFrom(), resize(), setPhysicalLimit(), setSwapDirectory(), setUndoLimit(), setVirtualLimit(), sizeOf(), unmap(), and writeTo().
|
private |
map of swapfile objects that are already mapped into memory
Definition at line 301 of file MemoryManager.h.
Referenced by close(), dump(), free(), map(), newHandle(), readFrom(), resize(), sizeOf(), tryToMakePhysical(), unmap(), virtualUsed(), and writeTo().
|
private |
map of objects in physical memory
Definition at line 295 of file MemoryManager.h.
Referenced by allocatePhysical(), close(), convertToPhysical(), convertToVirtual(), dump(), free(), freePhysical(), map(), newHandle(), physicalUsed(), readFrom(), resize(), sizeOf(), tryToMakePhysical(), unmap(), and writeTo().
|
private |
limit of the physical memory
Definition at line 280 of file MemoryManager.h.
Referenced by allocatePhysical(), MemoryManager(), resize(), setPhysicalLimit(), and tryToMakePhysical().
|
private |
total maximum physical memory (system dependent)
Definition at line 283 of file MemoryManager.h.
Referenced by MemoryManager(), and totalPhysical().
|
private |
path where to store swap files
Definition at line 289 of file MemoryManager.h.
Referenced by nextSwapFileName(), and setSwapDirectory().
|
private |
limit of memory available for undo/redo
Definition at line 292 of file MemoryManager.h.
Referenced by setUndoLimit(), and undoLimit().
|
private |
map of swapfile objects that are not mapped into memory
Definition at line 298 of file MemoryManager.h.
Referenced by allocateVirtual(), close(), convertToPhysical(), convertToVirtual(), dump(), free(), map(), newHandle(), readFrom(), resize(), sizeOf(), tryToMakePhysical(), unmap(), unmapFromCache(), virtualUsed(), and writeTo().
|
private |
limit of the virtual memory, 0 = disabled
Definition at line 286 of file MemoryManager.h.
Referenced by allocateVirtual(), MemoryManager(), resize(), and setVirtualLimit().