IsoSpec  1.95
dirtyAllocator.h
1 /*
2  * Copyright (C) 2015-2018 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 #pragma once
18 
19 #include <vector>
20 #include <iostream>
21 #include <string.h>
22 
23 namespace IsoSpec
24 {
25 
27 private:
28  void* currentTab;
29  void* currentConf;
30  void* endOfTablePtr;
31  const int tabSize;
32  int cellSize;
33  std::vector<void*> prevTabs;
34 public:
35  DirtyAllocator(const int dim, const int tabSize = 10000);
36  ~DirtyAllocator();
37 
38  void shiftTables();
39 
40  inline void* newConf()
41  {
42  if (currentConf >= endOfTablePtr)
43  {
44  shiftTables();
45  }
46 
47  void* ret = currentConf;
48  currentConf = reinterpret_cast<char*>(currentConf) + cellSize;
49 
50  return ret;
51  }
52 
53  inline void* makeCopy(const void* conf)
54  {
55  void* currentPlace = newConf();
56 
57  memcpy(currentPlace, conf, cellSize);
58 
59  return currentPlace;
60  }
61 
62  inline void* makeExternalCopy(const void* conf)
63  {
64  void* res = malloc(cellSize);
65 
66  memcpy(res, conf, cellSize);
67 
68  return res;
69  }
70 };
71 
72 } // namespace IsoSpec
73