IsoSpec  1.95
allocator.h
1 /*
2  * Copyright (C) 2015-2019 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 #include "conf.h"
23 
24 namespace IsoSpec
25 {
26 
27 template <typename T> inline void copyConf(
28  const T* source, T* destination,
29  int dim
30 ){
31  memcpy(destination, source, dim*sizeof(T));
32 }
33 
34 template <typename T> class Allocator{
35 private:
36  T* currentTab;
37  int currentId;
38  const int dim, tabSize;
39  std::vector<T*> prevTabs;
40 public:
41  Allocator(const int dim, const int tabSize = 10000);
42  ~Allocator();
43 
44  void shiftTables();
45 
46  inline T* newConf()
47  {
48  currentId++;
49 
50  if (currentId >= tabSize)
51  shiftTables();
52 
53  return &(currentTab[ currentId * dim ]);
54  }
55 
56  inline T* makeCopy(const T* conf)
57  {
58  T* currentPlace = newConf();
59  copyConf<T>( conf, currentPlace, dim );
60 
61  return currentPlace;
62  }
63 
64  inline T* makeExternalCopy(const T* conf)
65  {
66  T* res = new T[dim];
67  copyConf( conf, res, dim );
68 
69  return res;
70  }
71 };
72 
73 }
74 
IsoSpec::Allocator
Definition: allocator.h:34
IsoSpec
Definition: allocator.cpp:21