+// Align functions, from http://stackoverflow.com/questions/4840410/how-to-align-a-pointer-in-c
+#define PAGE_SIZE 0x1000
+#define ALIGN_UP(n, align) (((n) + (align)-1) & -(align))
+#define ALIGN_DOWN(n, align) ((n) & -(align))
+
+void *smpi_shared_malloc_global__(size_t size, const char *file, int line, int *shared_block_offsets, int nb_shared_blocks) {
+ void *mem;
+ xbt_assert(smpi_shared_malloc_blocksize % PAGE_SIZE == 0, "The block size of shared malloc should be a multiple of the page size.");
+ /* First reserve memory area */
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+
+ xbt_assert(mem != MAP_FAILED, "Failed to allocate %luMiB of memory. Run \"sysctl vm.overcommit_memory=1\" as root "
+ "to allow big allocations.\n",
+ (unsigned long)(size >> 20));
+
+ /* Create bogus file if not done already */
+ if (smpi_shared_malloc_bogusfile == -1) {
+ /* Create a fd to a new file on disk, make it smpi_shared_malloc_blocksize big, and unlink it.
+ * It still exists in memory but not in the file system (thus it cannot be leaked). */
+ smpi_shared_malloc_blocksize = static_cast<unsigned long>(xbt_cfg_get_double("smpi/shared-malloc-blocksize"));
+ XBT_DEBUG("global shared allocation. Blocksize %lu", smpi_shared_malloc_blocksize);
+ char* name = xbt_strdup("/tmp/simgrid-shmalloc-XXXXXX");
+ smpi_shared_malloc_bogusfile = mkstemp(name);
+ unlink(name);
+ xbt_free(name);
+ char* dumb = (char*)calloc(1, smpi_shared_malloc_blocksize);
+ ssize_t err = write(smpi_shared_malloc_bogusfile, dumb, smpi_shared_malloc_blocksize);
+ if(err<0)
+ xbt_die("Could not write bogus file for shared malloc");
+ xbt_free(dumb);
+ }
+
+ /* Map the bogus file in place of the anonymous memory */
+ for(int i_block = 0; i_block < nb_shared_blocks; i_block ++) {
+ int start_offset = shared_block_offsets[2*i_block];
+ int stop_offset = shared_block_offsets[2*i_block+1];
+ int start_block_offset = ALIGN_UP(start_offset, smpi_shared_malloc_blocksize);
+ int stop_block_offset = ALIGN_DOWN(stop_offset, smpi_shared_malloc_blocksize);