Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BlockAllocatorWithGC.h
1 /*
2  * BlockAllocatorWithGC class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2012, 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_BLOCK_ALLOCATOR_WITH_GC
22 #define ELM_ALLOC_BLOCK_ALLOCATOR_WITH_GC
23 
24 #include <elm/alloc/DefaultAllocator.h>
25 #include <elm/genstruct/Vector.h>
26 #include <elm/util/BitVector.h>
27 
28 namespace elm {
29 
30 // abstract version
32 public:
33  AbstractBlockAllocatorWithGC(t::size block_size, t::size chunk_size = 1 << 20);
34  virtual ~AbstractBlockAllocatorWithGC(void);
35  void *allocate(void) throw(BadAlloc);
36  void collectGarbage(void);
37  inline t::size blockSize(void) const { return bsize; }
38  inline t::size chunkSize(void) const { return csize; }
39  inline int freeCount(void) const { return free_cnt; }
40  int totalCount(void) const;
41  inline int usedCount(void) const { return totalCount() - freeCount(); }
42 
43  // Allocator concept compatibility
44  void *allocate(t::size size) throw(BadAlloc);
45  void free(void *block);
46 
47 protected:
48  bool mark(void *ptr);
49  virtual void collect(void) = 0;
50  virtual void beginGC(void);
51  virtual void endGC(void);
52 
53  typedef struct free_t { free_t *next; } free_t;
55  int free_cnt;
56 
57 private:
59  t::uint8 *top;
60  t::size bsize, csize;
61  BitVector *coll;
62 };
63 
64 // template version
65 template <class T>
67 public:
68  inline BlockAllocatorWithGC(t::size chunk_size = 1 << 20): AbstractBlockAllocatorWithGC(sizeof(T), chunk_size) { }
69  inline T *allocate(void) throw(BadAlloc) { return static_cast<T *>(AbstractBlockAllocatorWithGC::allocate()); }
70 
71 protected:
72  inline bool mark(T *b) { return AbstractBlockAllocatorWithGC::mark(b); }
73 };
74 
75 } // elm
76 
77 #endif // ELM_ALLOC_BLOCK_ALLOCATOR_WITH_GC
BlockAllocatorWithGC(t::size chunk_size=1<< 20)
Definition: BlockAllocatorWithGC.h:68
virtual void beginGC(void)
Definition: alloc_BlockAllocatorWithGC.cpp:186
int freeCount(void) const
Definition: BlockAllocatorWithGC.h:39
void * allocate(void)
Definition: alloc_BlockAllocatorWithGC.cpp:68
virtual void collect(void)=0
uint32 size
Definition: int.h:41
virtual ~AbstractBlockAllocatorWithGC(void)
Definition: alloc_BlockAllocatorWithGC.cpp:47
Definition: BitVector.h:33
bool mark(void *ptr)
Definition: alloc_BlockAllocatorWithGC.cpp:159
Definition: DefaultAllocator.h:31
virtual void endGC(void)
Definition: alloc_BlockAllocatorWithGC.cpp:195
int free_cnt
Definition: BlockAllocatorWithGC.h:55
struct elm::AbstractBlockAllocatorWithGC::free_t free_t
t::size blockSize(void) const
Definition: BlockAllocatorWithGC.h:37
int totalCount(void) const
Definition: alloc_BlockAllocatorWithGC.cpp:203
free_t * free_list
Definition: BlockAllocatorWithGC.h:54
uint8_t uint8
Definition: int.h:31
Definition: BlockAllocatorWithGC.h:31
T * allocate(void)
Definition: BlockAllocatorWithGC.h:69
void collectGarbage(void)
Definition: alloc_BlockAllocatorWithGC.cpp:115
int usedCount(void) const
Definition: BlockAllocatorWithGC.h:41
bool mark(T *b)
Definition: BlockAllocatorWithGC.h:72
free_t * next
Definition: BlockAllocatorWithGC.h:53
AbstractBlockAllocatorWithGC(t::size block_size, t::size chunk_size=1<< 20)
Definition: alloc_BlockAllocatorWithGC.cpp:37
t::size chunkSize(void) const
Definition: BlockAllocatorWithGC.h:38
Definition: BlockAllocatorWithGC.h:53
Definition: BlockAllocatorWithGC.h:66
void free(void *block)
Definition: alloc_BlockAllocatorWithGC.cpp:108