aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/ode/src/stack.h
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ode-0.9/ode/src/stack.h')
-rw-r--r--libraries/ode-0.9/ode/src/stack.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/libraries/ode-0.9/ode/src/stack.h b/libraries/ode-0.9/ode/src/stack.h
new file mode 100644
index 0000000..5afff41
--- /dev/null
+++ b/libraries/ode-0.9/ode/src/stack.h
@@ -0,0 +1,138 @@
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/* this comes from the `reuse' library. copy any changes back to the source.
24
25these stack allocation functions are a replacement for alloca(), except that
26they allocate memory from a separate pool.
27
28advantages over alloca():
29 - consecutive allocations are guaranteed to be contiguous with increasing
30 address.
31 - functions can allocate stack memory that is returned to the caller,
32 in other words pushing and popping stack frames is optional.
33
34disadvantages compared to alloca():
35 - less portable
36 - slightly slower, although still orders of magnitude faster than malloc().
37 - longjmp() and exceptions do not deallocate stack memory (but who cares?).
38
39just like alloca():
40 - using too much stack memory does not fail gracefully, it fails with a
41 segfault.
42
43*/
44
45
46#ifndef _ODE_STACK_H_
47#define _ODE_STACK_H_
48
49
50#ifdef WIN32
51#include "windows.h"
52#endif
53
54
55struct dStack {
56 char *base; // bottom of the stack
57 int size; // maximum size of the stack
58 char *pointer; // current top of the stack
59 char *frame; // linked list of stack frame ptrs
60# ifdef WIN32 // stuff for windows:
61 int pagesize; // - page size - this is ASSUMED to be a power of 2
62 int committed; // - bytes committed in allocated region
63#endif
64
65 // initialize the stack. `max_size' is the maximum size that the stack can
66 // reach. on unix and windows a `virtual' memory block of this size is
67 // mapped into the address space but does not actually consume physical
68 // memory until it is referenced - so it is safe to set this to a high value.
69
70 void init (int max_size);
71
72
73 // destroy the stack. this unmaps any virtual memory that was allocated.
74
75 void destroy();
76
77
78 // allocate `size' bytes from the stack and return a pointer to the allocated
79 // memory. `size' must be >= 0. the returned pointer will be aligned to the
80 // size of a long int.
81
82 char * alloc (int size)
83 {
84 char *ret = pointer;
85 pointer += ((size-1) | (sizeof(long int)-1) )+1;
86# ifdef WIN32
87 // for windows we need to commit pages as they are required
88 if ((pointer-base) > committed) {
89 committed = ((pointer-base-1) | (pagesize-1))+1; // round up to pgsize
90 VirtualAlloc (base,committed,MEM_COMMIT,PAGE_READWRITE);
91 }
92# endif
93 return ret;
94 }
95
96
97 // return the address that will be returned by the next call to alloc()
98
99 char *nextAlloc()
100 {
101 return pointer;
102 }
103
104
105 // push and pop the current size of the stack. pushFrame() saves the current
106 // frame pointer on the stack, and popFrame() retrieves it. a typical
107 // stack-using function will bracket alloc() calls with pushFrame() and
108 // popFrame(). both functions return the current stack pointer - this should
109 // be the same value for the two bracketing calls. calling popFrame() too
110 // many times will result in a segfault.
111
112 char * pushFrame()
113 {
114 char *newframe = pointer;
115 char **addr = (char**) alloc (sizeof(char*));
116 *addr = frame;
117 frame = newframe;
118 return newframe;
119
120 /* OLD CODE
121 *((char**)pointer) = frame;
122 frame = pointer;
123 char *ret = pointer;
124 pointer += sizeof(char*);
125 return ret;
126 */
127 }
128
129 char * popFrame()
130 {
131 pointer = frame;
132 frame = *((char**)pointer);
133 return pointer;
134 }
135};
136
137
138#endif