Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
SimpleGC.h
1 /*
2  * SimpleGC class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2014, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_ALLOC_SIMPLEGC_H_
22 #define ELM_ALLOC_SIMPLEGC_H_
23 
24 #include <elm/util/BitVector.h>
25 #include <elm/stree/Tree.h>
26 #include <elm/data/List.h>
27 #include <elm/data/BiDiList.h>
28 #include <elm/alloc/DefaultAllocator.h>
29 
30 namespace elm {
31 
32 class SimpleGC;
33 
34 class Temp: public inhstruct::DLNode {
35 public:
36  inline Temp(SimpleGC& gc);
37  virtual ~Temp(void);
38  virtual void collect(SimpleGC& gc) = 0;
39 };
40 
41 template <class T>
42 class TempPtr: public Temp {
43 public:
44  inline TempPtr(SimpleGC& gc, T *ptr): Temp(gc), p(ptr) { }
45  virtual void collect(SimpleGC& gc) { p->collect(gc); }
46 
47  inline operator T *(void) const { return p; }
48  inline T *operator&(void) const { return *p; }
49  inline T *operator->(void) const { return *p; }
50  inline T& operator*(void) const { return *p; }
51  inline operator bool(void) const { return p; }
52  inline TempPtr<T>& operator=(T *ptr) { p = ptr; return *this; }
53 
54 private:
55  T *p;
56 };
57 
58 class SimpleGC {
59  friend class Temp;
60 public:
61  SimpleGC(t::size size = 4096);
62  virtual ~SimpleGC(void);
63  void clear(void);
64  void doGC(void);
65 
66  void *allocate(t::size size);
67  inline void free(void *block) { }
68 
69 protected:
70  bool mark(void *data, t::size size);
71 
72  virtual void beginGC(void);
73  virtual void collect(void) = 0;
74  virtual void endGC(void);
75 
76 private:
77  void newChunk(void);
78  void *allocFromFreeList(t::size size);
79 
80  typedef struct block_t {
81  block_t *next;
83  } block_t;
84  typedef struct chunk_t {
85  BitVector *bits;
86  t::uint8 buffer[0];
87  } chunk_t;
88 
89  List<chunk_t *> chunks;
90  t::size csize;
91  block_t *free_list;
92  inhstruct::DLList temps;
93 
94  typedef stree::Tree<void *, chunk_t *> tree_t;
95  tree_t *st;
96 
97  static inline t::size round(t::size size) { return (size + sizeof(block_t) - 1) & ~(sizeof(block_t) - 1); }
98 };
99 
100 } // elm
101 
102 #endif /* ELM_ALLOC_SIMPLEGC_H_ */
elm::Temp
Definition: SimpleGC.h:34
elm::TempPtr::operator*
T & operator*(void) const
Definition: SimpleGC.h:50
elm::TempPtr
Definition: SimpleGC.h:42
elm::TempPtr::operator->
T * operator->(void) const
Definition: SimpleGC.h:49
elm::TempPtr::operator&
T * operator&(void) const
Definition: SimpleGC.h:48
elm::SimpleGC::doGC
void doGC(void)
Definition: alloc_SimpleGC.cpp:121
elm::TempPtr::TempPtr
TempPtr(SimpleGC &gc, T *ptr)
Definition: SimpleGC.h:44
elm::SimpleGC::SimpleGC
SimpleGC(t::size size=4096)
Definition: alloc_SimpleGC.cpp:84
void
bool
elm::TempPtr::operator=
TempPtr< T > & operator=(T *ptr)
Definition: SimpleGC.h:52
elm
Definition: adapter.h:26
elm::t::size
uint64 size
Definition: arch.h:35
elm::SimpleGC::endGC
virtual void endGC(void)
Definition: alloc_SimpleGC.cpp:231
elm::t::uint8
unsigned char uint8
Definition: arch.h:27
elm::SimpleGC::free
void free(void *block)
Definition: SimpleGC.h:67
elm::SimpleGC
Definition: SimpleGC.h:58
elm::TempPtr::collect
virtual void collect(SimpleGC &gc)
Definition: SimpleGC.h:45
elm::Temp::~Temp
virtual ~Temp(void)
Definition: alloc_SimpleGC.cpp:46
elm::Temp::collect
virtual void collect(SimpleGC &gc)=0
elm::SimpleGC::mark
bool mark(void *data, t::size size)
Definition: alloc_SimpleGC.cpp:182
elm::SimpleGC::collect
virtual void collect(void)=0
elm::SimpleGC::~SimpleGC
virtual ~SimpleGC(void)
Definition: alloc_SimpleGC.cpp:105
elm::SimpleGC::clear
void clear(void)
Definition: alloc_SimpleGC.cpp:112
elm::Temp::Temp
Temp(SimpleGC &gc)
Definition: alloc_SimpleGC.cpp:40
elm::SimpleGC::allocate
void * allocate(t::size size)
Definition: alloc_SimpleGC.cpp:152
elm::SimpleGC::beginGC
virtual void beginGC(void)
Definition: alloc_SimpleGC.cpp:205
elm::inhstruct::DLNode
Definition: DLList.h:30
elm::t::intptr
uint64 intptr
Definition: arch.h:38