File: mmap_allocator.h

package info (click to toggle)
tilemaker 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,488 kB
  • sloc: cpp: 29,461; ansic: 12,510; makefile: 229; ruby: 77; sh: 43
file content (63 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef _MMAP_ALLOCATOR_H
#define _MMAP_ALLOCATOR_H

#include <cstddef>
#include <sstream>

class void_mmap_allocator
{
public:
	typedef std::size_t size_type;

	static void *allocate(size_type n, const void *hint = 0);
	static void reportStoreSize(std::ostringstream &str);
	static void openMmapFile(const std::string& mmapFilename);
};

template<typename T>
class mmap_allocator
{
public:
	typedef std::size_t size_type;
	typedef std::ptrdiff_t difference_type;
	typedef T *pointer;
	typedef const T *const_pointer;
	typedef const T &const_reference;
	typedef T value_type;

	template <class U>
	struct rebind
	{
		typedef mmap_allocator<U> other;
	};

	mmap_allocator() = default;

	template<typename OtherT>
	mmap_allocator(OtherT &)
	{ }

	pointer allocate(size_type n, const void *hint = 0)
	{
		return reinterpret_cast<T *>(void_mmap_allocator::allocate(n * sizeof(T), hint));
	}

	void deallocate(pointer p, size_type n)
	{
		// This is a no-op. Most usage of tilemaker should never deallocate
		// an mmap_allocator resource. On program termination, everything gets
		// freed.
	}

	void construct(pointer p, const_reference val)
	{
		new((void *)p) T(val);        
	}
};

template<typename T1, typename T2>
static inline bool operator==(mmap_allocator<T1> &, mmap_allocator<T2> &) { return true; }
template<typename T1, typename T2>
static inline bool operator!=(mmap_allocator<T1> &, mmap_allocator<T2> &) { return false; }

#endif