From: Arnaud Giersch Date: Fri, 2 Oct 2020 20:04:16 +0000 (+0200) Subject: Reduce scope for loop variables. X-Git-Tag: v3.26~408 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/e34f4a3d8b2aa4081ada02909800ae3cc08d48d5 Reduce scope for loop variables. --- diff --git a/examples/c/dht-kademlia/node.c b/examples/c/dht-kademlia/node.c index 79d228b4f6..da155cc306 100644 --- a/examples/c/dht-kademlia/node.c +++ b/examples/c/dht-kademlia/node.c @@ -170,7 +170,6 @@ void routing_table_update(const_node_t node, unsigned int id) */ answer_t find_closest(const_node_t node, unsigned int destination_id) { - int i; answer_t answer = answer_init(destination_id); /* We find the corresponding bucket for the id */ const_bucket_t bucket = routing_table_find_bucket(node->table, destination_id); @@ -182,7 +181,7 @@ answer_t find_closest(const_node_t node, unsigned int destination_id) /* However, if we don't have enough elements in our bucket, we NEED to include at least "BUCKET_SIZE" elements * (if, of course, we know at least "BUCKET_SIZE" elements. So we're going to look unsigned into the other buckets. */ - for (i = 1; answer->size < BUCKET_SIZE && ((bucket_id - i > 0) || (bucket_id + i < IDENTIFIER_SIZE)); i++) { + for (int i = 1; answer->size < BUCKET_SIZE && ((bucket_id - i > 0) || (bucket_id + i < IDENTIFIER_SIZE)); i++) { /* We check the previous buckets */ if (bucket_id - i >= 0) { const_bucket_t bucket_p = &node->table->buckets[bucket_id - i]; diff --git a/examples/c/dht-kademlia/routing_table.c b/examples/c/dht-kademlia/routing_table.c index b8204f837d..9751cf7ffe 100644 --- a/examples/c/dht-kademlia/routing_table.c +++ b/examples/c/dht-kademlia/routing_table.c @@ -25,9 +25,8 @@ routing_table_t routing_table_init(unsigned int node_id) /** @brief Frees the routing table */ void routing_table_free(routing_table_t table) { - unsigned int i; // Free the buckets. - for (i = 0; i <= IDENTIFIER_SIZE; i++) { + for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) { xbt_dynar_free(&table->buckets[i].nodes); } xbt_free(table->buckets); @@ -37,13 +36,13 @@ void routing_table_free(routing_table_t table) /**@brief prints the routing table, to debug stuff. */ void routing_table_print(const_routing_table_t table) { - unsigned int j; - unsigned int value; XBT_INFO("Routing table of %08x:", table->id); for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) { if (!xbt_dynar_is_empty(table->buckets[i].nodes)) { XBT_INFO("Bucket number %u: ", i); + unsigned int j; + unsigned int value; xbt_dynar_foreach (table->buckets[i].nodes, j, value) { XBT_INFO("Element %u: %08x", j, value); } diff --git a/examples/s4u/dht-kademlia/node.cpp b/examples/s4u/dht-kademlia/node.cpp index 265cdc06d3..f98c9ef032 100644 --- a/examples/s4u/dht-kademlia/node.cpp +++ b/examples/s4u/dht-kademlia/node.cpp @@ -23,7 +23,6 @@ static void destroy(void* message) bool Node::join(unsigned int known_id) { const Answer* node_list; - unsigned int i; bool got_answer = false; /* Add the guy we know to our routing table and ourselves. */ @@ -59,7 +58,7 @@ bool Node::join(unsigned int known_id) /* Second step: Send a FIND_NODE to a a random node in buckets */ unsigned int bucket_id = table.findBucket(known_id)->getId(); xbt_assert(bucket_id <= IDENTIFIER_SIZE); - for (i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) { + for (unsigned int i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) { if (bucket_id > i) { unsigned int id_in_bucket = get_id_in_prefix(id_, bucket_id - i); findNode(id_in_bucket, false); diff --git a/src/kernel/lmm/maxmin.cpp b/src/kernel/lmm/maxmin.cpp index 3dd1bb67f2..eff10f6a30 100644 --- a/src/kernel/lmm/maxmin.cpp +++ b/src/kernel/lmm/maxmin.cpp @@ -645,8 +645,7 @@ template void System::lmm_solve(CnstList& cnst_list) min_usage = -1; min_bound = -1; saturated_constraints.clear(); - int pos; - for (pos = 0; pos < cnst_light_num; pos++) { + for (int pos = 0; pos < cnst_light_num; pos++) { xbt_assert(not cnst_light_tab[pos].cnst->active_element_set_.empty(), "Cannot saturate more a constraint that has" " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:)" diff --git a/src/mc/compare.cpp b/src/mc/compare.cpp index 70c2982251..f13a96a470 100644 --- a/src/mc/compare.cpp +++ b/src/mc/compare.cpp @@ -1015,7 +1015,6 @@ static bool areas_differ_with_type(simgrid::mc::StateComparator& state, const vo const simgrid::mc::Type* subtype; const simgrid::mc::Type* subsubtype; int elm_size; - int i; xbt_assert(type != nullptr); switch (type->type) { @@ -1060,7 +1059,7 @@ static bool areas_differ_with_type(simgrid::mc::StateComparator& state, const vo default: return false; } - for (i = 0; i < type->element_count; i++) { + for (int i = 0; i < type->element_count; i++) { size_t off = i * elm_size; if (areas_differ_with_type(state, (const char*)real_area1 + off, snapshot1, region1, (const char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level)) diff --git a/src/mc/inspect/mc_dwarf.cpp b/src/mc/inspect/mc_dwarf.cpp index f6f8601e95..c1620ec660 100644 --- a/src/mc/inspect/mc_dwarf.cpp +++ b/src/mc/inspect/mc_dwarf.cpp @@ -396,8 +396,7 @@ static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) int result = 1; Dwarf_Die child; - int res; - for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) { + for (int res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) { int child_tag = dwarf_tag(&child); if (child_tag == DW_TAG_subrange_type || child_tag == DW_TAG_enumeration_type) result *= MC_dwarf_subrange_element_count(&child, unit); @@ -487,10 +486,9 @@ static void MC_dwarf_fill_member_location(const simgrid::mc::Type* type, simgrid static void MC_dwarf_add_members(const simgrid::mc::ObjectInformation* /*info*/, Dwarf_Die* die, const Dwarf_Die* /*unit*/, simgrid::mc::Type* type) { - int res; Dwarf_Die child; xbt_assert(type->members.empty()); - for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) { + for (int res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) { int tag = dwarf_tag(&child); if (tag == DW_TAG_member || tag == DW_TAG_inheritance) { // Skip declarations: @@ -853,8 +851,7 @@ static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf { // For each child DIE: Dwarf_Die child; - int res; - for (res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) + for (int res = dwarf_child(die, &child); res == 0; res = dwarf_siblingof(&child, &child)) MC_dwarf_handle_die(info, &child, unit, frame, ns); } diff --git a/src/smpi/bindings/smpi_f77.cpp b/src/smpi/bindings/smpi_f77.cpp index 4d108c298c..23b5270425 100644 --- a/src/smpi/bindings/smpi_f77.cpp +++ b/src/smpi/bindings/smpi_f77.cpp @@ -405,8 +405,7 @@ void mpi_info_get_(int* info, char* key, int* valuelen, char* value, int* flag, xbt_free(tkey); if(*flag!=0){ int replace=0; - int i=0; - for (i=0; i<*valuelen; i++){ + for (int i = 0; i < *valuelen; i++) { if(value[i]=='\0') replace=1; if(replace) @@ -902,8 +901,7 @@ void mpi_info_get_nkeys_ ( int* info, int *nkeys, int* ierr){ void mpi_info_get_nthkey_ ( int* info, int* n, char *key, int* ierr, unsigned int keylen){ *ierr = MPI_Info_get_nthkey( simgrid::smpi::Info::f2c(*info), *n, key); - unsigned int i = 0; - for (i=strlen(key); i(xbt_malloc(*count*sizeof(MPI_Datatype))); MPI_Aint* indices_aint=new MPI_Aint[*count]; - for(i=0; i< *count; i++){ + for (int i = 0; i < *count; i++) { indices_aint[i]=indices[i]; types[i] = simgrid::smpi::Datatype::f2c(old_types[i]); } @@ -203,9 +202,8 @@ void mpi_type_struct_ (int* count, int* blocklens, int* indices, int* old_types, void mpi_type_create_struct_(int* count, int* blocklens, MPI_Aint* indices, int* old_types, int* newtype, int* ierr){ MPI_Datatype tmp; - int i=0; MPI_Datatype* types = static_cast(xbt_malloc(*count*sizeof(MPI_Datatype))); - for(i=0; i< *count; i++){ + for (int i = 0; i < *count; i++) { types[i] = simgrid::smpi::Datatype::f2c(old_types[i]); } *ierr = MPI_Type_create_struct(*count, blocklens, indices, types, &tmp); diff --git a/src/smpi/mpi/smpi_win.cpp b/src/smpi/mpi/smpi_win.cpp index f62ab4e2ef..456905c383 100644 --- a/src/smpi/mpi/smpi_win.cpp +++ b/src/smpi/mpi/smpi_win.cpp @@ -617,12 +617,11 @@ int Win::lock(int lock_type, int rank, int /*assert*/) } int Win::lock_all(int assert){ - int i=0; int retval = MPI_SUCCESS; - for (i=0; isize();i++){ - int ret = this->lock(MPI_LOCK_SHARED, i, assert); - if(ret != MPI_SUCCESS) - retval = ret; + for (int i = 0; i < comm_->size(); i++) { + int ret = this->lock(MPI_LOCK_SHARED, i, assert); + if (ret != MPI_SUCCESS) + retval = ret; } return retval; } @@ -644,9 +643,8 @@ int Win::unlock(int rank){ } int Win::unlock_all(){ - int i=0; int retval = MPI_SUCCESS; - for (i=0; isize();i++){ + for (int i = 0; i < comm_->size(); i++) { int ret = this->unlock(i); if (ret != MPI_SUCCESS) retval = ret; diff --git a/src/xbt/dynar.cpp b/src/xbt/dynar.cpp index 6778ca1f92..ae898abfee 100644 --- a/src/xbt/dynar.cpp +++ b/src/xbt/dynar.cpp @@ -300,9 +300,7 @@ void xbt_dynar_remove_at(xbt_dynar_t dynar, int idx, void* object) */ signed int xbt_dynar_search_or_negative(const_xbt_dynar_t dynar, const void* elem) { - unsigned long it; - - for (it = 0; it < dynar->used; it++) + for (unsigned long it = 0; it < dynar->used; it++) if (not memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { return it; } @@ -317,9 +315,7 @@ signed int xbt_dynar_search_or_negative(const_xbt_dynar_t dynar, const void* ele */ int xbt_dynar_member(const_xbt_dynar_t dynar, const void* elem) { - unsigned long it; - - for (it = 0; it < dynar->used; it++) + for (unsigned long it = 0; it < dynar->used; it++) if (not memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { return 1; } @@ -394,11 +390,10 @@ void xbt_dynar_map(const_xbt_dynar_t dynar, void_f_pvoid_t op) char *const data = (char *) dynar->data; const unsigned long elmsize = dynar->elmsize; const unsigned long used = dynar->used; - unsigned long i; _sanity_check_dynar(dynar); - for (i = 0; i < used; i++) { + for (unsigned long i = 0; i < used; i++) { char* elm = data + i * elmsize; op(elm); } diff --git a/src/xbt/log.cpp b/src/xbt/log.cpp index 7f4da38243..5b16eff25b 100644 --- a/src/xbt/log.cpp +++ b/src/xbt/log.cpp @@ -437,18 +437,15 @@ static xbt_log_setting_t _xbt_log_parse_setting(const char *control_string) static xbt_log_category_t _xbt_log_cat_searchsub(xbt_log_category_t cat, const char* name) { - xbt_log_category_t child; - xbt_log_category_t res; - XBT_DEBUG("Search '%s' into '%s' (firstChild='%s'; nextSibling='%s')", name, cat->name, (cat->firstChild ? cat->firstChild->name : "none"), (cat->nextSibling ? cat->nextSibling->name : "none")); if (strcmp(cat->name, name) == 0) return cat; - for (child = cat->firstChild; child != nullptr; child = child->nextSibling) { + for (xbt_log_category_t child = cat->firstChild; child != nullptr; child = child->nextSibling) { XBT_DEBUG("Dig into %s", child->name); - res = _xbt_log_cat_searchsub(child, name); + xbt_log_category_t res = _xbt_log_cat_searchsub(child, name); if (res) return res; } diff --git a/src/xbt/mallocator.c b/src/xbt/mallocator.c index 8fff0ce255..d4685d63a9 100644 --- a/src/xbt/mallocator.c +++ b/src/xbt/mallocator.c @@ -125,11 +125,10 @@ xbt_mallocator_t xbt_mallocator_new(int size, pvoid_f_void_t new_f, void_f_pvoid */ void xbt_mallocator_free(xbt_mallocator_t m) { - int i; xbt_assert(m != NULL, "Invalid parameter"); XBT_VERB("Frees mallocator %p (size:%d/%d)", m, m->current_size, m->max_size); - for (i = 0; i < m->current_size; i++) { + for (int i = 0; i < m->current_size; i++) { m->free_f(m->objects[i]); } xbt_free(m->objects); @@ -159,9 +158,8 @@ void *xbt_mallocator_get(xbt_mallocator_t m) if (m->current_size <= 0) { /* No object is ready yet. Create a bunch of them to try to group the * mallocs on the same memory pages (to help the cache lines) */ - int i; int amount = MIN(m->max_size / 2, 1000); - for (i = 0; i < amount; i++) + for (int i = 0; i < amount; i++) m->objects[i] = m->new_f(); m->current_size = amount; } diff --git a/src/xbt/mmalloc/mmalloc.c b/src/xbt/mmalloc/mmalloc.c index d3c26bbd26..7b98cdcff1 100644 --- a/src/xbt/mmalloc/mmalloc.c +++ b/src/xbt/mmalloc/mmalloc.c @@ -70,9 +70,6 @@ static void initialize_heapinfo_heapinfo(const s_xbt_mheap_t* mdp) * properly, we need to make the align function publicly visible, too */ static void initialize(xbt_mheap_t mdp) { - int i; - malloc_info mi; /* to compute the offset of the swag hook */ - // Update mdp meta-data: mdp->heapsize = HEAP / BLOCKSIZE; mdp->heapinfo = (malloc_info *) @@ -89,9 +86,9 @@ static void initialize(xbt_mheap_t mdp) initialize_heapinfo_heapinfo(mdp); - for (i=0;ifraghead[i]), - xbt_swag_offset(mi, freehook)); + for (int i = 0; i < BLOCKLOG; i++) { + malloc_info mi; /* to compute the offset of the swag hook */ + xbt_swag_init(&(mdp->fraghead[i]), xbt_swag_offset(mi, freehook)); } } @@ -109,8 +106,6 @@ static void *register_morecore(struct mdesc *mdp, size_t size) /* Check if we need to grow the info table (in a multiplicative manner) */ if ((size_t) BLOCK((char *) result + size) > mdp->heapsize) { - int it; - size_t newsize = mdp->heapsize; while ((size_t) BLOCK((char *) result + size) > newsize) newsize *= 2; @@ -140,7 +135,7 @@ static void *register_morecore(struct mdesc *mdp, size_t size) /* mark the space previously occupied by the block info as free by first marking it * as occupied in the regular way, and then freing it */ - for (it=0; itheapsize * sizeof(malloc_info)); it++){ + for (int it = 0; it < BLOCKIFY(mdp->heapsize * sizeof(malloc_info)); it++) { newinfo[BLOCK(oldinfo)+it].type = MMALLOC_TYPE_UNFRAGMENTED; newinfo[BLOCK(oldinfo)+it].busy_block.ignore = 0; } diff --git a/src/xbt/mmalloc/mrealloc.c b/src/xbt/mmalloc/mrealloc.c index 4ce18deaf3..ad6ea5d20d 100644 --- a/src/xbt/mmalloc/mrealloc.c +++ b/src/xbt/mmalloc/mrealloc.c @@ -79,9 +79,8 @@ void *mrealloc(xbt_mheap_t mdp, void *ptr, size_t size) /* Full blocks -> Full blocks; see if we can hold it in place. */ blocks = BLOCKIFY(size); if (blocks < mdp->heapinfo[block].busy_block.size) { - size_t it; /* The new size is smaller; return excess memory to the free list. */ - for (it= block+blocks; it< mdp->heapinfo[block].busy_block.size ; it++){ + for (size_t it = block + blocks; it < mdp->heapinfo[block].busy_block.size; it++) { mdp->heapinfo[it].type = MMALLOC_TYPE_UNFRAGMENTED; // FIXME that should be useless, type should already be 0 here mdp->heapinfo[it].busy_block.ignore = 0; mdp->heapinfo[it].busy_block.size = 0; diff --git a/teshsuite/smpi/io-all-at/io-all-at.c b/teshsuite/smpi/io-all-at/io-all-at.c index 3c2963b1d1..b977265a5e 100644 --- a/teshsuite/smpi/io-all-at/io-all-at.c +++ b/teshsuite/smpi/io-all-at/io-all-at.c @@ -11,7 +11,6 @@ int main( int argc, char *argv[] ) int errs = 0; int size; int rank; - int i; int* buf; int count; MPI_File fh; @@ -61,7 +60,8 @@ int main( int argc, char *argv[] ) MPI_Barrier( comm ); - for (i=0; i