Macaulay2 Engine
Loading...
Searching...
No Matches
res-memblock.hpp
Go to the documentation of this file.
1// Copyright 2004-2016 Michael E. Stillman
2#ifndef __res_memblock_hpp_
3#define __res_memblock_hpp_
4
36
37template <typename T, long int NSLAB = 4092>
39{
40 struct slab
41 {
43 T block[NSLAB];
44 };
45
49 T *next_free; /* points into current_slab */
50
51 private:
52 slab *new_slab();
53
54 public:
57
58 void reset();
59
60 T *reserve(int len); // returns space for len T's.
61 void intern(int len); // increments
62 T *allocate(int len = 1); // reserve and intern
63
64 int n_slabs() const;
65
67 const; // total number of bytes allocated in slabs (plus size of this)
68};
69
71// ResMemoryBlock //
73
74template <typename T, long int NSLAB>
83
84template <typename T, long int NSLAB>
86{
87 // Destroy the slabs one by one
88 while (first_slab != nullptr)
89 {
90 slab *tmp = first_slab;
91 first_slab = first_slab->next;
92 delete tmp;
93 }
94
95 current_slab = nullptr;
96 last_slab = nullptr;
97 next_free = nullptr;
98}
99
100template <typename T, long int NSLAB>
102{
103 slab *result = new slab;
104 result->next = nullptr;
105 return result;
106}
107
108template <typename T, long int NSLAB>
114
115template <typename T, long int NSLAB>
117{
118 if (next_free + len > current_slab->block + NSLAB)
119 {
120 if (current_slab->next == nullptr)
121 {
122 last_slab->next = new_slab();
123 last_slab = last_slab->next;
125 }
126 else
127 {
129 }
130 next_free = current_slab->block;
131 }
132 return next_free;
133}
134
135template <typename T, long int NSLAB>
137{
138 next_free += len;
139}
140
141template <typename T, long int NSLAB>
143{
144 T *result = reserve(len);
145 next_free += len;
146 return result;
147}
148
149template <typename T, long int NSLAB>
151{
152 int result = 0;
153 for (slab *p = first_slab; p != nullptr; p = p->next) result++;
154 return result;
155}
156
157template <typename T, long int NSLAB>
159{
160 long result = sizeof(*this);
161 result += n_slabs() * sizeof(slab);
162 return result;
163}
164
165#endif
166
167// Local Variables:
168// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
169// End:
long memoryUsage() const
void intern(int len)
int n_slabs() const
T * reserve(int len)
T * allocate(int len=1)
int p
VALGRIND_MAKE_MEM_DEFINED & result(result)
#define T
Definition table.c:13