From f50a3b14cac88d8c6db2219f5a2c79696770ed6d Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 20 Nov 2020 21:52:39 +0100 Subject: [PATCH] Replace fixed-size C-style arrays with std::array. --- src/mc/inspect/DwarfExpression.hpp | 9 +++++---- src/mc/sosp/PageStore_test.cpp | 5 +++-- src/smpi/internals/smpi_shared.cpp | 7 ++++--- src/smpi/mpi/smpi_comm.cpp | 7 ++----- src/smpi/mpi/smpi_datatype.cpp | 24 +++++++++++++----------- src/smpi/mpi/smpi_datatype_derived.cpp | 9 +++++---- src/smpi/mpi/smpi_request.cpp | 9 +++++---- 7 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/mc/inspect/DwarfExpression.hpp b/src/mc/inspect/DwarfExpression.hpp index 3a94b47239..ab229ded85 100644 --- a/src/mc/inspect/DwarfExpression.hpp +++ b/src/mc/inspect/DwarfExpression.hpp @@ -9,6 +9,7 @@ #include #include +#include #include // runtime_error #include #include @@ -62,12 +63,12 @@ public: */ class ExpressionStack { public: - using value_type = std::uintptr_t; - static const std::size_t max_size = 64; + using value_type = std::uintptr_t; + static constexpr std::size_t MAX_SIZE = 64; private: // Values of the stack (the top is stack_[size_ - 1]): - uintptr_t stack_[max_size]{0}; + std::array stack_{{0}}; size_t size_ = 0; public: @@ -97,7 +98,7 @@ public: /** Push a value on the top of the stack */ void push(value_type value) { - if (size_ == max_size) + if (size_ == stack_.size()) throw evaluation_error("DWARF stack overflow"); stack_[size_++] = value; } diff --git a/src/mc/sosp/PageStore_test.cpp b/src/mc/sosp/PageStore_test.cpp index 99ff03aef3..526bfc7df5 100644 --- a/src/mc/sosp/PageStore_test.cpp +++ b/src/mc/sosp/PageStore_test.cpp @@ -5,6 +5,7 @@ #include "src/include/catch.hpp" +#include #include #include #include @@ -25,7 +26,7 @@ public: static std::size_t pagesize; static std::unique_ptr store; static void* data; - static size_t pageno[4]; + static std::array pageno; static int value; // member functions used by the test suite(s) @@ -43,7 +44,7 @@ public: std::size_t helper_tests::pagesize = 0; std::unique_ptr helper_tests::store = nullptr; void* helper_tests::data = nullptr; -size_t helper_tests::pageno[4] = {0, 0, 0, 0}; +std::array helper_tests::pageno = {{0, 0, 0, 0}}; int helper_tests::value = 0; void helper_tests::Init() diff --git a/src/smpi/internals/smpi_shared.cpp b/src/smpi/internals/smpi_shared.cpp index 50be4ae6d5..87838e7d85 100644 --- a/src/smpi/internals/smpi_shared.cpp +++ b/src/smpi/internals/smpi_shared.cpp @@ -33,8 +33,9 @@ * \ | | * ---- */ -#include +#include #include +#include #include "private.hpp" #include "xbt/config.hpp" @@ -326,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); diff --git a/src/smpi/mpi/smpi_comm.cpp b/src/smpi/mpi/smpi_comm.cpp index 73d4f84092..307fcdd8d2 100644 --- a/src/smpi/mpi/smpi_comm.cpp +++ b/src/smpi/mpi/smpi_comm.cpp @@ -241,16 +241,13 @@ MPI_Comm Comm::split(int color, int key) int myrank = this->rank(); int size = this->size(); /* Gather all colors and keys on rank 0 */ - int* sendbuf = xbt_new(int, 2); - sendbuf[0] = color; - sendbuf[1] = key; + const std::array sendbuf = {{color, key}}; if (myrank == 0) { recvbuf = xbt_new(int, 2 * size); } else { recvbuf = nullptr; } - gather__default(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this); - xbt_free(sendbuf); + gather__default(sendbuf.data(), 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this); /* Do the actual job */ if (myrank == 0) { MPI_Group* group_snd = xbt_new(MPI_Group, size); diff --git a/src/smpi/mpi/smpi_datatype.cpp b/src/smpi/mpi/smpi_datatype.cpp index a7ea791711..3f770e9c9d 100644 --- a/src/smpi/mpi/smpi_datatype.cpp +++ b/src/smpi/mpi/smpi_datatype.cpp @@ -12,6 +12,7 @@ #include "src/smpi/include/smpi_actor.hpp" #include +#include #include #include @@ -423,8 +424,8 @@ int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatyp /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/ *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)* old_type->size(), DT_FLAG_CONTIGUOUS); - int ints[3] = {count, block_length, stride}; - (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints, 0, nullptr, 1, &old_type); + const std::array ints = {{count, block_length, stride}}; + (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type); retval=MPI_SUCCESS; } return retval; @@ -449,8 +450,8 @@ int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_D }else{ /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/ *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS); - int ints[2] = {count, block_length}; - (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints, 1, &stride, 1, &old_type); + const std::array ints = {{count, block_length}}; + (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type); retval=MPI_SUCCESS; } return retval; @@ -618,10 +619,10 @@ int Datatype::create_subarray(int ndims, const int* array_of_sizes, tmp = *newtype; } - MPI_Aint lbs[1] = {lb * extent}; - int sizes [1]={1}; + const MPI_Aint lbs = lb * extent; + const int sizes = 1; //handle LB and UB with a resized call - create_hindexed( 1, sizes, lbs, tmp, newtype); + create_hindexed(1, &sizes, &lbs, tmp, newtype); unref(tmp); tmp = *newtype; @@ -632,11 +633,12 @@ int Datatype::create_subarray(int ndims, const int* array_of_sizes, } int Datatype::create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){ - int blocks[3] = {1, 1, 1}; - MPI_Aint disps[3] = {lb, 0, lb + extent}; - MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB}; + const std::array blocks = {{1, 1, 1}}; + const std::array disps = {{lb, 0, lb + extent}}; + const std::array types = {{MPI_LB, oldtype, MPI_UB}}; - *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types); + *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks.data(), + disps.data(), types.data()); (*newtype)->addflag(~DT_FLAG_COMMITED); return MPI_SUCCESS; diff --git a/src/smpi/mpi/smpi_datatype_derived.cpp b/src/smpi/mpi/smpi_datatype_derived.cpp index 75abfb4a81..924c02d834 100644 --- a/src/smpi/mpi/smpi_datatype_derived.cpp +++ b/src/smpi/mpi/smpi_datatype_derived.cpp @@ -8,6 +8,7 @@ #include "smpi_op.hpp" #include +#include #include namespace simgrid{ @@ -58,8 +59,8 @@ void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguou } Type_Hvector::Type_Hvector(int size,MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, MPI_Aint stride, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_length_(block_length), block_stride_(stride), old_type_(old_type){ - int ints[2] = {count, block_length}; - contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints, 1, &stride, 1, &old_type); + const std::array ints = {{count, block_length}}; + contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type); old_type->ref(); } Type_Hvector::~Type_Hvector(){ @@ -116,8 +117,8 @@ Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int coun : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type) { delete contents_; - int ints[3] = {count, block_length, stride}; - contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints, 0, nullptr, 1, &old_type); + const std::array ints = {{count, block_length, stride}}; + contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type); } int Type_Vector::clone(MPI_Datatype* type) diff --git a/src/smpi/mpi/smpi_request.cpp b/src/smpi/mpi/smpi_request.cpp index 5dd26dcdf4..e0c6ec5d1e 100644 --- a/src/smpi/mpi/smpi_request.cpp +++ b/src/smpi/mpi/smpi_request.cpp @@ -18,6 +18,7 @@ #include "src/smpi/include/smpi_actor.hpp" #include +#include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_request, smpi, "Logging specific to SMPI (request)"); @@ -337,8 +338,8 @@ void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype void *recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status * status) { - MPI_Request requests[2]; - MPI_Status stats[2]; + std::array requests; + std::array stats; int myid = simgrid::s4u::this_actor::get_pid(); if ((comm->group()->actor(dst)->get_pid() == myid) && (comm->group()->actor(src)->get_pid() == myid)) { Datatype::copy(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype); @@ -352,8 +353,8 @@ void Request::sendrecv(const void *sendbuf, int sendcount, MPI_Datatype sendtype } requests[0] = isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm); requests[1] = irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm); - startall(2, requests); - waitall(2, requests, stats); + startall(2, requests.data()); + waitall(2, requests.data(), stats.data()); unref(&requests[0]); unref(&requests[1]); if(status != MPI_STATUS_IGNORE) { -- 2.20.1