X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/2750bde086c69304587076350db922bfd9da04f6..39c935d6d5ee86d153f6f7e6a10d723ae7c57f6f:/src/smpi/internals/smpi_shared.cpp diff --git a/src/smpi/internals/smpi_shared.cpp b/src/smpi/internals/smpi_shared.cpp index c984b99f65..83224d3d39 100644 --- a/src/smpi/internals/smpi_shared.cpp +++ b/src/smpi/internals/smpi_shared.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -33,8 +33,9 @@ * \ | | * ---- */ -#include +#include #include +#include #include "private.hpp" #include "xbt/config.hpp" @@ -45,9 +46,9 @@ #ifndef WIN32 #include #endif -#include -#include -#include +#include +#include +#include #ifndef MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON @@ -79,7 +80,7 @@ struct shared_data_t { }; std::unordered_map> allocs; -typedef decltype(allocs)::value_type shared_data_key_type; +using shared_data_key_type = decltype(allocs)::value_type; struct shared_metadata_t { size_t size; @@ -90,12 +91,12 @@ struct shared_metadata_t { }; std::map allocs_metadata; -std::map calls; +std::map> 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 } @@ -155,7 +156,7 @@ constexpr unsigned HUGE_PAGE_SIZE = 1U << 21; * The others are not. */ -void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int nb_shared_blocks) +void* smpi_shared_malloc_partial(size_t size, const size_t* shared_block_offsets, int nb_shared_blocks) { std::string huge_page_mount_point = simgrid::config::get_value("smpi/shared-malloc-hugepage"); bool use_huge_page = not huge_page_mount_point.empty(); @@ -178,7 +179,7 @@ void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int /* 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", @@ -208,11 +209,9 @@ void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int 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; @@ -287,14 +286,14 @@ void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int 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; @@ -328,8 +327,8 @@ void* smpi_shared_malloc(size_t size, const char* file, int line) return smpi_shared_malloc_local(size, file, line); } else if (smpi_cfg_shared_malloc() == SharedMallocType::GLOBAL) { int nb_shared_blocks = 1; - size_t shared_block_offsets[2] = {0, size}; - return smpi_shared_malloc_partial(size, shared_block_offsets, nb_shared_blocks); + const std::array shared_block_offsets = {{0, size}}; + return smpi_shared_malloc_partial(size, shared_block_offsets.data(), nb_shared_blocks); } XBT_DEBUG("Classic allocation of %zu bytes", size); return ::operator new(size);