Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
StackAllocator.h
1 /*
2  * $Id$
3  * StackAllocator class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2009, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef ELM_ALLOC_STACKALLOCATOR_H_
24 #define ELM_ALLOC_STACKALLOCATOR_H_
25 
26 #include <elm/types.h>
27 #include <elm/assert.h>
28 #include <elm/alloc/DefaultAllocator.h>
29 #include <elm/PreIterator.h>
30 
31 namespace elm {
32 
33 // StackAllocator class
35 public:
37  StackAllocator(t::size size = 4096);
38  virtual ~StackAllocator(void);
39  void *allocate(t::size size);
40  template <class T> inline void *allocate() { return allocate(sizeof(T)); }
41  inline void free(void *block) { }
42  void clear(void);
43 
44  // mark management
45  typedef char *mark_t;
46  mark_t mark(void);
47  void release(mark_t mark);
48 
49  // template access
50  template <class T>
51  inline T *allocate(int n = 1) { return static_cast<T *>(StackAllocator::allocate(n * sizeof(T))); }
52 
53 protected:
54  virtual void *chunkFilled(t::size size);
55 
56  typedef struct chunk_t {
57  struct chunk_t *next;
58  char buffer[0];
59  } chunk_t;
60 
61  class ChunkIter: public PreIterator<ChunkIter, chunk_t *> {
62  public:
63  inline ChunkIter(const StackAllocator& a): cur(a.cur) { }
64  inline ChunkIter(const ChunkIter& i): cur(i.cur) { }
65  inline ChunkIter& operator=(const ChunkIter& i) { cur = i.cur; return *this; }
66  inline bool ended(void) const { return !cur; }
67  inline chunk_t *item(void) const { return cur; }
68  inline void next(void) { cur = cur->next; }
69  private:
70  chunk_t *cur;
71  };
72 
73  inline t::size chunkSize(void) const { return _size; }
74  void newChunk(void);
75 
76 private:
77  chunk_t *cur;
78  char *max, *top;
79  t::size _size;
80 };
81 
82 } // elm
83 
84 #endif /* ELM_ALLOC_STACKALLOCATOR_H_ */
elm::StackAllocator::chunkFilled
virtual void * chunkFilled(t::size size)
Definition: alloc_StackAllocator.cpp:92
elm::StackAllocator::ChunkIter::item
chunk_t * item(void) const
Definition: StackAllocator.h:67
elm::StackAllocator::ChunkIter
Definition: StackAllocator.h:61
elm::StackAllocator::chunk_t::buffer
char buffer[0]
Definition: StackAllocator.h:58
elm::StackAllocator::allocate
T * allocate(int n=1)
Definition: StackAllocator.h:51
elm::StackAllocator::free
void free(void *block)
Definition: StackAllocator.h:41
elm::StackAllocator
Definition: StackAllocator.h:34
elm::StackAllocator::allocate
void * allocate()
Definition: StackAllocator.h:40
elm::StackAllocator::chunk_t
Definition: StackAllocator.h:56
elm::StackAllocator::ChunkIter::operator=
ChunkIter & operator=(const ChunkIter &i)
Definition: StackAllocator.h:65
elm
Definition: adapter.h:26
elm::StackAllocator::DEFAULT
static StackAllocator DEFAULT
Definition: StackAllocator.h:36
elm::t::size
uint64 size
Definition: arch.h:35
elm::StackAllocator::ChunkIter::ended
bool ended(void) const
Definition: StackAllocator.h:66
elm::StackAllocator::clear
void clear(void)
Definition: alloc_StackAllocator.cpp:109
elm::StackAllocator::mark_t
char * mark_t
Definition: StackAllocator.h:45
elm::StackAllocator::mark
mark_t mark(void)
Definition: alloc_StackAllocator.cpp:142
elm::StackAllocator::chunk_t
struct elm::StackAllocator::chunk_t chunk_t
elm::StackAllocator::newChunk
void newChunk(void)
Definition: alloc_StackAllocator.cpp:121
elm::StackAllocator::chunkSize
t::size chunkSize(void) const
Definition: StackAllocator.h:73
elm::StackAllocator::ChunkIter::ChunkIter
ChunkIter(const ChunkIter &i)
Definition: StackAllocator.h:64
elm::PreIterator
Definition: iter.h:28
elm::StackAllocator::~StackAllocator
virtual ~StackAllocator(void)
Definition: alloc_StackAllocator.cpp:63
elm::StackAllocator::release
void release(mark_t mark)
Definition: alloc_StackAllocator.cpp:151
elm::StackAllocator::chunk_t::next
struct chunk_t * next
Definition: StackAllocator.h:57
elm::StackAllocator::ChunkIter::next
void next(void)
Definition: StackAllocator.h:68
elm::StackAllocator::ChunkIter::ChunkIter
ChunkIter(const StackAllocator &a)
Definition: StackAllocator.h:63
elm::StackAllocator::StackAllocator
StackAllocator(t::size size=4096)
Definition: alloc_StackAllocator.cpp:55