1 #ifndef MOLPRO_MEMORY_H
2 #define MOLPRO_MEMORY_H
9 #include <unordered_map>
18 #include <initializer_list>
25 #ifdef MOLPRO_MEMORY_FORTRAN
38 double* memory_allocate(
size_t n);
47 void memory_release(
char* p,
int all = 1);
58 void memory_resize(
char** p,
size_t n);
70 void memory_release_saved(
size_t p);
95 void memory_module_test_fortran(
int printlevel);
123 std::cout <<
"Memory used: " <<
memory_used() << std::endl;
143 template<
typename T,
typename A = std::allocator<T> >
145 using a_t = std::allocator_traits<A>;
169 #ifdef MOLPRO_MEMORY_FORTRAN
176 throw std::runtime_error(std::string(
"molpro::allocate: insufficient remaining stack memory; remaining=")
178 + std::string(
", requested=")
179 + std::to_string(
static_cast<unsigned long long>(cnt *
sizeof(T))));
180 #ifdef MOLPRO_MEMORY_FORTRAN
181 return reinterpret_cast<pointer>(memory_allocate(cnt *
sizeof(T)));
186 auto result =
reinterpret_cast<pointer>( malloc(cnt *
sizeof (T)));
188 void* pp = ::operator
new[](cnt *
sizeof(T));
189 auto result =
reinterpret_cast<pointer>( pp);
197 #ifdef MOLPRO_MEMORY_FORTRAN
198 memory_release((
char*) p, 0);
204 free(
reinterpret_cast<char*
>(p));
206 delete[]
reinterpret_cast<char*
>(p);
213 return std::numeric_limits<size_type>::max();
220 noexcept(std::is_nothrow_default_constructible<U>::value) {
221 ::new(
static_cast<void*
>(ptr)) U;
223 template<
typename U,
typename...Args>
225 a_t::construct(
static_cast<A&
>(*
this),
226 ptr, std::forward<Args>(
args)...);
238 template<
typename T=
double,
typename A=molpro::allocator<T> >
272 template<
typename T=
double,
typename _Alloc=molpro::allocator<T>>
278 template<
typename T=
double,
typename _Alloc=molpro::allocator<T>>
286 std::vector<T, _Alloc> m_stdvector;
296 : m_stdvector(length), m_buffer(m_stdvector.
data()) {}
304 : m_stdvector(length, value), m_buffer(m_stdvector.
data()) {}
311 template<
class InputIterator>
312 vector<T, _Alloc>(InputIterator first, InputIterator last)
313 : m_stdvector(first, last), m_buffer(m_stdvector.
data()) {}
319 vector<T, _Alloc>(std::initializer_list<T> il) : m_stdvector(il), m_buffer(m_stdvector.
data()) {}
325 vector<T, _Alloc>(
const vector<T, _Alloc>& source) {
326 m_stdvector.insert(m_stdvector.begin(), source.begin(), source.end());
327 m_buffer = m_stdvector.data();
334 if (© ==
this)
return *
this;
336 std::copy(copy.
begin(), copy.
end(), this->begin());
343 return m_stdvector[n];
346 return m_stdvector[n];
353 template<
class InputIterator>
354 void assign(InputIterator first, InputIterator last) {
355 if (last - first != m_stdvector.size())
resize(last - first);
356 std::copy(first, last,
begin());
371 m_stdvector.assign(n, value);
377 void assign(std::initializer_list<T> il) {
378 m_stdvector.assign(il);
382 return m_stdvector.at(n);
385 const T&
at(
size_t n)
const {
386 return m_stdvector.at(n);
390 return m_stdvector.back();
393 const T&
back() const noexcept {
394 return m_stdvector.back();
398 return m_stdvector.capacity();
406 return m_stdvector.empty();
410 return m_stdvector.front();
414 return m_stdvector.front();
418 const T*
data() const noexcept {
return m_stdvector.data(); }
421 return m_stdvector.max_size();
425 return m_stdvector.size();
435 std::swap(a.m_stdvector, b.m_stdvector);
438 template <
bool IsConst>
449 template<
bool IsConst_ = IsConst,
class = std::enable_if_t<IsConst_>>
502 return &(*m_stdvector.erase(m_stdvector.begin() + (&(*pos) -
data())));
506 return &(*m_stdvector.erase(m_stdvector.begin() + (&(*first) -
data()),
507 m_stdvector.begin() + (&(*last) -
data())));
516 std::string
str(
int verbosity = 1,
unsigned int columns = UINT_MAX)
const {
517 std::ostringstream s;
520 else if (verbosity > 0) {
521 for (
size_t i = 0; i <
size(); i++) {
535 m_stdvector.reserve(new_cap);
536 m_buffer = m_stdvector.data();
544 m_stdvector.resize(n);
545 m_buffer = m_stdvector.data();
554 m_stdvector.resize(n, val);
555 m_buffer = m_stdvector.data();
559 m_stdvector.shrink_to_fit();
560 m_buffer = m_stdvector.data();
564 m_stdvector.pop_back();
568 m_stdvector.push_back(value);
569 m_buffer = m_stdvector.data();
573 m_stdvector.push_back(std::forward<T>(value));
574 m_buffer = m_stdvector.data();
577 template<
class... Args>
579 m_stdvector.emplace_back(std::forward<Args>(
args)...);
580 m_buffer = m_stdvector.data();
583 template<
class... Args>
585 m_stdvector.emplace(pos, std::forward<Args>(
args)...);
586 m_buffer = m_stdvector.data();
590 template<
class T,
typename _Alloc>
592 return os << obj.
str();
595 template<
typename T=
double,
typename _Alloc=molpro::allocator<T>>
602 template <
typename T,
typename _Alloc>
611 template <
typename T,
typename _Alloc>
665 template<
typename T=
double>
683 explicit array<T>(
size_t const length = 0)
684 : m_allocator(), m_length(length), m_owned(true)
686 , m_buffer(m_allocator.allocate(length)) {
695 array<T>(
size_t const length,
const T& value)
696 : m_allocator(), m_length(length), m_owned(true), m_buffer(m_allocator.allocate(length)) {
assign(value); }
703 template<
class InputIterator>
704 array<T>(InputIterator
708 : m_allocator(), m_length(last - first), m_buffer(m_allocator.allocate(last - first)), m_owned(true) {}
714 array<T>(std::initializer_list<T>
716 : m_allocator(), m_length(il.
size()),
717 m_buffer(m_allocator.allocate(il.
size())), m_owned(true) { std::copy(il.begin(), il.end(),
begin()); }
724 array<T>(T*
const buffer,
size_t const length)
726 m_length(length), m_owned(false), m_buffer(buffer) {}
732 template<std::
size_t N>
733 explicit array<T>(std::array<T, N>&
array)
740 explicit array<T>(std::vector<T>&
array)
747 array<T>(
const array<T>& source)
748 : m_allocator(), m_length(source.
size()), m_owned(true), m_buffer(m_allocator.allocate(source.
size())) {
749 std::copy(source.begin(), source.end(),
begin());
756 if (© ==
this)
return *
this;
757 if (copy.
size() != m_length)
throw std::runtime_error(
"Unequal length copy not supported");
758 std::copy(copy.
begin(), copy.
end(), this->begin());
763 if (m_owned) m_allocator.
deallocate(m_buffer, m_length);
767 assert(m_length == 0 || (n <= m_length));
771 assert(m_length == 0 || (n <= m_length));
779 template<
class InputIterator>
780 void assign(InputIterator first, InputIterator last) {
781 if (last - first != m_length)
throw std::runtime_error(
"Unequal lengths in copy");
782 std::copy(first, last,
begin());
798 if (n != m_length)
throw std::runtime_error(
"Unequal lengths in copy");
806 void assign(std::initializer_list<T> il) {
807 if (il.size() != m_length)
throw std::runtime_error(
"Unequal lengths in copy");
808 std::copy(il.begin(), il.end(),
begin());
812 if (n < m_length)
return m_buffer[n];
else throw std::out_of_range(
"array::at() out of range");
815 const T&
at(
size_t n)
const {
816 if (n < m_length)
return m_buffer[n];
else throw std::out_of_range(
"array::at() out of range");
820 return m_buffer[
size() - 1];
823 const T&
back() const noexcept {
824 return m_buffer[
size() - 1];
828 return m_length == 0;
840 const T*
data() const noexcept {
return m_buffer; }
857 std::swap(a.m_buffer, b.m_buffer);
858 std::swap(a.m_length, b.m_length);
859 std::swap(a.m_owned, b.m_owned);
862 template <
bool IsConst>
873 template<
bool IsConst_ = IsConst,
class = std::enable_if_t<IsConst_>>
924 std::string
str(
int verbosity = 1,
unsigned int columns = UINT_MAX)
const {
925 std::ostringstream s;
928 else if (verbosity > 0) {
929 for (
size_t i = 0; i <
size(); i++) {
945 return os << obj.
str();
size_type max_size() const
Definition: memory.h:212
value_type & reference
Definition: memory.h:151
value_type * pointer
Definition: memory.h:149
pointer address(reference r)
Definition: memory.h:164
void deallocate(pointer p, size_type)
Definition: memory.h:196
std::ptrdiff_t difference_type
Definition: memory.h:154
std::size_t size_type
Definition: memory.h:153
bool operator==(allocator_ const &)
Definition: memory.h:229
const value_type * const_pointer
Definition: memory.h:150
void construct(U *ptr) noexcept(std::is_nothrow_default_constructible< U >::value)
Definition: memory.h:219
const_pointer address(const_reference r) const
Definition: memory.h:165
T value_type
Definition: memory.h:148
const value_type & const_reference
Definition: memory.h:152
bool operator!=(allocator_ const &a)
Definition: memory.h:230
pointer allocate(size_type cnt, typename std::allocator< void >::const_pointer=nullptr)
Definition: memory.h:168
void construct(U *ptr, Args &&... args)
Definition: memory.h:224
std::forward_iterator_tag iterator_category
Definition: memory.h:865
MyIterator operator++(int)
Definition: memory.h:879
MyIterator & operator++()
Definition: memory.h:878
MyIterator & operator--()
Definition: memory.h:880
friend bool operator<=(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:886
T & reference
Definition: memory.h:869
friend bool operator>(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:885
T * pointer
Definition: memory.h:868
friend bool operator>=(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:887
T value_type
Definition: memory.h:866
pointer operator->() const noexcept
Definition: memory.h:877
MyIterator(const MyIterator< false > &rhs)
Definition: memory.h:874
reference operator*() const noexcept
Definition: memory.h:876
MyIterator(pointer ptr)
Definition: memory.h:871
MyIterator() noexcept
Definition: memory.h:870
std::ptrdiff_t difference_type
Definition: memory.h:867
friend bool operator==(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:882
friend bool operator!=(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:883
MyIterator(const MyIterator &)=default
friend bool operator<(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:884
MyIterator operator--(int)
Definition: memory.h:881
A template for a container class like std::array<T> but with the following features.
Definition: memory.h:666
iterator begin() noexcept
Definition: memory.h:900
const T & operator[](size_t n) const
Definition: memory.h:770
const_iterator cend() const noexcept
Definition: memory.h:906
const_reverse_iterator crend() const noexcept
Definition: memory.h:915
std::reverse_iterator< Iterator > reverse_iterator
Definition: memory.h:908
void assign(InputIterator first, InputIterator last)
Assign new contents to the array, replacing its current contents.
Definition: memory.h:780
T & back() noexcept
Definition: memory.h:819
const_iterator begin() const noexcept
Definition: memory.h:901
void swap(array< T > &x)
Exchange the content of the container by the content of x, which is another object of the same type.
Definition: memory.h:854
friend void swap(array< T > &a, array< T > &b)
Definition: memory.h:856
std::string str(int verbosity=1, unsigned int columns=UINT_MAX) const
Generate a printable representation of the object.
Definition: memory.h:924
reverse_iterator rbegin() noexcept
Definition: memory.h:910
const T * data() const noexcept
Definition: memory.h:840
virtual ~array()
Definition: memory.h:762
std::reverse_iterator< ConstIterator > const_reverse_iterator
Definition: memory.h:909
size_t size() const noexcept
Definition: memory.h:846
const_iterator end() const noexcept
Definition: memory.h:905
T & at(size_t n)
Definition: memory.h:811
array< T > & operator=(const array< T > ©)
Copy assignment operator.
Definition: memory.h:755
const_iterator cbegin() const noexcept
Definition: memory.h:902
void assign(const T &value)
Assign new contents to the array, replacing its current contents.
Definition: memory.h:788
iterator end() noexcept
Definition: memory.h:904
T * data() noexcept
Definition: memory.h:839
const T & front() const noexcept
Definition: memory.h:835
reverse_iterator rend() noexcept
Definition: memory.h:913
const T & back() const noexcept
Definition: memory.h:823
T & front() noexcept
Definition: memory.h:831
Iterator iterator
Definition: memory.h:897
void assign(size_t n, const T &value)
Assign new contents to the array, replacing its current contents.
Definition: memory.h:796
ConstIterator const_iterator
Definition: memory.h:898
const T & at(size_t n) const
Definition: memory.h:815
void assign(std::initializer_list< T > il)
Assign new contents to the array, replacing its current contents.
Definition: memory.h:806
array(size_t const length=0)
Construct an array of type T with managed storage.
Definition: memory.h:683
T & operator[](size_t n)
Definition: memory.h:766
const_reverse_iterator rbegin() const noexcept
Definition: memory.h:911
const_reverse_iterator crbegin() const noexcept
Definition: memory.h:912
bool empty() const noexcept
Definition: memory.h:827
const_reverse_iterator rend() const noexcept
Definition: memory.h:914
size_t max_size() const noexcept
Definition: memory.h:842
A template for a container class like std::vector<T> but with the following features.
Definition: memory.h:273
pointer_holder(T *const ptr)
Definition: memory.h:275
T * m_ptr
Definition: memory.h:276
T value_type
Definition: memory.h:442
friend MyIterator operator+(const MyIterator &rhs, difference_type n)
Definition: memory.h:461
friend MyIterator operator-(const MyIterator &rhs, difference_type n)
Definition: memory.h:463
friend bool operator<=(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:468
std::forward_iterator_tag iterator_category
Definition: memory.h:441
MyIterator & operator+=(difference_type n)
Definition: memory.h:458
friend bool operator>(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:467
std::ptrdiff_t difference_type
Definition: memory.h:443
MyIterator() noexcept
Definition: memory.h:446
MyIterator(const MyIterator &)=default
MyIterator & operator-=(difference_type n)
Definition: memory.h:459
MyIterator & operator--()
Definition: memory.h:456
T & reference
Definition: memory.h:445
MyIterator operator++(int)
Definition: memory.h:455
MyIterator(pointer ptr)
Definition: memory.h:447
friend bool operator>=(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:469
friend bool operator==(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:464
MyIterator operator--(int)
Definition: memory.h:457
friend bool operator!=(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:465
MyIterator(const MyIterator< false > &rhs)
Definition: memory.h:450
T * pointer
Definition: memory.h:444
friend bool operator<(const MyIterator &lhs, const MyIterator &rhs)
Definition: memory.h:466
pointer operator->() const noexcept
Definition: memory.h:453
MyIterator & operator++()
Definition: memory.h:454
reference operator*() const noexcept
Definition: memory.h:452
friend MyIterator operator+(difference_type n, const MyIterator &rhs)
Definition: memory.h:460
friend MyIterator operator-(difference_type n, const MyIterator &rhs)
Definition: memory.h:462
T * data() noexcept
Definition: memory.h:417
void resize(size_t n)
Resize the buffer.
Definition: memory.h:543
void push_back(T &&value)
Definition: memory.h:572
iterator end() noexcept
Definition: memory.h:488
ConstIterator const_iterator
Definition: memory.h:482
iterator emplace(const_iterator pos, Args &&... args)
Definition: memory.h:584
std::reverse_iterator< ConstIterator > const_reverse_iterator
Definition: memory.h:493
bool empty() const noexcept
Definition: memory.h:405
reverse_iterator rend() noexcept
Definition: memory.h:497
std::string str(int verbosity=1, unsigned int columns=UINT_MAX) const
Generate a printable representation of the object.
Definition: memory.h:516
Iterator iterator
Definition: memory.h:481
const T & front() const noexcept
Definition: memory.h:413
void assign(InputIterator first, InputIterator last)
Assign new contents to the vector, replacing its current contents.
Definition: memory.h:354
void assign(std::initializer_list< T > il)
Assign new contents to the vector, replacing its current contents.
Definition: memory.h:377
void clear() noexcept
Definition: memory.h:401
iterator erase(const_iterator first, const_iterator last)
Definition: memory.h:505
void shrink_to_fit()
Definition: memory.h:558
std::reverse_iterator< Iterator > reverse_iterator
Definition: memory.h:492
reverse_iterator rbegin() noexcept
Definition: memory.h:494
void emplace_back(Args &&... args)
Definition: memory.h:578
iterator begin() noexcept
Definition: memory.h:484
const T * data() const noexcept
Definition: memory.h:418
T & at(size_t n)
Definition: memory.h:381
const T & operator[](size_t n) const
Definition: memory.h:345
T & back() noexcept
Definition: memory.h:389
const_iterator begin() const noexcept
Definition: memory.h:485
size_t size() const noexcept
Definition: memory.h:424
void reserve(size_t new_cap)
Definition: memory.h:534
const_iterator cbegin() const noexcept
Definition: memory.h:486
const_iterator cend() const noexcept
Definition: memory.h:490
void resize(size_t n, const T &val)
Resize the buffer, and assign a value to any new elements if it grows.
Definition: memory.h:553
const T & back() const noexcept
Definition: memory.h:393
void push_back(const T &value)
Definition: memory.h:567
MyIterator< false > Iterator
Definition: memory.h:478
T & front() noexcept
Definition: memory.h:409
iterator erase(const_iterator pos)
Definition: memory.h:501
size_t capacity() const noexcept
Definition: memory.h:397
const_reverse_iterator rbegin() const noexcept
Definition: memory.h:495
void swap(vector< T, _Alloc > &x)
Exchange the content of the container by the content of x, which is another object of the same type.
Definition: memory.h:432
friend void swap(vector< T, _Alloc > &a, vector< T, _Alloc > &b)
Definition: memory.h:434
size_t max_size() const noexcept
Definition: memory.h:420
const_iterator end() const noexcept
Definition: memory.h:489
void assign(const T &value)
Assign new contents to the vector, replacing its current contents.
Definition: memory.h:362
const T & at(size_t n) const
Definition: memory.h:385
vector< T, _Alloc > & operator=(const vector< T, _Alloc > ©)
Copy assignment operator.
Definition: memory.h:333
const_reverse_iterator crbegin() const noexcept
Definition: memory.h:496
void pop_back()
Definition: memory.h:563
void assign(size_t n, const T &value)
Assign new contents to the vector, replacing its current contents.
Definition: memory.h:370
virtual ~vector()=default
const_reverse_iterator rend() const noexcept
Definition: memory.h:498
const_reverse_iterator crend() const noexcept
Definition: memory.h:499
T & operator[](size_t n)
Definition: memory.h:342
size_t memory_used(int maximum=0)
memory_used Report used memory
Definition: memory.h:113
size_t memory_initialize(char *buffer, size_t max)
Definition: memory.h:104
std::unordered_map< char *, size_t > _private_memory_lengths
Definition: memoryC.cpp:5
size_t _private_memory_maximum_allocatable
Definition: memoryC.cpp:4
size_t _private_memory_used
Definition: memoryC.cpp:3
void memory_print_status()
memory_print_status Print the state of the memory
Definition: memory.h:122
size_t memory_remaining()
memory_remaining Report used memory
Definition: memory.h:118
void memory_reset_maximum_stack(int64_t level)
memory_reset_maximum_stack Reset the maximum stack used statistic to the currently-used stack
Definition: memory.h:132
Class that manages input options.
Definition: iostream.h:14
vector< T, _Alloc >::Iterator operator+(const typename vector< T, _Alloc >::Iterator &a, int increment)
Definition: memory.h:603
std::ptrdiff_t operator-(const pointer_holder< T > &a, const pointer_holder< T > &b)
Definition: memory.h:596
std::vector< T, A > stdvector
Definition: memory.h:239
std::string args(int argc, char **argv)
Definition: Options.cpp:27
std::ostream & operator<<(std::ostream &os, vector< T, _Alloc > const &obj)
Definition: memory.h:591
allocator_< U, typename a_t::template rebind_alloc< U > > other
Definition: memory.h:159