posix: wrap mmap() for anonymous maps

Automatically munmap() when the result is destroyed.
This commit is contained in:
Avi Kivity
2014-10-05 18:04:18 +03:00
parent 1cd888c4a9
commit 08706fcc6d
2 changed files with 20 additions and 0 deletions

View File

@@ -3,6 +3,17 @@
*/
#include "posix.hh"
#include <sys/mman.h>
void mmap_deleter::operator()(void* ptr) const {
::munmap(ptr, _size);
}
mmap_area mmap_anonymous(void* addr, size_t length, int prot, int flags) {
auto ret = ::mmap(addr, length, prot, flags | MAP_ANONYMOUS, -1, 0);
throw_system_error_on(ret == MAP_FAILED);
return mmap_area(reinterpret_cast<char*>(ret), mmap_deleter{length});
}
void* posix_thread::start_routine(void* arg) {
auto pfunc = reinterpret_cast<std::function<void ()>*>(arg);

View File

@@ -185,6 +185,15 @@ private:
file_desc(int fd) : _fd(fd) {}
};
struct mmap_deleter {
size_t _size;
void operator()(void* ptr) const;
};
using mmap_area = std::unique_ptr<char[], mmap_deleter>;
mmap_area mmap_anonymous(void* addr, size_t length, int prot, int flags);
class posix_thread {
// must allocate, since this class is moveable
std::unique_ptr<std::function<void ()>> _func;