Open Dynamics Engine
obstack.h
1 /*************************************************************************
2  * *
3  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4  * All rights reserved. Email: russ@q12.org Web: www.q12.org *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of EITHER: *
8  * (1) The GNU Lesser General Public License as published by the Free *
9  * Software Foundation; either version 2.1 of the License, or (at *
10  * your option) any later version. The text of the GNU Lesser *
11  * General Public License is included with this library in the *
12  * file LICENSE.TXT. *
13  * (2) The BSD-style license that is included with this library in *
14  * the file LICENSE-BSD.TXT. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19  * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20  * *
21  *************************************************************************/
22 
23 #ifndef _ODE_OBSTACK_H_
24 #define _ODE_OBSTACK_H_
25 
26 #include "objects.h"
27 
28 // each obstack Arena pointer points to a block of this many bytes
29 #define dOBSTACK_ARENA_SIZE 16384
30 
31 
32 struct dObStack : public dBase {
33  dObStack();
34  ~dObStack();
35 
36  void *alloc (size_t num_bytes);
37  // allocate a block in the last arena, allocating a new arena if necessary.
38  // it is a runtime error if num_bytes is larger than the arena size.
39 
40  void freeAll();
41  // free all blocks in all arenas. this does not deallocate the arenas
42  // themselves, so future alloc()s will reuse them.
43 
44  void *rewind();
45  // rewind the obstack iterator, and return the address of the first
46  // allocated block. return 0 if there are no allocated blocks.
47 
48  void *next (size_t num_bytes);
49  // return the address of the next allocated block. 'num_bytes' is the size
50  // of the previous block. this returns null if there are no more arenas.
51  // the sequence of 'num_bytes' parameters passed to next() during a
52  // traversal of the list must exactly match the parameters passed to alloc().
53 
54 private:
55  struct Arena {
56  Arena *m_next; // next arena in linked list
57  size_t m_used; // total number of bytes used in this arena, counting
58  }; // this header
59 
60 private:
61  void *switch_to_arena(Arena *next_arena);
62 
63 private:
64  Arena *m_first; // head of the arena linked list. 0 if no arenas yet
65  Arena *m_last; // arena where blocks are currently being allocated
66 
67  // used for iterator
68  Arena *m_current_arena;
69  size_t m_current_ofs;
70 };
71 
72 
73 #endif
Definition: ode/src/objects.h:57
Definition: obstack.h:55
Definition: obstack.h:32