kwave  18.07.70
MemoryManager.h
Go to the documentation of this file.
1 /***************************************************************************
2  MemoryManager.h - Manager for virtual and physical memory
3  -------------------
4  begin : Wed Aug 08 2001
5  copyright : (C) 2000 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef MEMORY_MANAGER_H
19 #define MEMORY_MANAGER_H
20 
21 #include "config.h"
22 #include <stddef.h> // for size_t
23 
24 #include <QtGlobal>
25 #include <QDir>
26 #include <QHash>
27 #include <QMutex>
28 #include <QString>
29 
30 #include "libkwave/LRU_Cache.h"
31 
32 namespace Kwave
33 {
34  class SwapFile;
35 
37  typedef int Handle;
38 
40  {
41  public:
43  MemoryManager();
44 
46  virtual ~MemoryManager();
47 
49  void close();
50 
60  Kwave::Handle allocate(size_t size) Q_DECL_EXPORT;
61 
71  bool resize(Kwave::Handle handle, size_t size) Q_DECL_EXPORT;
72 
77  size_t sizeOf(Kwave::Handle handle) Q_DECL_EXPORT;
78 
86  void free(Kwave::Handle &handle) Q_DECL_EXPORT;
87 
92  void setPhysicalLimit(quint64 mb) Q_DECL_EXPORT;
93 
98  void setVirtualLimit(quint64 mb) Q_DECL_EXPORT;
99 
104  void setSwapDirectory(const QString &dir) Q_DECL_EXPORT;
105 
110  void setUndoLimit(quint64 mb) Q_DECL_EXPORT;
111 
116  quint64 undoLimit() const Q_DECL_EXPORT;
117 
123  quint64 totalPhysical() Q_DECL_EXPORT;
124 
129  static MemoryManager &instance() Q_DECL_EXPORT;
130 
138  void *map(Kwave::Handle handle) Q_DECL_EXPORT;
139 
145  void unmap(Kwave::Handle handle) Q_DECL_EXPORT;
146 
157  int readFrom(Kwave::Handle handle, unsigned int offset,
158  void *buffer, unsigned int length) Q_DECL_EXPORT;
159 
170  int writeTo(Kwave::Handle handle, unsigned int offset,
171  const void *buffer, unsigned int length) Q_DECL_EXPORT;
172 
173 #ifdef DEBUG_MEMORY
174 
175  typedef struct {
177  struct {
178  quint64 handles;
179  quint64 bytes;
180  quint64 limit;
181  quint64 allocs;
182  quint64 frees;
183  } physical;
184 
186  struct {
188  struct {
189  quint64 handles;
190  quint64 bytes;
191  } mapped;
193  struct {
194  quint64 handles;
195  quint64 bytes;
196  } cached;
198  struct {
199  quint64 handles;
200  quint64 bytes;
201  } unmapped;
202  quint64 limit;
203  quint64 allocs;
204  quint64 frees;
205  } swap;
206  } statistic_t;
207 #endif
208 
209  protected:
210 
212  quint64 physicalUsed();
213 
215  quint64 virtualUsed();
216 
218  QString nextSwapFileName(Kwave::Handle handle);
219 
221  bool convertToVirtual(Kwave::Handle handle, size_t new_size);
222 
224  bool convertToPhysical(Kwave::Handle handle, size_t new_size);
225 
227  Kwave::Handle allocatePhysical(size_t size);
228 
230  void tryToMakePhysical(Kwave::Handle handle);
231 
238  Kwave::Handle allocateVirtual(size_t size);
239 
240  private:
241 
248 
256  bool freePhysical(size_t size);
257 
264  void unmapFromCache(Kwave::Handle handle);
265 
267  void dump(const char *function);
268 
269  private:
270 
271  typedef struct physical_memory_t {
272  void *m_data;
273  size_t m_size;
276 
277  private:
278 
281 
283  quint64 m_physical_max;
284 
287 
290 
292  quint64 m_undo_limit;
293 
296 
298  QHash<Kwave::Handle, Kwave::SwapFile *> m_unmapped_swap;
299 
301  QHash<Kwave::Handle, Kwave::SwapFile *> m_mapped_swap;
302 
308  QHash<Kwave::Handle, Kwave::SwapFile *> m_cached_swap;
309 
311  QMutex m_lock;
312 
315 
316 #ifdef DEBUG_MEMORY
317 
318  statistic_t m_stats;
319 #endif /* DEBUG_MEMORY */
320  };
321 
322 }
323 
324 #endif /* MEMORY_MANAGER_H */
325 
326 //***************************************************************************
327 //***************************************************************************
QHash< Kwave::Handle, Kwave::SwapFile * > m_unmapped_swap
void setVirtualLimit(quint64 mb) Q_DECL_EXPORT
quint64 totalPhysical() Q_DECL_EXPORT
Definition: App.h:33
int readFrom(Kwave::Handle handle, unsigned int offset, void *buffer, unsigned int length) Q_DECL_EXPORT
bool convertToVirtual(Kwave::Handle handle, size_t new_size)
bool resize(Kwave::Handle handle, size_t size) Q_DECL_EXPORT
size_t sizeOf(Kwave::Handle handle) Q_DECL_EXPORT
static MemoryManager & instance() Q_DECL_EXPORT
Kwave::Handle newHandle()
bool convertToPhysical(Kwave::Handle handle, size_t new_size)
bool freePhysical(size_t size)
QHash< Kwave::Handle, Kwave::SwapFile * > m_mapped_swap
void setUndoLimit(quint64 mb) Q_DECL_EXPORT
void tryToMakePhysical(Kwave::Handle handle)
void free(Kwave::Handle &handle) Q_DECL_EXPORT
void unmapFromCache(Kwave::Handle handle)
int Handle
Definition: MemoryManager.h:34
void setSwapDirectory(const QString &dir) Q_DECL_EXPORT
QHash< Kwave::Handle, Kwave::SwapFile * > m_cached_swap
struct Kwave::MemoryManager::physical_memory_t physical_memory_t
void setPhysicalLimit(quint64 mb) Q_DECL_EXPORT
QString nextSwapFileName(Kwave::Handle handle)
Kwave::Handle allocatePhysical(size_t size)
quint64 undoLimit() const Q_DECL_EXPORT
static Kwave::Handle m_last_handle
void unmap(Kwave::Handle handle) Q_DECL_EXPORT
Kwave::Handle allocateVirtual(size_t size)
void * map(Kwave::Handle handle) Q_DECL_EXPORT
int writeTo(Kwave::Handle handle, unsigned int offset, const void *buffer, unsigned int length) Q_DECL_EXPORT
Kwave::Handle allocate(size_t size) Q_DECL_EXPORT
Kwave::LRU_Cache< Kwave::Handle, physical_memory_t > m_physical
void dump(const char *function)