2018-03-23 16:05:14 +00:00
|
|
|
#include <common/mremap.h>
|
2019-03-30 10:47:25 +00:00
|
|
|
|
2017-11-16 19:17:09 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2017-11-17 09:50:09 +00:00
|
|
|
#include <errno.h>
|
2017-11-16 19:17:09 +00:00
|
|
|
|
|
|
|
|
2019-03-30 10:47:25 +00:00
|
|
|
void * mremap_fallback(
|
2017-11-16 19:17:09 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
/// No actual shrink
|
|
|
|
if (new_size < old_size)
|
|
|
|
return old_address;
|
|
|
|
|
|
|
|
if (!(flags & MREMAP_MAYMOVE))
|
|
|
|
{
|
2017-11-16 20:17:57 +00:00
|
|
|
errno = ENOMEM;
|
2019-03-30 10:47:25 +00:00
|
|
|
return MAP_FAILED;
|
2017-11-16 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:05:14 +00:00
|
|
|
#if _MSC_VER
|
|
|
|
void * new_address = ::operator new(new_size);
|
|
|
|
#else
|
2017-11-16 19:17:09 +00:00
|
|
|
void * new_address = mmap(nullptr, new_size, mmap_prot, mmap_flags, mmap_fd, mmap_offset);
|
|
|
|
if (MAP_FAILED == new_address)
|
|
|
|
return MAP_FAILED;
|
2018-03-23 16:05:14 +00:00
|
|
|
#endif
|
2017-11-16 19:17:09 +00:00
|
|
|
|
|
|
|
memcpy(new_address, old_address, old_size);
|
|
|
|
|
2018-03-23 16:05:14 +00:00
|
|
|
#if _MSC_VER
|
|
|
|
delete old_address;
|
|
|
|
#else
|
2017-11-16 19:17:09 +00:00
|
|
|
if (munmap(old_address, old_size))
|
|
|
|
abort();
|
2018-03-23 16:05:14 +00:00
|
|
|
#endif
|
2017-11-16 19:17:09 +00:00
|
|
|
|
|
|
|
return new_address;
|
|
|
|
}
|