VirtualMemory.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2008 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/genutil/VirtualMemory.h
10 
11 #ifndef __VirtualMemory_h
12 #define __VirtualMemory_h
13 
14 #include "TemporaryFile.h"
15 #include "ThreadContext.h"
16 
17 #include <map>
18 #include <inttypes.h>
19 
22 {
23  public:
24 
26  VirtualMemory (const std::string& filename, bool unlink = true);
27 
29  ~VirtualMemory ();
30 
32  template<typename T>
33  T* create (unsigned elements)
34  { return reinterpret_cast<T*>( mmap (elements * sizeof(T)) ); }
35 
37  void destroy (void* pointer)
38  { munmap (pointer); }
39 
40  private:
41 
43  void* mmap (uint64_t length);
44 
46  void munmap (void*);
47 
48  typedef std::map<char*, uint64_t> Map;
49  typedef Map::iterator Block;
50 
52  void add_allocated (char* ptr, uint64_t size);
53 
55  Block find_allocated (char* ptr);
56 
58  Block add_available (char* ptr, uint64_t size);
59 
61  Block find_available (uint64_t size);
62 
64  Map available;
65 
67  Map allocated;
68 
70  ThreadContext* context;
71 
73  Block extend (uint64_t size);
74 
76  uint64_t swap_space;
77 
79  void destroy ();
80 
81 };
82 
83 #endif
84 
~VirtualMemory()
Destructor.
Definition: VirtualMemory.C:50
Locks the mutex on construction and unlocks on deletion.
Definition: ThreadContext.h:56
int get_fd() const
Get the file descriptor of the temporary file.
Definition: TemporaryFile.h:49
Virtual memory manager.
Definition: VirtualMemory.h:21
Opens a temporary file with a unique name and deletes it when finished.
Definition: TemporaryFile.h:20
ThreadContext class.
Definition: ThreadContext.h:15
void unlink()
Unlink the temporary file.
Definition: TemporaryFile.C:176
void destroy(void *pointer)
Destroy an existing array.
Definition: VirtualMemory.h:42
T * create(unsigned elements)
Create a new array.
Definition: VirtualMemory.h:38
VirtualMemory(const std::string &filename, bool unlink=true)
Construct a virutal memory resource with the given filename.
Definition: VirtualMemory.C:26

Generated using doxygen 1.8.17