Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
SmallArray.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 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-2023 */
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{
28const bool is_verbose = false;
29}
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34bool StackMemoryAllocator::
35hasRealloc() const
36{
37 return true;
38}
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43void* StackMemoryAllocator::
44allocate(size_t new_size)
45{
46 if (new_size <= m_preallocated_size) {
47 if (is_verbose)
48 std::cout << "ALLOCATE: use preallocated s=" << new_size << "\n";
49 return m_preallocated_buffer;
50 }
51
52 if (is_verbose)
53 std::cout << "ALLOCATE: use malloc s=" << new_size << "\n";
54 return std::malloc(new_size);
55}
56
57/*---------------------------------------------------------------------------*/
58/*---------------------------------------------------------------------------*/
59
60void* StackMemoryAllocator::
61reallocate(void* current_ptr, size_t new_size)
62{
63 if (current_ptr != m_preallocated_buffer) {
64 if (new_size < m_preallocated_size) {
65 // On passe d'un pointeur alloué vers notre buffer interne.
66 // Il faut recopier les valeurs. On ne connait pas exactement
67 // la taille de 'current_ptr' mais on est certain qu'elle est
68 // supérieure à 'm_preallocated_size' donc à 'new_size'.
69 // On ne recopie donc que ces valeurs là
70 if (is_verbose)
71 std::cout << "REALLOCATE: use own buffer from realloc s=" << new_size << "\n";
72 std::memcpy(m_preallocated_buffer, current_ptr, new_size);
73 std::free(current_ptr);
74 return m_preallocated_buffer;
75 }
76 if (is_verbose)
77 std::cout << "REALLOCATE: use realloc s=" << new_size << "\n";
78 return std::realloc(current_ptr, new_size);
79 }
80
81 if (new_size <= m_preallocated_size) {
82 if (is_verbose)
83 std::cout << "REALLOCATE: use buffer because small size s=" << new_size << "\n";
84 return m_preallocated_buffer;
85 }
86
87 // Il faut allouer et recopier depuis le buffer pré-alloué.
88 if (is_verbose)
89 std::cout << "REALLOCATE: use malloc and copy s=" << new_size << "\n";
90 void* new_ptr = std::malloc(new_size);
91 std::memcpy(new_ptr, m_preallocated_buffer, m_preallocated_size);
92 return new_ptr;
93}
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
98void StackMemoryAllocator::
99deallocate(void* ptr)
100{
101 if (ptr != m_preallocated_buffer)
102 std::free(ptr);
103}
104
105/*---------------------------------------------------------------------------*/
106/*---------------------------------------------------------------------------*/
107
108} // namespace Arcane
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/