#ifndef WIN32
#include <sys/mman.h>
#endif
-#include <cstdio>
-#include <fcntl.h>
-#include <sys/stat.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
std::map<std::string, void*> calls;
#ifndef WIN32
-static int smpi_shared_malloc_bogusfile = -1;
-static int smpi_shared_malloc_bogusfile_huge_page = -1;
-static unsigned long smpi_shared_malloc_blocksize = 1UL << 20;
+int smpi_shared_malloc_bogusfile = -1;
+int smpi_shared_malloc_bogusfile_huge_page = -1;
+unsigned long smpi_shared_malloc_blocksize = 1UL << 20;
#endif
}
/* First reserve memory area */
- void* allocated_ptr = mmap(NULL, allocated_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ void* allocated_ptr = mmap(nullptr, allocated_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
xbt_assert(allocated_ptr != MAP_FAILED, "Failed to allocate %zuMiB of memory. Run \"sysctl vm.overcommit_memory=1\" as root "
"to allow big allocations.\n",
smpi_shared_malloc_bogusfile = mkstemp(name);
XBT_DEBUG("bogusfile : %s\n", name);
unlink(name);
- const char* dumb = new char[smpi_shared_malloc_blocksize](); // zero initialized
- ssize_t err = write(smpi_shared_malloc_bogusfile, dumb, smpi_shared_malloc_blocksize);
- if(err<0)
+ int err = ftruncate(smpi_shared_malloc_bogusfile, smpi_shared_malloc_blocksize);
+ if (err != 0)
xbt_die("Could not write bogus file for shared malloc");
- delete[] dumb;
}
int mmap_base_flag = MAP_FIXED | MAP_SHARED | MAP_POPULATE;
size_t stop_block_offset = ALIGN_DOWN(stop_offset, smpi_shared_malloc_blocksize);
for (size_t offset = start_block_offset; offset < stop_block_offset; offset += smpi_shared_malloc_blocksize) {
XBT_DEBUG("\t\tglobal shared allocation, mmap block offset %zx", offset);
- void* pos = (void*)((unsigned long)mem + offset);
+ void* pos = static_cast<char*>(mem) + offset;
const void* res = mmap(pos, smpi_shared_malloc_blocksize, PROT_READ | PROT_WRITE, mmap_flag, huge_fd, 0);
xbt_assert(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the "
"size of the mapped file using --cfg=smpi/shared-malloc-blocksize:newvalue (default 1048576) ? "
size_t low_page_stop_offset = (int64_t)start_block_offset < ALIGN_DOWN(stop_offset, PAGE_SIZE) ? start_block_offset : ALIGN_DOWN(stop_offset, PAGE_SIZE);
if(low_page_start_offset < low_page_stop_offset) {
XBT_DEBUG("\t\tglobal shared allocation, mmap block start");
- void* pos = (void*)((unsigned long)mem + low_page_start_offset);
+ void* pos = static_cast<char*>(mem) + low_page_start_offset;
const void* res = mmap(pos, low_page_stop_offset - low_page_start_offset, PROT_READ | PROT_WRITE,
mmap_base_flag, // not a full huge page
smpi_shared_malloc_bogusfile, 0);
XBT_DEBUG("\t\tglobal shared allocation, mmap block stop");
size_t high_page_stop_offset = stop_offset == size ? size : ALIGN_DOWN(stop_offset, PAGE_SIZE);
if(high_page_stop_offset > stop_block_offset) {
- void* pos = (void*)((unsigned long)mem + stop_block_offset);
+ void* pos = static_cast<char*>(mem) + stop_block_offset;
const void* res = mmap(pos, high_page_stop_offset - stop_block_offset, PROT_READ | PROT_WRITE,
mmap_base_flag, // not a full huge page
smpi_shared_malloc_bogusfile, 0);
shared_metadata_t newmeta;
//register metadata for memcpy avoidance
- shared_data_key_type* data = new shared_data_key_type;
+ auto* data = new shared_data_key_type;
data->second.fd = -1;
data->second.count = 1;
newmeta.size = size;
newmeta.allocated_ptr = allocated_ptr;
newmeta.allocated_size = allocated_size;
if(shared_block_offsets[0] > 0) {
- newmeta.private_blocks.push_back(std::make_pair(0, shared_block_offsets[0]));
+ newmeta.private_blocks.emplace_back(0, shared_block_offsets[0]);
}
int i_block;
for(i_block = 0; i_block < nb_shared_blocks-1; i_block ++) {
- newmeta.private_blocks.push_back(std::make_pair(shared_block_offsets[2*i_block+1], shared_block_offsets[2*i_block+2]));
+ newmeta.private_blocks.emplace_back(shared_block_offsets[2 * i_block + 1], shared_block_offsets[2 * i_block + 2]);
}
if(shared_block_offsets[2*i_block+1] < size) {
- newmeta.private_blocks.push_back(std::make_pair(shared_block_offsets[2*i_block+1], size));
+ newmeta.private_blocks.emplace_back(shared_block_offsets[2 * i_block + 1], size);
}
allocs_metadata[mem] = newmeta;