2021-10-02 07:13:14 +00:00
|
|
|
#include <base/mremap.h>
|
2019-03-30 10:47:25 +00:00
|
|
|
|
2017-11-16 19:17:09 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2022-05-08 17:01:47 +00:00
|
|
|
#include <cerrno>
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void * new_address = mmap(nullptr, new_size, mmap_prot, mmap_flags, mmap_fd, mmap_offset);
|
|
|
|
if (MAP_FAILED == new_address)
|
|
|
|
return MAP_FAILED;
|
|
|
|
|
|
|
|
memcpy(new_address, old_address, old_size);
|
|
|
|
|
|
|
|
if (munmap(old_address, old_size))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
return new_address;
|
|
|
|
}
|