-
Notifications
You must be signed in to change notification settings - Fork 22
/
mmap_alloc.hpp
44 lines (39 loc) · 1.16 KB
/
mmap_alloc.hpp
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
#pragma once
#include <algorithm>
extern "C" {
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
}
size_t pagesize()
{
return ::getpagesize();
}
size_t page_count( size_t s )
{
return static_cast< size_t >( ceilf( static_cast< float >( s) / pagesize() ) );
}
char* mmap_alloc( size_t s, void* loc = 0 )
{
//fprintf( stderr, "mmap_alloc %llu %p\n", s, loc );
const std::size_t pages( page_count(s) ); // add +1 for guard page
std::size_t size_ = pages * pagesize();
# if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
void* limit = ::mmap( loc, size_, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
# else
const int fd( ::open("/dev/zero", O_RDONLY) );
assert( -1 != fd);
void* limit = ::mmap( loc, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
# endif
if ( !limit ) throw std::bad_alloc();
return static_cast<char*>(limit);
}
void mmap_free( void* pos, size_t s )
{
const std::size_t pages( page_count( s) ); // add +1 for guard page
std::size_t size_ = pages * pagesize();
::munmap( pos, size_);
}