CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
temporaryMemory.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <algorithm>
38#include <vector>
39
40#include "../config.h"
41#include "macros.hpp"
42
44namespace codi {
45
55 static size_t constexpr InitialDataSize = 4 * 1024 * 1024;
56
57 private:
58
59 std::vector<char> data;
60 size_t dataPos;
61
62 public:
63
66
68 CODI_INLINE TemporaryMemory(size_t initialSize) : data(initialSize), dataPos() {}
69
72 return 0 == dataPos;
73 }
74
77 template<typename T>
78 CODI_INLINE T* alloc(size_t size) {
79 codiAssert(dataPos + size * sizeof(T) <= data.size());
80
81 T* castPointer = reinterpret_cast<T*>(&data.data()[dataPos]);
82 dataPos += size * sizeof(T);
83
84 return castPointer;
85 }
86
88 template<typename T, typename... Args>
89 CODI_INLINE T* allocAndInit(Args&&... args) {
90 T* value = alloc<T>(1);
91 new (value) T(std::forward<Args>(args)...);
92
93 return value;
94 }
95
98 CODI_INLINE void ensureSize(size_t newSize) {
99 if (dataPos != 0) {
100 CODI_EXCEPTION("Temporary memory can only be extended when no data is allocated.");
101 }
102
103 if (data.size() < newSize) {
104 data.resize(newSize);
105 }
106 }
107
111 // Clear used data.
112 std::fill(data.begin(), data.begin() + dataPos, 0);
113 dataPos = 0;
114 }
115 };
116}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Allocator for temporary used memory.
Definition temporaryMemory.hpp:54
T * allocAndInit(Args &&... args)
Allocate a single entity of T and call the constructor with args.
Definition temporaryMemory.hpp:89
bool isEmpty()
Returns true if no data is currently allocated.
Definition temporaryMemory.hpp:71
void free()
Free all allocated memory. No destructors are called. Stored pointers and resources need to be deallo...
Definition temporaryMemory.hpp:110
TemporaryMemory()
Constructor.
Definition temporaryMemory.hpp:65
void ensureSize(size_t newSize)
Definition temporaryMemory.hpp:98
static size_t constexpr InitialDataSize
4 MiB of memory.
Definition temporaryMemory.hpp:55
TemporaryMemory(size_t initialSize)
Constructor.
Definition temporaryMemory.hpp:68
T * alloc(size_t size)
Allocate an array of type T with length size. Data is zero initialized. No constructors of T are call...
Definition temporaryMemory.hpp:78