11673e404SJohn Birrell /* 21673e404SJohn Birrell * CDDL HEADER START 31673e404SJohn Birrell * 41673e404SJohn Birrell * The contents of this file are subject to the terms of the 51673e404SJohn Birrell * Common Development and Distribution License, Version 1.0 only 61673e404SJohn Birrell * (the "License"). You may not use this file except in compliance 71673e404SJohn Birrell * with the License. 81673e404SJohn Birrell * 91673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 101673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing. 111673e404SJohn Birrell * See the License for the specific language governing permissions 121673e404SJohn Birrell * and limitations under the License. 131673e404SJohn Birrell * 141673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each 151673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 161673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the 171673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying 181673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner] 191673e404SJohn Birrell * 201673e404SJohn Birrell * CDDL HEADER END 211673e404SJohn Birrell */ 221673e404SJohn Birrell /* 231673e404SJohn Birrell * Copyright (c) 2001 by Sun Microsystems, Inc. 241673e404SJohn Birrell * All rights reserved. 251673e404SJohn Birrell */ 261673e404SJohn Birrell 271673e404SJohn Birrell #pragma ident "%Z%%M% %I% %E% SMI" 281673e404SJohn Birrell 291673e404SJohn Birrell /* 301673e404SJohn Birrell * Routines for manipulating stacks 311673e404SJohn Birrell */ 321673e404SJohn Birrell 331673e404SJohn Birrell #include <stdio.h> 341673e404SJohn Birrell #include <assert.h> 351673e404SJohn Birrell #include <stdlib.h> 361673e404SJohn Birrell 371673e404SJohn Birrell #include "stack.h" 381673e404SJohn Birrell #include "memory.h" 391673e404SJohn Birrell 401673e404SJohn Birrell #define STACK_SEEDSIZE 5 411673e404SJohn Birrell 421673e404SJohn Birrell struct stk { 431673e404SJohn Birrell int st_nument; 441673e404SJohn Birrell int st_top; 451673e404SJohn Birrell void **st_data; 461673e404SJohn Birrell 471673e404SJohn Birrell void (*st_free)(void *); 481673e404SJohn Birrell }; 491673e404SJohn Birrell 501673e404SJohn Birrell stk_t * stack_new(void (* freep)(void *))511673e404SJohn Birrellstack_new(void (*freep)(void *)) 521673e404SJohn Birrell { 531673e404SJohn Birrell stk_t *sp; 541673e404SJohn Birrell 551673e404SJohn Birrell sp = xmalloc(sizeof (stk_t)); 561673e404SJohn Birrell sp->st_nument = STACK_SEEDSIZE; 571673e404SJohn Birrell sp->st_top = -1; 581673e404SJohn Birrell sp->st_data = xmalloc(sizeof (void *) * sp->st_nument); 591673e404SJohn Birrell sp->st_free = freep; 601673e404SJohn Birrell 611673e404SJohn Birrell return (sp); 621673e404SJohn Birrell } 631673e404SJohn Birrell 641673e404SJohn Birrell void stack_free(stk_t * sp)651673e404SJohn Birrellstack_free(stk_t *sp) 661673e404SJohn Birrell { 671673e404SJohn Birrell int i; 681673e404SJohn Birrell 691673e404SJohn Birrell if (sp->st_free) { 701673e404SJohn Birrell for (i = 0; i <= sp->st_top; i++) 711673e404SJohn Birrell sp->st_free(sp->st_data[i]); 721673e404SJohn Birrell } 731673e404SJohn Birrell free(sp->st_data); 741673e404SJohn Birrell free(sp); 751673e404SJohn Birrell } 761673e404SJohn Birrell 771673e404SJohn Birrell void * stack_pop(stk_t * sp)781673e404SJohn Birrellstack_pop(stk_t *sp) 791673e404SJohn Birrell { 801673e404SJohn Birrell assert(sp->st_top >= 0); 811673e404SJohn Birrell 821673e404SJohn Birrell return (sp->st_data[sp->st_top--]); 831673e404SJohn Birrell } 841673e404SJohn Birrell 851673e404SJohn Birrell void * stack_peek(stk_t * sp)861673e404SJohn Birrellstack_peek(stk_t *sp) 871673e404SJohn Birrell { 881673e404SJohn Birrell if (sp->st_top == -1) 891673e404SJohn Birrell return (NULL); 901673e404SJohn Birrell 911673e404SJohn Birrell return (sp->st_data[sp->st_top]); 921673e404SJohn Birrell } 931673e404SJohn Birrell 941673e404SJohn Birrell void stack_push(stk_t * sp,void * data)951673e404SJohn Birrellstack_push(stk_t *sp, void *data) 961673e404SJohn Birrell { 971673e404SJohn Birrell sp->st_top++; 981673e404SJohn Birrell 991673e404SJohn Birrell if (sp->st_top == sp->st_nument) { 1001673e404SJohn Birrell sp->st_nument += STACK_SEEDSIZE; 1011673e404SJohn Birrell sp->st_data = xrealloc(sp->st_data, 1021673e404SJohn Birrell sizeof (void *) * sp->st_nument); 1031673e404SJohn Birrell } 1041673e404SJohn Birrell 1051673e404SJohn Birrell sp->st_data[sp->st_top] = data; 1061673e404SJohn Birrell } 1071673e404SJohn Birrell 1081673e404SJohn Birrell int stack_level(stk_t * sp)1091673e404SJohn Birrellstack_level(stk_t *sp) 1101673e404SJohn Birrell { 1111673e404SJohn Birrell return (sp->st_top + 1); 1121673e404SJohn Birrell } 113