Gröbner basis project
Codebase for research into Gröbner basis computation
goda.hpp
1 #ifndef __GODA_HPP_
2 #define __GODA_HPP_
3 
4 /*****************************************************************************\
5 * This file is part of DynGB. *
6 * *
7 * DynGB is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Foobar is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
19 \*****************************************************************************/
20 
21 #include <cstdlib>
22 #include <iostream>
23 
24 using std::cout; using std::endl;
25 
26 #include "system_constants.hpp"
27 
44 template <typename TYPE>
45 union goda_block {
49  TYPE * data;
50 };
51 
66 template <typename TYPE>
68 public:
74  goda_block<TYPE> * result
75  = (goda_block<TYPE> *)malloc(10000 * data_size*sizeof(TYPE)*sizeof(long)/sizeof(TYPE));
76  goda_block<TYPE> * tmp = result;
77  for (unsigned i=0; i < 9998; ++i) {
78  tmp->next = &(tmp[data_size]);
79  tmp += data_size;
80  }
81  tmp->next = nullptr;
82  return result;
83  }
91  : data_size(n)
92  {
93  big_blocks = block = allocate_new_block();
94  block->next = nullptr; block += data_size;
95  }
98  while (big_blocks != nullptr) {
99  goda_block<TYPE> * next_block = big_blocks->next;
100  free(big_blocks);
101  big_blocks = next_block;
102  }
103  }
108  TYPE * get_new_block() {
109  if (block == nullptr) {
110  block = allocate_new_block();
111  block->next = big_blocks; big_blocks = block; block += data_size;
112  }
113  TYPE * result = (TYPE *)block;
114  block = block->next;
115  return result;
116  }
121  void return_used_block(TYPE * freed_block) {
122  goda_block<TYPE> * new_head = (goda_block<TYPE> *)freed_block;
123  new_head->next = block;
124  block = new_head;
125  }
126 protected:
128  const unsigned data_size;
133 };
134 
135 #endif
special memory pool allocator for Grevlex_Order_Data and WGrevlex_Order_Data
Definition: goda.hpp:67
goda_block< TYPE > * block
pointer to the next free block
Definition: goda.hpp:130
TYPE * get_new_block()
allocates and returns a block of memory
Definition: goda.hpp:108
goda_block< TYPE > * big_blocks
pointer to the superblock of all blocks
Definition: goda.hpp:132
~Grading_Order_Data_Allocator()
releases all memory — you&#39;d better have freed yours!
Definition: goda.hpp:97
Grading_Order_Data_Allocator(NVAR_TYPE n)
sets allocator up for blocks of of type TYPE.
Definition: goda.hpp:90
TYPE * data
the data contained in this block
Definition: goda.hpp:49
void return_used_block(TYPE *freed_block)
returns a block of memory that is no longer needed to the pool
Definition: goda.hpp:121
heart of the memory pool allocator (Grading_Order_Data_Allocator)
Definition: goda.hpp:45
goda_block< TYPE > * allocate_new_block()
allocates a new superblock of almost 10000 blocks
Definition: goda.hpp:73
goda_block * next
pointer to the next free block
Definition: goda.hpp:47
const unsigned data_size
how many words to step from one block to the next
Definition: goda.hpp:128