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