Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
AlignedMemoryAllocator.h
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/* AlignedMemoryAllocator.h (C) 2000-2025 */
9/* */
10/* Default memory allocator. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_COMMON_ALIGNEDMEMORYALLOCATOR_H
13#define ARCCORE_COMMON_ALIGNEDMEMORYALLOCATOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/common/IMemoryAllocator.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Impl
29{
30 extern "C++" ARCCORE_COMMON_EXPORT size_t
31 adjustMemoryCapacity(size_t wanted_capacity, size_t element_size);
32}
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37/*!
38 * \brief Memory allocator with specific memory alignment.
39 *
40 * This class is used via the two public methods Simd()
41 * and CacheLine(), which return an allocator with adequate alignment to
42 * allow vectorization, and an allocator aligned to a cache line, respectively.
43 */
44class ARCCORE_COMMON_EXPORT AlignedMemoryAllocator
45: public IMemoryAllocator
46{
47 private:
48
49 static AlignedMemoryAllocator SimdAllocator;
50 static AlignedMemoryAllocator CacheLineAllocator;
51
52 public:
53
54 // TODO: try to find the correct values based on the target.
55 // 64 is OK for all x64 architectures for both SIMD and cache line.
56
57 // IMPORTANT: If we change the value here, we must change the alignment
58 // size of ArrayImplBase.
59
60 // TODO Currently, only 64 alignment is allowed. To allow other values,
61 // the implementation in ArrayImplBase must be modified.
62
63 // TODO mark the methods as 'final'.
64
65 //! Alignment for structures using vectorization
66 static constexpr Integer simdAlignment() { return 64; }
67 //! Alignment for a cache line.
68 static constexpr Integer cacheLineAlignment() { return 64; }
69
70 /*!
71 * \brief Allocator guaranteeing alignment to use vectorization on the
72 * target platform.
73 *
74 * This is the alignment for the more restrictive type, and therefore
75 * this allocator can be used for all vector structures.
76 */
77 static AlignedMemoryAllocator* Simd()
78 {
79 return &SimdAllocator;
80 }
81
82 /*!
83 * \brief Allocator guaranteeing alignment to a cache line.
84 */
85 static AlignedMemoryAllocator* CacheLine()
86 {
87 return &CacheLineAllocator;
88 }
89
90 protected:
91
92 explicit AlignedMemoryAllocator(Int32 alignment)
93 : m_alignment(static_cast<size_t>(alignment))
94 {}
95
96 public:
97
98 bool hasRealloc(MemoryAllocationArgs) const override { return false; }
99 AllocatedMemoryInfo allocate(MemoryAllocationArgs args, Int64 new_size) override;
100 AllocatedMemoryInfo reallocate(MemoryAllocationArgs args, AllocatedMemoryInfo current_ptr, Int64 new_size) override;
101 void deallocate(MemoryAllocationArgs args, AllocatedMemoryInfo ptr) override;
102 Int64 adjustedCapacity(MemoryAllocationArgs args, Int64 wanted_capacity, Int64 element_size) const override;
103 size_t guaranteedAlignment(MemoryAllocationArgs) const override { return m_alignment; }
105
106 private:
107
108 size_t m_alignment;
109};
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114} // namespace Arcane
115
116/*---------------------------------------------------------------------------*/
117/*---------------------------------------------------------------------------*/
118
119#endif
Memory allocator with specific memory alignment.
size_t guaranteedAlignment(MemoryAllocationArgs) const override
Value of the alignment guaranteed by the allocator.
static AlignedMemoryAllocator * CacheLine()
Allocator guaranteeing alignment to a cache line.
eMemoryResource memoryResource() const override
Memory resource provided by the allocator.
static constexpr Integer simdAlignment()
Alignment for structures using vectorization.
static AlignedMemoryAllocator * Simd()
Allocator guaranteeing alignment to use vectorization on the target platform.
bool hasRealloc(MemoryAllocationArgs) const override
Indicates whether the allocator supports realloc semantics.
static constexpr Integer cacheLineAlignment()
Alignment for a cache line.
Information about an allocated memory region.
Class containing information to specialize allocations.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
eMemoryResource
List of available memory resources.
@ Host
Allocates on the host.
std::int32_t Int32
Signed integer type of 32 bits.