b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2allocator.H
Go to the documentation of this file.
1//------------------------------------------------------------------------
2// b2allocator.H --
3//
4// written by Mathias Doreille
5// Thomas Blome <thomas.blome@dlr.de>
6// Harald Klimach <harald.klimach@dlr.de>
7//
8// Copyright (c) 2004-2012 SMR Engineering & Development SA
9// 2502 Bienne, Switzerland
10//
11// Copyright (c) 2023-2024 Deutsches Zentrum für Luft- und Raumfahrt (DLR) e.V.
12// Linder Höhe, 51147 Köln
13//
14// All Rights Reserved. Proprietary source code. The contents of
15// this file may not be disclosed to third parties, copied or
16// duplicated in any form, in whole or in part, without the prior
17// written permission of SMR.
18//------------------------------------------------------------------------
19
20#ifndef B2ALLOCATOR_H_
21#define B2ALLOCATOR_H_
22
23#include <new>
24#include <stdlib.h>
25
26#include <forward_list>
27
28#include "b2ppconfig.h"
29
37namespace b2000 {
38
39using std::size_t;
40
63class Allocator {
64public:
65 Allocator() { no_debug_alloc = getenv("GLIBCXX_FORCE_NEW") == nullptr; }
66
67 explicit Allocator(size_t block_size_) : block_size(block_size_) {
68 no_debug_alloc = getenv("GLIBCXX_FORCE_NEW") == nullptr;
69 }
70
71 ~Allocator() {
72 for (list_block_t::iterator i = list_block.begin(); i != list_block.end(); ++i) {
73 ::operator delete(*i);
74 }
75 }
76
77 void* allocate(size_t size) {
78 void* res{nullptr};
79 if (no_debug_alloc) {
80 res = start;
81 const size_t aligned{(size + align_comp) & ~align_comp};
82 start += aligned;
83 if (start > end) {
84 while (block_size < 8 * aligned) { block_size *= 2; }
85 start = (char*)::operator new(block_size, block_alignment);
86 end = start + block_size;
87 list_block.push_front(start);
88 res = start;
89 start += size;
90 }
91 } else {
92 res = (char*)::operator new(size);
93 list_block.push_front((char*)res);
94 }
95 return res;
96 }
97
98 void deallocate(void* ptr, size_t size) {}
99
100private:
101 // Ensure 64-byte (AVX-512) alignment in starting addresses
102 static constexpr size_t align_comp{63};
103 static constexpr std::align_val_t block_alignment{align_comp+1};
104 using list_block_t = std::forward_list<char*>;
105 list_block_t list_block;
106 char* start{};
107 char* end{};
108 size_t block_size{65536};
109 bool no_debug_alloc;
110};
111
112} // namespace b2000
113
114#endif // B2ALLOCATOR_H_
Definition b2allocator.H:63
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32