source: abuse/trunk/src/include/stack.hpp @ 30

Last change on this file since 30 was 30, checked in by Sam Hocevar, 14 years ago
  • Fix heap corruption and resulting at-exit crash, thanks to Kees Cook.
File size: 822 bytes
Line 
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>
10struct cons_cell;
11
12template<class T> class grow_stack        // stack does not shrink
13{
14  public :
15  T **sdata;
16  long son;
17  long smax;
18
19  grow_stack(int max_size) {
20    smax=max_size;
21    son=0;
22    sdata=(T **)jmalloc(sizeof(T *)*smax,"pointer stack");
23  }
24  void push(T *data)
25  {
26    if (son>=smax) { lbreak("stack overflow (%ld)\n",smax); exit(1); }
27    sdata[son]=data;
28    son++;
29  }
30   
31  T *pop(long total)
32  { if (total>son) { lbreak("stack underflow\n"); exit(1); }
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.