Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
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/data/Vector.h>
26 #include <elm/util/BitVector.h>
27 #include <elm/util/Flags.h>
28 
29 namespace elm {
30 
31 // abstract version
33 public:
34  AbstractBlockAllocatorWithGC(t::size block_size, t::size chunk_size = 1 << 20);
35  virtual ~AbstractBlockAllocatorWithGC(void);
36  void *allocate(void);
37 
38  inline bool needsCollect() const { return flags(NEED); }
39  inline bool isSync() { return flags(SYNC); }
40  inline void setSync() { flags.set(SYNC); }
41  inline void setAsync() { flags.clear(SYNC); }
42  void collectGarbage(void);
43 
44  inline t::size blockSize(void) const { return bsize; }
45  inline t::size chunkSize(void) const { return csize; }
46  inline int freeCount(void) const { return free_cnt; }
47  int totalCount(void) const;
48  inline int usedCount(void) const { return totalCount() - freeCount(); }
49 
50  // Allocator concept compatibility
51  void *allocate(t::size size);
52  void free(void *block);
53 
54 protected:
55  bool mark(void *ptr);
56  virtual void collect(void) = 0;
57  virtual void beginGC(void);
58  virtual void endGC(void);
59  virtual void destroy(void *p);
60 
61  typedef struct free_t { free_t *next; } free_t;
63  int free_cnt;
64 
65 private:
66  static const t::uint32
67  SYNC = 0,
68  NEED = 1;
69  Flags<> flags;
70  Vector<t::uint8 *> chunks;
71  t::uint8 *top;
72  t::size bsize, csize;
73  BitVector *coll;
74 };
75 
76 // template version
77 template <class T>
79 public:
80  inline BlockAllocatorWithGC(t::size chunk_size = 1 << 20): AbstractBlockAllocatorWithGC(sizeof(T), chunk_size) { }
81  inline T *allocate(void) { return static_cast<T *>(AbstractBlockAllocatorWithGC::allocate()); }
82  virtual void destroy(T *p) { }
83  void destroy(void *p) override { destroy(static_cast<T *>(p)); }
84 
85 protected:
86  inline bool mark(T *b) { return AbstractBlockAllocatorWithGC::mark(b); }
87 };
88 
89 } // elm
90 
91 #endif // ELM_ALLOC_BLOCK_ALLOCATOR_WITH_GC
elm::BlockAllocatorWithGC::destroy
virtual void destroy(T *p)
Definition: BlockAllocatorWithGC.h:82
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::AbstractBlockAllocatorWithGC::free_list
free_t * free_list
Definition: BlockAllocatorWithGC.h:62
elm::BlockAllocatorWithGC::BlockAllocatorWithGC
BlockAllocatorWithGC(t::size chunk_size=1<< 20)
Definition: BlockAllocatorWithGC.h:80
elm::AbstractBlockAllocatorWithGC::free_t::next
free_t * next
Definition: BlockAllocatorWithGC.h:61
elm::AbstractBlockAllocatorWithGC::freeCount
int freeCount(void) const
Definition: BlockAllocatorWithGC.h:46
elm::AbstractBlockAllocatorWithGC::allocate
void * allocate(void)
Definition: alloc_BlockAllocatorWithGC.cpp:69
elm::AbstractBlockAllocatorWithGC::usedCount
int usedCount(void) const
Definition: BlockAllocatorWithGC.h:48
elm::AbstractBlockAllocatorWithGC::free
void free(void *block)
Definition: alloc_BlockAllocatorWithGC.cpp:117
elm::Flags::clear
void clear(int i)
Definition: Flags.h:34
elm::AbstractBlockAllocatorWithGC::setAsync
void setAsync()
Definition: BlockAllocatorWithGC.h:41
elm::BlockAllocatorWithGC::mark
bool mark(T *b)
Definition: BlockAllocatorWithGC.h:86
elm::AbstractBlockAllocatorWithGC::setSync
void setSync()
Definition: BlockAllocatorWithGC.h:40
elm::AbstractBlockAllocatorWithGC::destroy
virtual void destroy(void *p)
Definition: alloc_BlockAllocatorWithGC.cpp:236
elm::BlockAllocatorWithGC::destroy
void destroy(void *p) override
Definition: BlockAllocatorWithGC.h:83
elm::AbstractBlockAllocatorWithGC::needsCollect
bool needsCollect() const
Definition: BlockAllocatorWithGC.h:38
elm::Flags
Definition: Flags.h:29
elm::AbstractBlockAllocatorWithGC::isSync
bool isSync()
Definition: BlockAllocatorWithGC.h:39
elm::AbstractBlockAllocatorWithGC::endGC
virtual void endGC(void)
Definition: alloc_BlockAllocatorWithGC.cpp:227
elm::AbstractBlockAllocatorWithGC::free_t
Definition: BlockAllocatorWithGC.h:61
elm::AbstractBlockAllocatorWithGC
Definition: BlockAllocatorWithGC.h:32
elm
Definition: adapter.h:26
elm::Vector< t::uint8 * >
elm::AbstractBlockAllocatorWithGC::free_t
struct elm::AbstractBlockAllocatorWithGC::free_t free_t
elm::t::size
uint64 size
Definition: arch.h:35
elm::BlockAllocatorWithGC::allocate
T * allocate(void)
Definition: BlockAllocatorWithGC.h:81
elm::t::uint8
unsigned char uint8
Definition: arch.h:27
elm::AbstractBlockAllocatorWithGC::~AbstractBlockAllocatorWithGC
virtual ~AbstractBlockAllocatorWithGC(void)
Definition: alloc_BlockAllocatorWithGC.cpp:48
elm::AbstractBlockAllocatorWithGC::collect
virtual void collect(void)=0
elm::AbstractBlockAllocatorWithGC::blockSize
t::size blockSize(void) const
Definition: BlockAllocatorWithGC.h:44
elm::AbstractBlockAllocatorWithGC::AbstractBlockAllocatorWithGC
AbstractBlockAllocatorWithGC(t::size block_size, t::size chunk_size=1<< 20)
Definition: alloc_BlockAllocatorWithGC.cpp:37
elm::AbstractBlockAllocatorWithGC::free_cnt
int free_cnt
Definition: BlockAllocatorWithGC.h:63
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::AbstractBlockAllocatorWithGC::chunkSize
t::size chunkSize(void) const
Definition: BlockAllocatorWithGC.h:45
elm::AbstractBlockAllocatorWithGC::totalCount
int totalCount(void) const
Definition: alloc_BlockAllocatorWithGC.cpp:244
elm::BitVector
Definition: BitVector.h:31
elm::Flags::set
void set(int i)
Definition: Flags.h:33
elm::BlockAllocatorWithGC
Definition: BlockAllocatorWithGC.h:78
elm::AbstractBlockAllocatorWithGC::mark
bool mark(void *ptr)
Definition: alloc_BlockAllocatorWithGC.cpp:191
elm::AbstractBlockAllocatorWithGC::collectGarbage
void collectGarbage(void)
Definition: alloc_BlockAllocatorWithGC.cpp:124
elm::AbstractBlockAllocatorWithGC::beginGC
virtual void beginGC(void)
Definition: alloc_BlockAllocatorWithGC.cpp:218