Macaulay2 Engine
Loading...
Searching...
No Matches
memblock.hpp
Go to the documentation of this file.
1// Copyright 2004 Michael E. Stillman
2#ifndef __memblock_h_
3#define __memblock_h_
4
34
35#include "newdelete.hpp"
36
37template <typename T, long int NSLAB = 4092>
39{
40 struct slab : public our_new_delete
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// F4MemoryBlock //
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 != 0)
89 {
90 slab *tmp = first_slab;
91 first_slab = first_slab->next;
92 delete tmp;
93 }
94
95 current_slab = 0;
96 last_slab = 0;
97 next_free = 0;
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 != 0; 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:
slab * current_slab
Definition memblock.hpp:47
int n_slabs() const
Definition memblock.hpp:150
void intern(int len)
Definition memblock.hpp:136
slab * new_slab()
Definition memblock.hpp:101
slab * last_slab
Definition memblock.hpp:48
long memoryUsage() const
Definition memblock.hpp:158
T * allocate(int len=1)
Definition memblock.hpp:142
slab * first_slab
Definition memblock.hpp:46
T * reserve(int len)
Definition memblock.hpp:116
int p
VALGRIND_MAKE_MEM_DEFINED & result(result)
our_new_delete — per-class opt-in routing of new / delete through bdwgc.
#define T
Definition table.c:13