source: abuse/tags/pd/macabuse/inc/stack.hpp

Last change on this file was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
File size: 719 bytes
Line 
1#ifndef __STACK_HPP_
2#define __STACK_HPP_
3
4#ifndef NO_LIBS
5#include "jmalloc.hpp"
6
7#include "dprint.hpp"
8#else
9#include "fakelib.hpp"
10#endif
11
12#include <stdio.h>
13struct cons_cell;
14
15template<class T> class grow_stack        // stack does not shrink
16{
17  public :
18  T **sdata;
19  long son;
20
21  grow_stack(int max_size) { sdata=(T **)jmalloc(max_size,"pointer stack");  son=0; }
22  void push(T *data)
23  {
24    sdata[son]=data;
25    son++;
26  }
27   
28  T *pop(long total)
29  { if (total>son) { lbreak("stack underflow\n"); exit(0); }
30    son-=total;
31    return sdata[son];
32  }
33  void clean_up()
34  {
35    if (son!=0) dprintf("Warning cleaning up stack and not empty\n");
36    jfree(sdata);
37    sdata=NULL;  son=0;
38  }
39} ;
40
41#endif
Note: See TracBrowser for help on using the repository browser.