Arcane  v3.15.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
SmallArray.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* SmallArray.cc (C) 2000-2025 */
9/* */
10/* Tableau 1D de données avec buffer pré-alloué. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/SmallArray.h"
15
16#include <cstdlib>
17#include <cstring>
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane::impl
23{
24
25namespace
26{
27 //! A mettre à true si on souhaite activer les traces d'allocation
28 const bool is_verbose = false;
29} // namespace
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34AllocatedMemoryInfo StackMemoryAllocator::
35allocate(MemoryAllocationArgs, Int64 new_size)
36{
37 if (new_size <= m_preallocated_size) {
38 if (is_verbose)
39 std::cout << "ALLOCATE: use preallocated s=" << new_size << "\n";
40 return { m_preallocated_buffer, new_size };
41 }
42
43 if (is_verbose)
44 std::cout << "ALLOCATE: use malloc s=" << new_size << "\n";
45 return { std::malloc(new_size), new_size };
46}
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51AllocatedMemoryInfo StackMemoryAllocator::
52reallocate(MemoryAllocationArgs, AllocatedMemoryInfo current_ptr_info, Int64 new_size)
53{
54 void* current_ptr = current_ptr_info.baseAddress();
55 if (current_ptr != m_preallocated_buffer) {
56 if (new_size < m_preallocated_size) {
57 // On passe d'un pointeur alloué vers notre buffer interne.
58 // Il faut recopier les valeurs. On ne connait pas exactement
59 // la taille de 'current_ptr' mais on est certain qu'elle est
60 // supérieure à 'm_preallocated_size' donc à 'new_size'.
61 // On ne recopie donc que ces valeurs là
62 if (is_verbose)
63 std::cout << "REALLOCATE: use own buffer from realloc s=" << new_size << "\n";
64 std::memcpy(m_preallocated_buffer, current_ptr, new_size);
65 std::free(current_ptr);
66 return { m_preallocated_buffer, new_size };
67 }
68 if (is_verbose)
69 std::cout << "REALLOCATE: use realloc s=" << new_size << "\n";
70 return { std::realloc(current_ptr, new_size), new_size };
71 }
72
73 if (new_size <= m_preallocated_size) {
74 if (is_verbose)
75 std::cout << "REALLOCATE: use buffer because small size s=" << new_size << "\n";
76 return { m_preallocated_buffer, new_size };
77 }
78
79 // Il faut allouer et recopier depuis le buffer pré-alloué.
80 if (is_verbose)
81 std::cout << "REALLOCATE: use malloc and copy s=" << new_size << "\n";
82 void* new_ptr = std::malloc(new_size);
83 std::memcpy(new_ptr, m_preallocated_buffer, m_preallocated_size);
84 return { new_ptr, new_size };
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
92{
93 void* ptr = ptr_info.baseAddress();
94 if (ptr != m_preallocated_buffer)
95 std::free(ptr);
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101} // namespace Arcane::impl
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) final
Libère la mémoire dont l'adresse de base est ptr.
Definition SmallArray.cc:91
Informations sur une zone mémoire allouée.
void * baseAddress() const
Adresse du début de la zone allouée.
Classe contenant des informations pour spécialiser les allocations.