Last change
on this file since 30 was
30,
checked in by Sam Hocevar, 15 years ago
|
- Fix heap corruption and resulting at-exit crash, thanks to Kees Cook.
|
File size:
822 bytes
|
Rev | Line | |
---|
[2] | 1 | #ifndef __STACK_HPP_ |
---|
| 2 | #define __STACK_HPP_ |
---|
| 3 | #ifndef NO_LIBS |
---|
| 4 | #include "jmalloc.hpp" |
---|
| 5 | #else |
---|
| 6 | #include "fakelib.hpp" |
---|
| 7 | #endif |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | struct cons_cell; |
---|
| 11 | |
---|
| 12 | template<class T> class grow_stack // stack does not shrink |
---|
| 13 | { |
---|
| 14 | public : |
---|
| 15 | T **sdata; |
---|
| 16 | long son; |
---|
[30] | 17 | long smax; |
---|
[2] | 18 | |
---|
[30] | 19 | grow_stack(int max_size) { |
---|
| 20 | smax=max_size; |
---|
| 21 | son=0; |
---|
| 22 | sdata=(T **)jmalloc(sizeof(T *)*smax,"pointer stack"); |
---|
| 23 | } |
---|
[2] | 24 | void push(T *data) |
---|
| 25 | { |
---|
[30] | 26 | if (son>=smax) { lbreak("stack overflow (%ld)\n",smax); exit(1); } |
---|
[2] | 27 | sdata[son]=data; |
---|
| 28 | son++; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | T *pop(long total) |
---|
[30] | 32 | { if (total>son) { lbreak("stack underflow\n"); exit(1); } |
---|
[2] | 33 | son-=total; |
---|
| 34 | return sdata[son]; |
---|
| 35 | } |
---|
| 36 | void clean_up() |
---|
| 37 | { |
---|
| 38 | if (son!=0) fprintf(stderr,"Warning cleaning up stack and not empty\n"); |
---|
| 39 | jfree(sdata); |
---|
| 40 | sdata=NULL; son=0; |
---|
| 41 | } |
---|
| 42 | } ; |
---|
| 43 | |
---|
| 44 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.