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