-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathBufferSTL.cpp
74 lines (61 loc) · 1.81 KB
/
BufferSTL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* BufferSTL.cpp
*
* Created on: Sep 26, 2017
* Author: William F Godoy [email protected]
*/
#include "BufferSTL.h"
#include "BufferSTL.tcc"
namespace adios2
{
namespace format
{
BufferSTL::BufferSTL() : Buffer("BufferSTL") {}
char *BufferSTL::Data() noexcept { return m_Buffer.data(); }
const char *BufferSTL::Data() const noexcept { return m_Buffer.data(); }
void BufferSTL::Resize(const size_t size, const std::string hint)
{
try
{
// doing this will effectively replace the STL GNU default power of 2
// reallocation.
m_Buffer.reserve(size);
// must initialize memory (secure)
m_Buffer.resize(size, '\0');
}
catch (...)
{
// catch a bad_alloc
std::throw_with_nested(std::runtime_error(
"ERROR: buffer overflow when resizing to " + std::to_string(size) +
" bytes, " + hint + "\n"));
}
}
void BufferSTL::Reset(const bool resetAbsolutePosition,
const bool zeroInitialize)
{
m_Position = 0;
if (resetAbsolutePosition)
{
m_AbsolutePosition = 0;
}
if (zeroInitialize)
{
m_Buffer.assign(m_Buffer.size(), '\0');
}
}
size_t BufferSTL::GetAvailableSize() const
{
return m_Buffer.size() - m_Position;
}
#define declare_template_instantiation(T) \
template size_t BufferSTL::Align<T>() const noexcept;
ADIOS2_FOREACH_PRIMITIVE_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
void BufferSTL::Delete() { std::vector<char>().swap(m_Buffer); }
size_t BufferSTL::DebugGetSize() const { return m_Buffer.size(); };
} // end namespace format
} // end namespace adios2