1*1673e404SJohn Birrell /*
2*1673e404SJohn Birrell  * CDDL HEADER START
3*1673e404SJohn Birrell  *
4*1673e404SJohn Birrell  * The contents of this file are subject to the terms of the
5*1673e404SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
6*1673e404SJohn Birrell  * (the "License").  You may not use this file except in compliance
7*1673e404SJohn Birrell  * with the License.
8*1673e404SJohn Birrell  *
9*1673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*1673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
11*1673e404SJohn Birrell  * See the License for the specific language governing permissions
12*1673e404SJohn Birrell  * and limitations under the License.
13*1673e404SJohn Birrell  *
14*1673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
15*1673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*1673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
17*1673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
18*1673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
19*1673e404SJohn Birrell  *
20*1673e404SJohn Birrell  * CDDL HEADER END
21*1673e404SJohn Birrell  */
22*1673e404SJohn Birrell /*
23*1673e404SJohn Birrell  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*1673e404SJohn Birrell  * All rights reserved.
25*1673e404SJohn Birrell  */
26*1673e404SJohn Birrell 
27*1673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*1673e404SJohn Birrell 
29*1673e404SJohn Birrell /*
30*1673e404SJohn Birrell  * Routines for manipulating stacks
31*1673e404SJohn Birrell  */
32*1673e404SJohn Birrell 
33*1673e404SJohn Birrell #include <stdio.h>
34*1673e404SJohn Birrell #include <assert.h>
35*1673e404SJohn Birrell #include <stdlib.h>
36*1673e404SJohn Birrell 
37*1673e404SJohn Birrell #include "stack.h"
38*1673e404SJohn Birrell #include "memory.h"
39*1673e404SJohn Birrell 
40*1673e404SJohn Birrell #define	STACK_SEEDSIZE	5
41*1673e404SJohn Birrell 
42*1673e404SJohn Birrell struct stk {
43*1673e404SJohn Birrell 	int st_nument;
44*1673e404SJohn Birrell 	int st_top;
45*1673e404SJohn Birrell 	void **st_data;
46*1673e404SJohn Birrell 
47*1673e404SJohn Birrell 	void (*st_free)(void *);
48*1673e404SJohn Birrell };
49*1673e404SJohn Birrell 
50*1673e404SJohn Birrell stk_t *
stack_new(void (* freep)(void *))51*1673e404SJohn Birrell stack_new(void (*freep)(void *))
52*1673e404SJohn Birrell {
53*1673e404SJohn Birrell 	stk_t *sp;
54*1673e404SJohn Birrell 
55*1673e404SJohn Birrell 	sp = xmalloc(sizeof (stk_t));
56*1673e404SJohn Birrell 	sp->st_nument = STACK_SEEDSIZE;
57*1673e404SJohn Birrell 	sp->st_top = -1;
58*1673e404SJohn Birrell 	sp->st_data = xmalloc(sizeof (void *) * sp->st_nument);
59*1673e404SJohn Birrell 	sp->st_free = freep;
60*1673e404SJohn Birrell 
61*1673e404SJohn Birrell 	return (sp);
62*1673e404SJohn Birrell }
63*1673e404SJohn Birrell 
64*1673e404SJohn Birrell void
stack_free(stk_t * sp)65*1673e404SJohn Birrell stack_free(stk_t *sp)
66*1673e404SJohn Birrell {
67*1673e404SJohn Birrell 	int i;
68*1673e404SJohn Birrell 
69*1673e404SJohn Birrell 	if (sp->st_free) {
70*1673e404SJohn Birrell 		for (i = 0; i <= sp->st_top; i++)
71*1673e404SJohn Birrell 			sp->st_free(sp->st_data[i]);
72*1673e404SJohn Birrell 	}
73*1673e404SJohn Birrell 	free(sp->st_data);
74*1673e404SJohn Birrell 	free(sp);
75*1673e404SJohn Birrell }
76*1673e404SJohn Birrell 
77*1673e404SJohn Birrell void *
stack_pop(stk_t * sp)78*1673e404SJohn Birrell stack_pop(stk_t *sp)
79*1673e404SJohn Birrell {
80*1673e404SJohn Birrell 	assert(sp->st_top >= 0);
81*1673e404SJohn Birrell 
82*1673e404SJohn Birrell 	return (sp->st_data[sp->st_top--]);
83*1673e404SJohn Birrell }
84*1673e404SJohn Birrell 
85*1673e404SJohn Birrell void *
stack_peek(stk_t * sp)86*1673e404SJohn Birrell stack_peek(stk_t *sp)
87*1673e404SJohn Birrell {
88*1673e404SJohn Birrell 	if (sp->st_top == -1)
89*1673e404SJohn Birrell 		return (NULL);
90*1673e404SJohn Birrell 
91*1673e404SJohn Birrell 	return (sp->st_data[sp->st_top]);
92*1673e404SJohn Birrell }
93*1673e404SJohn Birrell 
94*1673e404SJohn Birrell void
stack_push(stk_t * sp,void * data)95*1673e404SJohn Birrell stack_push(stk_t *sp, void *data)
96*1673e404SJohn Birrell {
97*1673e404SJohn Birrell 	sp->st_top++;
98*1673e404SJohn Birrell 
99*1673e404SJohn Birrell 	if (sp->st_top == sp->st_nument) {
100*1673e404SJohn Birrell 		sp->st_nument += STACK_SEEDSIZE;
101*1673e404SJohn Birrell 		sp->st_data = xrealloc(sp->st_data,
102*1673e404SJohn Birrell 		    sizeof (void *) * sp->st_nument);
103*1673e404SJohn Birrell 	}
104*1673e404SJohn Birrell 
105*1673e404SJohn Birrell 	sp->st_data[sp->st_top] = data;
106*1673e404SJohn Birrell }
107*1673e404SJohn Birrell 
108*1673e404SJohn Birrell int
stack_level(stk_t * sp)109*1673e404SJohn Birrell stack_level(stk_t *sp)
110*1673e404SJohn Birrell {
111*1673e404SJohn Birrell 	return (sp->st_top + 1);
112*1673e404SJohn Birrell }
113