Macaulay2 Engine
Loading...
Searching...
No Matches
myalloc.hpp
Go to the documentation of this file.
1// Copyright 2019, The Macaulay2 team.
2
3#ifndef __myalloc_hpp_
4#define __myalloc_hpp_
5
36
37#include <iostream>
38
39// This class is static as it appears easiest if all allocator objects
40// are essentially identical. It could be a static member of StatsAllocator,
41// but then each type T would have a different stats object.
42//
57// This class is meant for debugging/benchmark use only.
58// This class is not thread safe.
59//
60// TODO: perhaps include mathicgb logging facility, or perhaps even boost.
61// However, this is much simpler for the moment.
63{
64public:
65 static size_t mNumAllocs;
66 static size_t mAllocSize;
67 static size_t mNumDeallocs;
68 static long mCurrentAllocSize;
69 static size_t mHighWater;
70
71 static void reset() {
72 mNumAllocs = 0;
73 mAllocSize = 0;
74 mNumDeallocs = 0;
76 mHighWater = 0;
77 }
78
79 static void logAlloc(size_t num, size_t sz)
80 {
82 AllocLogger::mAllocSize += num * sz;
86 // std::cout << "allocating " << num << " elements, each of size " << sz << std::endl;
87 }
88
89 static void logDealloc(size_t num, size_t sz)
90 {
93 // std::cout << "deallocating " << num << " elements, each of size " << sz << std::endl;
94 }
95};
96
97std::ostream& operator<<(std::ostream& o, AllocLogger a);
98
99/* The following code is adapted from the book
100 * "The C++ Standard Library - A Tutorial and Reference"
101 * by Nicolai M. Josuttis, Addison-Wesley, 2nd edition, 2012.
102 */
103
104template <typename T>
106public:
107 typedef T value_type;
108
109 StatsAllocator() noexcept {}
110
111 template <typename U>
113
114 T* allocate(size_t num)
115 {
116 AllocLogger::logAlloc(num, sizeof(T));
117 return static_cast<T*>(::operator new (num * sizeof(T)));
118 }
119
120 void deallocate(T* p, std::size_t num)
121 {
122 AllocLogger::logDealloc(num, sizeof(T));
123 ::operator delete(p);
124 }
125};
126
127// all specializations of this allocator are interchangeable
128template <class T1, class T2>
130 const StatsAllocator<T2>&) noexcept {
131 return true;
132}
133template <class T1, class T2>
135 const StatsAllocator<T2>&) noexcept {
136 return false;
137}
138
139#endif
140
141// Local Variables:
142// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
143// indent-tabs-mode: nil
144// End:
static size_t mNumDeallocs
Definition myalloc.hpp:67
static long mCurrentAllocSize
Definition myalloc.hpp:68
static size_t mAllocSize
Definition myalloc.hpp:66
static void logAlloc(size_t num, size_t sz)
Definition myalloc.hpp:79
static void reset()
Definition myalloc.hpp:71
static size_t mHighWater
Definition myalloc.hpp:69
static size_t mNumAllocs
Definition myalloc.hpp:65
static void logDealloc(size_t num, size_t sz)
Definition myalloc.hpp:89
Process-wide allocation counter used by StatsAllocator for debugging and benchmarking.
Definition myalloc.hpp:63
T * allocate(size_t num)
Definition myalloc.hpp:114
StatsAllocator() noexcept
Definition myalloc.hpp:109
StatsAllocator(const StatsAllocator< U > &) noexcept
Definition myalloc.hpp:112
void deallocate(T *p, std::size_t num)
Definition myalloc.hpp:120
int p
bool operator!=(const StatsAllocator< T1 > &, const StatsAllocator< T2 > &) noexcept
Definition myalloc.hpp:134
bool operator==(const StatsAllocator< T1 > &, const StatsAllocator< T2 > &) noexcept
Definition myalloc.hpp:129
std::ostream & operator<<(std::ostream &o, AllocLogger a)
Definition myalloc.cpp:9
#define T
Definition table.c:13