1#include <common/mremap.h>
2
3#include <cstddef>
4#include <cstdlib>
5#include <cstring>
6#include <errno.h>
7
8
9void * mremap_fallback(
10 void * old_address, size_t old_size, size_t new_size, int flags, int mmap_prot, int mmap_flags, int mmap_fd, off_t mmap_offset)
11{
12 /// No actual shrink
13 if (new_size < old_size)
14 return old_address;
15
16 if (!(flags & MREMAP_MAYMOVE))
17 {
18 errno = ENOMEM;
19 return MAP_FAILED;
20 }
21
22#if _MSC_VER
23 void * new_address = ::operator new(new_size);
24#else
25 void * new_address = mmap(nullptr, new_size, mmap_prot, mmap_flags, mmap_fd, mmap_offset);
26 if (MAP_FAILED == new_address)
27 return MAP_FAILED;
28#endif
29
30 memcpy(new_address, old_address, old_size);
31
32#if _MSC_VER
33 delete old_address;
34#else
35 if (munmap(old_address, old_size))
36 abort();
37#endif
38
39 return new_address;
40}
41