IsoSpec  1.95
dirtyAllocator.cpp
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 
18 #include <iostream>
19 #include <stdlib.h>
20 #include "dirtyAllocator.h"
21 
22 namespace IsoSpec
23 {
24 
25 DirtyAllocator::DirtyAllocator(
26  const int dim, const int tabSize
27 ): tabSize(tabSize)
28 {
29  cellSize = sizeof(double) + sizeof(int) * dim;
30  // Fix memory alignment problems for SPARC
31  if(cellSize % sizeof(double) != 0)
32  cellSize += sizeof(double) - cellSize % sizeof(double);
33  currentTab = malloc( cellSize * tabSize );
34  currentConf = currentTab;
35  endOfTablePtr = reinterpret_cast<char*>(currentTab) + cellSize*tabSize;
36 }
37 
38 
39 DirtyAllocator::~DirtyAllocator()
40 {
41  for(unsigned int i = 0; i < prevTabs.size(); ++i) free(prevTabs[i]);
42  free(currentTab);
43 }
44 
45 void DirtyAllocator::shiftTables()
46 {
47  prevTabs.push_back(currentTab);
48 
49  currentTab = malloc( cellSize * tabSize );
50  currentConf = currentTab;
51  endOfTablePtr = reinterpret_cast<char*>(currentTab) + cellSize*tabSize;
52 }
53 
54 } // namespace IsoSpec
55