Advanced Strategic Command
stack.cpp
Go to the documentation of this file.
1 
5 /*
6  This file is part of Advanced Strategic Command; http://www.asc-hq.de
7  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to the
21  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  Boston, MA 02111-1307 USA
23 */
24 
25 #include <cstring>
26 
27 #include "global.h"
28 #include "stack.h"
29 
30 
31 #define stacksize 10000
32 
33 Uint8* stackpointer = NULL;
34 int stackofs = 0;
35 
36 const int magic = 0x12345678;
37 
38 void push_data(Uint8 * daten,
39  int size)
40 {
41  memcpy(stackpointer+stackofs, daten, size);
42  stackofs+=size;
43 }
44 
45 
46 
47 void pop_data(Uint8 * daten,
48  int size)
49 {
50  if (stackofs < size)
51  throw fatalstackerror();
52 
53  stackofs-=size;
54  memcpy(daten, stackpointer + stackofs, size);
55 }
56 
57 
58 void pushdata(Uint8 * daten, int size)
59 {
60  if ( !stackpointer ) {
61  stackpointer = new Uint8 [ stacksize ];
62  stackofs = 0;
63  }
64  push_data ( daten, size );
65  push_data ( (Uint8*) &magic, sizeof ( magic ));
66 }
67 
68 
69 void popdata(Uint8 * daten, int size)
70 {
71  int m;
72  pop_data ( (Uint8*) &m, sizeof ( m ));
73  if ( m != magic )
74  throw fatalstackerror();
75  pop_data ( daten, size );
76 }
77 
78 
79 int stackfree(void)
80 {
81  return ( stacksize - stackofs);
82 }
83 
void pushdata(Uint8 *daten, int size)
Definition: stack.cpp:58
Global platform dependant definitions. This file just branches to the platform specific files in thei...
Uint8 * stackpointer
Definition: stack.cpp:33
const int magic
Definition: stack.cpp:36
int stackfree(void)
Definition: stack.cpp:79
void pop_data(Uint8 *daten, int size)
Definition: stack.cpp:47
int stackofs
Definition: stack.cpp:34
void push_data(Uint8 *daten, int size)
Definition: stack.cpp:38
#define stacksize
Definition: stack.cpp:31
void popdata(Uint8 *daten, int size)
Definition: stack.cpp:69
Interface for a small general-purpose stack (not type safe)