Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
SmallArray.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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-2026 */
9/* */
10/* 1D array of data with pre-allocated buffer. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/common/SmallArray.h"
15#include "arccore/base/FatalErrorException.h"
16
17#include <cstdlib>
18#include <cstring>
19#include <iostream>
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane::Impl
25{
26
27namespace
28{
30 const bool is_verbose = false;
31} // namespace
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36void* StackMemoryAllocator::
37_allocateMemory(Int64 new_size)
38{
39 if (is_verbose)
40 std::cout << "ALLOCATE: use malloc s=" << new_size << "\n";
41 void* ptr = std::malloc(new_size);
42 if (!ptr)
43 ARCCORE_FATAL("Can not allocated memory for size '{0}'", new_size);
44 return ptr;
45}
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49
50AllocatedMemoryInfo StackMemoryAllocator::
51reallocate(MemoryAllocationArgs, AllocatedMemoryInfo current_ptr_info, Int64 new_size)
52{
53 void* current_ptr = current_ptr_info.baseAddress();
54 if (current_ptr != m_preallocated_buffer) {
55 if (new_size < m_preallocated_size) {
56 // We switch from an allocated pointer to our internal buffer.
57 // We must copy the values. We do not know exactly
58 // the size of 'current_ptr' but we are certain that it is
59 // greater than 'm_preallocated_size' and thus greater than 'new_size'.
60 // We therefore only copy these values
61 if (is_verbose)
62 std::cout << "REALLOCATE: use own buffer from realloc s=" << new_size << "\n";
63 std::memcpy(m_preallocated_buffer, current_ptr, new_size);
64 std::free(current_ptr);
65 return { m_preallocated_buffer, new_size };
66 }
67 if (is_verbose)
68 std::cout << "REALLOCATE: use realloc s=" << new_size << "\n";
69 return { std::realloc(current_ptr, new_size), new_size };
70 }
71
72 if (new_size <= m_preallocated_size) {
73 if (is_verbose)
74 std::cout << "REALLOCATE: use buffer because small size s=" << new_size << "\n";
75 return { m_preallocated_buffer, new_size };
76 }
77
78 // We must allocate and copy from the pre-allocated buffer.
79 if (is_verbose)
80 std::cout << "REALLOCATE: use malloc and copy s=" << new_size << "\n";
81 void* new_ptr = std::malloc(new_size);
82 std::memcpy(new_ptr, m_preallocated_buffer, m_preallocated_size);
83 return { new_ptr, new_size };
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89void StackMemoryAllocator::
90_freeMemory(void* ptr)
91{
92 std::free(ptr);
93}
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
98} // namespace Arcane::Impl
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Information about an allocated memory region.
void * baseAddress() const
Address of the start of the allocated region.
Class containing information to specialize allocations.
std::int64_t Int64
Signed integer type of 64 bits.