File: stack.h

package info (click to toggle)
nickle 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,612 kB
  • ctags: 3,746
  • sloc: ansic: 26,986; yacc: 1,873; sh: 954; lex: 884; makefile: 225
file content (104 lines) | stat: -rw-r--r-- 3,115 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* $Header$ */

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#ifndef _STACK_H_
#define _STACK_H_
#define STACK_ENTS_PER_CHUNK	126

typedef void		*StackElement;
typedef StackElement	*StackPointer;

typedef struct _StackChunk {
    DataType		*type;
    struct _StackChunk	*previous;
    StackElement	elements[STACK_ENTS_PER_CHUNK];
} StackChunk;

typedef struct _Stack {
    DataType		*type;
    StackPointer	stackPointer;
    StackChunk		*current;
    StackChunk		*save;
    StackElement	temp;
} StackObject;

extern StackObject  *StackCreate (void);
extern StackObject  *StackCopy (StackObject *stack);
extern StackElement StackPush (StackObject *stack, StackElement object);
extern StackElement StackPop (StackObject *stack);
extern void	    StackDrop (StackObject *stack, int i);
extern void	    StackReset (StackObject *stack, StackPointer stackPointer);
extern StackElement StackReturn (StackObject *stack, StackPointer stackPointer, StackElement object);
extern StackElement StackElt (StackObject *stack, int i);

#define CHUNK_MAX(c)	((c)->elements + STACK_ENTS_PER_CHUNK)
#define CHUNK_MIN(c)	((c)->elements)
#define STACK_MAX(s)	(CHUNK_MAX((s)->current))
#define STACK_MIN(s)	(CHUNK_MIN((s)->current))
#define STACK_TOP(s)	((s)->stackPointer)

#define STACK_POP(s)	    ((STACK_TOP(s) == STACK_MAX(s)) ? \
			     StackPop (s) : *STACK_TOP(s)++)

#define STACK_DROP(s,i)	    ((STACK_TOP(s) + (i) <= STACK_MAX(s)) ? \
			     ((STACK_TOP(s) += (i)), 0) : (StackDrop(s, i), 0))

#define STACK_RESET(s,sp)   (STACK_TOP(s) == (sp) ? 0 : \
			     ((STACK_TOP(s) <= (sp) && (sp) <= STACK_MAX(s)) ? \
			      ((STACK_TOP(s) = (sp)), 0) : \
			      (StackReset ((s), (sp)), 0)))

#define STACK_ELT(s,i)	((STACK_TOP(s) + (i) < STACK_MAX(s)) ? \
			 STACK_TOP(s)[i] : StackElt(s,i))

#if 0
#define STACK_VALID(s)	((!(s)->stackPointer && !(s)->current) || \
			 (STACK_MIN(s) <= STACK_TOP(s) && \
			  STACK_TOP(s) <= STACK_MAX(s)))

void
panic (char *, ...);

#define STACK_ASSERT(s)	if (!STACK_VALID(s)) panic ("invalid stack\n");
#define STACK_CHUNK_ASSERT(c)	assert((c)->type == &stackChunkType)
#else
#define STACK_ASSERT(s)
#define STACK_CHUNK_ASSERT(c)
#endif

#if 0
/*
 * Can't work -- o gets evaluated after the stack overflow check, 
 * if o also uses the stack, this will break
 */
#define STACK_PUSH(s,o)	    ((STACK_TOP(s) == STACK_MIN(s)) ? \
			     StackPush ((s), (o)) : (*--STACK_TOP(s) = (o)))
#endif

static inline StackElement
StackPushInline(StackObject *s, StackElement o)
{
    STACK_ASSERT (s);
    if (STACK_TOP(s) == STACK_MIN(s))
	return StackPush (s, o);
    return *--STACK_TOP(s) = o;
}
#define STACK_PUSH(s,o) StackPushInline(s,o)
static inline StackElement
StackReturnInline(StackObject *s, StackPointer sp, StackElement o)
{
    STACK_ASSERT(s);
    STACK_RESET(s, sp);
    STACK_ASSERT(s);
    if (STACK_TOP(s) == STACK_MIN(s))
	return StackPush (s, o);
    return *--STACK_TOP(s) = o;
}
#define STACK_RETURN(s,sp,o) StackReturnInline(s,sp,o)

#endif /* _STACK_H_ */