Frobby  0.9.5
LocalArray.h
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2011 University of Aarhus
3  Contact Bjarke Hammersholt Roune for license information (www.broune.com)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see http://www.gnu.org/licenses/.
17 */
18 #ifndef LOCAL_ARRAY_GUARD
19 #define LOCAL_ARRAY_GUARD
20 
21 #include "Arena.h"
22 #include <utility>
23 
35 template<class T, class ArenaSource = Arena>
36 class LocalArray {
37  public:
38  LocalArray(const size_t sizeParam):
39  _size(sizeParam),
40  _range(gccWorkAround(sizeParam)) {
41  ASSERT(this->size() == static_cast<size_t>(end() - begin()));
42  }
43 
45  ASSERT(size() == static_cast<size_t>(end() - begin()));
46  ArenaSource::getArena().freeTopArray(begin(), end());
47  }
48 
49  T& operator[](const size_t i) const {
50  ASSERT(i < size());
51  return begin()[i];
52  }
53 
54  T* begin() const {return _range.first;}
55  T* end() const {return _range.second;}
56  size_t size() const {return _size;}
57 
58  private:
59  static const pair<T*, T*> gccWorkAround(const size_t size) {
60  // for some reason Cygwin's GCC 4.3.4 will not accept this code:
61  // ArenaSource::getArena().allocArray<T>(size)
62  // but arena.allocArray<T>(size) is fine. So don't change it back.
63  // Unfortunately we need to put this in an initializer so it has to
64  // fit on one line so the work-around has to go in a separate function.
65  Arena& arena = ArenaSource::getArena();
66  return arena.allocArray<T>(size);
67  }
68 
69  // Everything is inline and should be on the stack and be unlikely
70  // to have its address taken. So the compiler should be able to
71  // eliminate any of these member variables that are not used.
72  const size_t _size;
73  const pair<T*, T*> _range;
74 };
75 
76 #endif
This is an arena allocator.
Definition: Arena.h:53
pair< T *, T * > allocArray(size_t elementCount)
As allocArrayNoCon except that constructors for the elements of the array are called.
Definition: Arena.h:263
Emulates stack allocation of an array using an Arena.
Definition: LocalArray.h:36
T * begin() const
Definition: LocalArray.h:54
const pair< T *, T * > _range
Definition: LocalArray.h:73
LocalArray(const size_t sizeParam)
Definition: LocalArray.h:38
T & operator[](const size_t i) const
Definition: LocalArray.h:49
size_t size() const
Definition: LocalArray.h:56
const size_t _size
Definition: LocalArray.h:72
T * end() const
Definition: LocalArray.h:55
static const pair< T *, T * > gccWorkAround(const size_t size)
Definition: LocalArray.h:59
#define ASSERT(X)
Definition: stdinc.h:86