| 1 |
#ifndef _MMAP_ALLOCATOR_H |
| 2 |
#define _MMAP_ALLOCATOR_H |
| 3 |
|
| 4 |
#include <memory> |
| 5 |
#include <string> |
| 6 |
#include <stdio.h> |
| 7 |
#include <vector> |
| 8 |
#include "mmap_access_mode.h" |
| 9 |
#include "mmap_exception.h" |
| 10 |
#include "mmap_file_pool.h" |
| 11 |
|
| 12 |
namespace mmap_allocator_namespace |
| 13 |
{ |
| 14 |
template <typename T, typename A> |
| 15 |
class mmappable_vector; |
| 16 |
|
| 17 |
template <typename T> |
| 18 |
class mmap_allocator: public std::allocator<T> |
| 19 |
{ |
| 20 |
public: |
| 21 |
typedef size_t size_type; |
| 22 |
typedef off_t offset_type; |
| 23 |
typedef T* pointer; |
| 24 |
typedef const T* const_pointer; |
| 25 |
|
| 26 |
template<typename _Tp1> |
| 27 |
struct rebind |
| 28 |
{ |
| 29 |
typedef mmap_allocator<_Tp1> other; |
| 30 |
}; |
| 31 |
|
| 32 |
pointer allocate(size_type n, const void *hint=0) |
| 33 |
{ |
| 34 |
void *the_pointer; |
| 35 |
if (get_verbosity() > 0) { |
| 36 |
fprintf(stderr, "Alloc %zd bytes.\n", n*sizeof(T)); |
| 37 |
} |
| 38 |
if (access_mode == DEFAULT_STL_ALLOCATOR) { |
| 39 |
return std::allocator<T>::allocate(n, hint); |
| 40 |
} else { |
| 41 |
if (n == 0) { |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
if (bypass_file_pool) { |
| 45 |
the_pointer = private_file.open_and_mmap_file(filename, access_mode, offset, n*sizeof(T), map_whole_file, allow_remap); |
| 46 |
} else { |
| 47 |
the_pointer = the_pool.mmap_file(filename, access_mode, offset, n*sizeof(T), map_whole_file, allow_remap); |
| 48 |
} |
| 49 |
if (the_pointer == NULL) { |
| 50 |
throw(mmap_allocator_exception("Couldn't mmap file, mmap_file returned NULL")); |
| 51 |
} |
| 52 |
if (get_verbosity() > 0) { |
| 53 |
fprintf(stderr, "pointer = %p\n", the_pointer); |
| 54 |
} |
| 55 |
|
| 56 |
return (pointer)the_pointer; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void deallocate(pointer p, size_type n) |
| 61 |
{ |
| 62 |
if (get_verbosity() > 0) { |
| 63 |
fprintf(stderr, "Dealloc %zd bytes (%p).\n", n*sizeof(T), p); |
| 64 |
} |
| 65 |
if (access_mode == DEFAULT_STL_ALLOCATOR) { |
| 66 |
std::allocator<T>::deallocate(p, n); |
| 67 |
} else { |
| 68 |
if (n == 0) { |
| 69 |
return; |
| 70 |
} |
| 71 |
if (bypass_file_pool) { |
| 72 |
private_file.munmap_and_close_file(); |
| 73 |
} else { |
| 74 |
if (!keep_forever) { |
| 75 |
the_pool.munmap_file(filename, access_mode, offset, n*sizeof(T)); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
mmap_allocator() throw(): |
| 82 |
std::allocator<T>(), |
| 83 |
filename(""), |
| 84 |
offset(0), |
| 85 |
access_mode(DEFAULT_STL_ALLOCATOR), |
| 86 |
map_whole_file(false), |
| 87 |
allow_remap(false), |
| 88 |
bypass_file_pool(false), |
| 89 |
private_file(), |
| 90 |
keep_forever(false) |
| 91 |
{ } |
| 92 |
|
| 93 |
mmap_allocator(const std::allocator<T> &a) throw(): |
| 94 |
std::allocator<T>(a), |
| 95 |
filename(""), |
| 96 |
offset(0), |
| 97 |
access_mode(DEFAULT_STL_ALLOCATOR), |
| 98 |
map_whole_file(false), |
| 99 |
allow_remap(false), |
| 100 |
bypass_file_pool(false), |
| 101 |
private_file(), |
| 102 |
keep_forever(false) |
| 103 |
{ } |
| 104 |
|
| 105 |
mmap_allocator(const mmap_allocator &a) throw(): |
| 106 |
std::allocator<T>(a), |
| 107 |
filename(a.filename), |
| 108 |
offset(a.offset), |
| 109 |
access_mode(a.access_mode), |
| 110 |
map_whole_file(a.map_whole_file), |
| 111 |
allow_remap(a.allow_remap), |
| 112 |
bypass_file_pool(a.bypass_file_pool), |
| 113 |
private_file(a.private_file), |
| 114 |
keep_forever(a.keep_forever) |
| 115 |
{ } |
| 116 |
mmap_allocator(const std::string filename_param, enum access_mode access_mode_param = READ_ONLY, offset_type offset_param = 0, int flags = 0) throw(): |
| 117 |
std::allocator<T>(), |
| 118 |
filename(filename_param), |
| 119 |
offset(offset_param), |
| 120 |
access_mode(access_mode_param), |
| 121 |
map_whole_file((flags & MAP_WHOLE_FILE) != 0), |
| 122 |
allow_remap((flags & ALLOW_REMAP) != 0), |
| 123 |
bypass_file_pool((flags & BYPASS_FILE_POOL) != 0), |
| 124 |
private_file(), |
| 125 |
keep_forever((flags & KEEP_FOREVER) != 0) |
| 126 |
{ |
| 127 |
} |
| 128 |
|
| 129 |
~mmap_allocator() throw() { } |
| 130 |
|
| 131 |
private: |
| 132 |
friend class mmappable_vector<T, mmap_allocator<T> >; |
| 133 |
|
| 134 |
std::string filename; |
| 135 |
offset_type offset; |
| 136 |
enum access_mode access_mode; |
| 137 |
bool map_whole_file; |
| 138 |
bool allow_remap; |
| 139 |
bool bypass_file_pool; |
| 140 |
mmapped_file private_file; /* used if bypass is set */ |
| 141 |
bool keep_forever; |
| 142 |
}; |
| 143 |
} |
| 144 |
|
| 145 |
#endif /* _MMAP_ALLOCATOR_H */ |