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
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) throw(BadAlloc);
40  inline void free(void *block) { }
41  void clear(void);
42 
43  // mark management
44  typedef char *mark_t;
45  mark_t mark(void);
46  void release(mark_t mark);
47 
48  // template access
49  template <class T>
50  inline T *allocate(int n = 1) throw(BadAlloc) { return static_cast<T *>(StackAllocator::allocate(n * sizeof(T))); }
51 
52 protected:
53  virtual void *chunkFilled(t::size size) throw(BadAlloc);
54 
55  typedef struct chunk_t {
56  struct chunk_t *next;
57  char buffer[0];
58  } chunk_t;
59 
60  class ChunkIter: public PreIterator<ChunkIter, chunk_t *> {
61  public:
62  inline ChunkIter(const StackAllocator& a): cur(a.cur) { }
63  inline ChunkIter(const ChunkIter& i): cur(i.cur) { }
64  inline ChunkIter& operator=(const ChunkIter& i) { cur = i.cur; return *this; }
65  inline bool ended(void) const { return !cur; }
66  inline chunk_t *item(void) const { return cur; }
67  inline void next(void) { cur = cur->next; }
68  private:
69  chunk_t *cur;
70  };
71 
72  inline t::size chunkSize(void) const { return _size; }
73  void newChunk(void) throw(BadAlloc);
74 
75 private:
76  chunk_t *cur;
77  char *max, *top;
78  t::size _size;
79 };
80 
81 } // elm
82 
83 #endif /* ELM_ALLOC_STACKALLOCATOR_H_ */
Definition: StackAllocator.h:55
struct chunk_t * next
Definition: StackAllocator.h:56
ChunkIter(const ChunkIter &i)
Definition: StackAllocator.h:63
Definition: PreIterator.h:29
char * mark_t
Definition: StackAllocator.h:44
StackAllocator(t::size size=4096)
Definition: alloc_StackAllocator.cpp:55
virtual ~StackAllocator(void)
Definition: alloc_StackAllocator.cpp:63
Definition: StackAllocator.h:34
uint32 size
Definition: int.h:41
struct elm::StackAllocator::chunk_t chunk_t
virtual void * chunkFilled(t::size size)
Definition: alloc_StackAllocator.cpp:92
Definition: DefaultAllocator.h:31
void free(void *block)
Definition: StackAllocator.h:40
mark_t mark(void)
Definition: alloc_StackAllocator.cpp:142
void * allocate(t::size size)
Definition: alloc_StackAllocator.cpp:75
t::size chunkSize(void) const
Definition: StackAllocator.h:72
ChunkIter & operator=(const ChunkIter &i)
Definition: StackAllocator.h:64
bool ended(void) const
Definition: StackAllocator.h:65
ChunkIter(const StackAllocator &a)
Definition: StackAllocator.h:62
chunk_t * item(void) const
Definition: StackAllocator.h:66
static StackAllocator DEFAULT
Definition: StackAllocator.h:36
void next(void)
Definition: StackAllocator.h:67
void release(mark_t mark)
Definition: alloc_StackAllocator.cpp:151
T * allocate(int n=1)
Definition: StackAllocator.h:50
Definition: StackAllocator.h:60
char buffer[0]
Definition: StackAllocator.h:57
void clear(void)
Definition: alloc_StackAllocator.cpp:109
void newChunk(void)
Definition: alloc_StackAllocator.cpp:121