Skip to content

Commit 2ca8560

Browse files
Merge pull request #1201 from arcaneframework/dev/gg-move-checkresizearray-in-memoryutils
Move helper function 'checkResizeArray()' from 'MeshUtils' to 'MemoryUtils'
2 parents b67dacf + 1a6e162 commit 2ca8560

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

arcane/src/arcane/core/MeshUtils.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,20 +1790,6 @@ findOneItem(IItemFamily* family, ItemUniqueId unique_id)
17901790
/*---------------------------------------------------------------------------*/
17911791
/*---------------------------------------------------------------------------*/
17921792

1793-
Int64 MeshUtils::impl::
1794-
computeCapacity(Int64 size)
1795-
{
1796-
Int64 new_capacity = size * 2;
1797-
if (size > 5000000)
1798-
new_capacity = static_cast<Int64>(static_cast<double>(size) * 1.2);
1799-
else if (size > 500000)
1800-
new_capacity = static_cast<Int64>(static_cast<double>(size) * 1.5);
1801-
return new_capacity;
1802-
}
1803-
1804-
/*---------------------------------------------------------------------------*/
1805-
/*---------------------------------------------------------------------------*/
1806-
18071793
namespace MeshUtils::impl
18081794
{
18091795
//! Retourne le max des uniqueId() des entités de \a group

arcane/src/arcane/core/MeshUtils.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
/*---------------------------------------------------------------------------*/
1616

1717
#include "arcane/utils/FunctorUtils.h"
18+
#include "arcane/utils/MemoryUtils.h"
1819

1920
#include "arcane/Item.h"
2021

@@ -335,9 +336,10 @@ visitGroups(IMesh* mesh, const LambdaType& f)
335336

336337
namespace impl
337338
{
338-
//! Calcule une capacité adaptée pour une taille de \a size
339-
extern "C++" ARCANE_CORE_EXPORT Int64
340-
computeCapacity(Int64 size);
339+
inline Int64 computeCapacity(Int64 size)
340+
{
341+
return Arcane::MemoryUtils::impl::computeCapacity(size);
342+
}
341343
} // namespace impl
342344

343345
/*---------------------------------------------------------------------------*/
@@ -361,15 +363,7 @@ namespace impl
361363
template <typename DataType> inline bool
362364
checkResizeArray(Array<DataType>& array, Int64 new_size, bool force_resize)
363365
{
364-
Int64 s = array.largeSize();
365-
if (new_size > s || force_resize) {
366-
if (new_size > array.capacity()) {
367-
array.reserve(impl::computeCapacity(new_size));
368-
}
369-
array.resize(new_size);
370-
return true;
371-
}
372-
return false;
366+
return Arcane::MemoryUtils::checkResizeArrayWithCapacity(array, new_size, force_resize);
373367
}
374368

375369
/*---------------------------------------------------------------------------*/

arcane/src/arcane/utils/MemoryUtils.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* MemoryUtils.cc (C) 2000-2023 */
8+
/* MemoryUtils.cc (C) 2000-2024 */
99
/* */
1010
/* Fonctions utilitaires de gestion mémoire. */
1111
/*---------------------------------------------------------------------------*/
@@ -52,6 +52,20 @@ getAllocatorForMostlyReadOnlyData()
5252
/*---------------------------------------------------------------------------*/
5353
/*---------------------------------------------------------------------------*/
5454

55+
Int64 MemoryUtils::impl::
56+
computeCapacity(Int64 size)
57+
{
58+
Int64 new_capacity = size * 2;
59+
if (size > 5000000)
60+
new_capacity = static_cast<Int64>(static_cast<double>(size) * 1.2);
61+
else if (size > 500000)
62+
new_capacity = static_cast<Int64>(static_cast<double>(size) * 1.5);
63+
return new_capacity;
64+
}
65+
66+
/*---------------------------------------------------------------------------*/
67+
/*---------------------------------------------------------------------------*/
68+
5569
} // End namespace Arcane
5670

5771
/*---------------------------------------------------------------------------*/

arcane/src/arcane/utils/MemoryUtils.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* MemoryUtils.h (C) 2000-2023 */
8+
/* MemoryUtils.h (C) 2000-2024 */
99
/* */
1010
/* Fonctions utilitaires de gestion mémoire. */
1111
/*---------------------------------------------------------------------------*/
@@ -67,6 +67,44 @@ getAllocatorForMostlyReadOnlyData();
6767
/*---------------------------------------------------------------------------*/
6868
/*---------------------------------------------------------------------------*/
6969

70+
namespace impl
71+
{
72+
//! Calcule une capacité adaptée pour une taille de \a size
73+
extern "C++" ARCANE_UTILS_EXPORT Int64
74+
computeCapacity(Int64 size);
75+
} // namespace impl
76+
77+
/*---------------------------------------------------------------------------*/
78+
/*---------------------------------------------------------------------------*/
79+
/*!
80+
* \brief Redimensionne un tableau en ajoutant une réserve de mémoire.
81+
*
82+
* Le tableau \a array est redimensionné uniquement si \a new_size est
83+
* supérieure à la taille actuelle du tableau ou si \a force_resize est vrai.
84+
*
85+
* Si le tableau est redimensionné, on réserve une capacité supplémentaire
86+
* pour éviter de réallouer à chaque fois.
87+
*
88+
* \retval true si un redimensionnement a eu lieu
89+
* \retval false sinon
90+
*/
91+
template <typename DataType> inline bool
92+
checkResizeArrayWithCapacity(Array<DataType>& array, Int64 new_size, bool force_resize)
93+
{
94+
Int64 s = array.largeSize();
95+
if (new_size > s || force_resize) {
96+
if (new_size > array.capacity()) {
97+
array.reserve(impl::computeCapacity(new_size));
98+
}
99+
array.resize(new_size);
100+
return true;
101+
}
102+
return false;
103+
}
104+
105+
/*---------------------------------------------------------------------------*/
106+
/*---------------------------------------------------------------------------*/
107+
70108
} // namespace Arcane::MemoryUtils
71109

72110
/*---------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)