1178481Sjb/* 2178481Sjb * CDDL HEADER START 3178481Sjb * 4178481Sjb * The contents of this file are subject to the terms of the 5178481Sjb * Common Development and Distribution License, Version 1.0 only 6178481Sjb * (the "License"). You may not use this file except in compliance 7178481Sjb * with the License. 8178481Sjb * 9178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10178481Sjb * or http://www.opensolaris.org/os/licensing. 11178481Sjb * See the License for the specific language governing permissions 12178481Sjb * and limitations under the License. 13178481Sjb * 14178481Sjb * When distributing Covered Code, include this CDDL HEADER in each 15178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16178481Sjb * If applicable, add the following below this CDDL HEADER, with the 17178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying 18178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 19178481Sjb * 20178481Sjb * CDDL HEADER END 21178481Sjb */ 22178481Sjb/* 23178481Sjb * Copyright (c) 2001 by Sun Microsystems, Inc. 24178481Sjb * All rights reserved. 25178481Sjb */ 26178481Sjb 27178481Sjb#pragma ident "%Z%%M% %I% %E% SMI" 28178481Sjb 29178481Sjb/* 30178481Sjb * Routines for manipulating stacks 31178481Sjb */ 32178481Sjb 33178481Sjb#include <stdio.h> 34178481Sjb#include <assert.h> 35178481Sjb#include <stdlib.h> 36178481Sjb 37178481Sjb#include "stack.h" 38178481Sjb#include "memory.h" 39178481Sjb 40178481Sjb#define STACK_SEEDSIZE 5 41178481Sjb 42178481Sjbstruct stk { 43178481Sjb int st_nument; 44178481Sjb int st_top; 45178481Sjb void **st_data; 46178481Sjb 47178481Sjb void (*st_free)(void *); 48178481Sjb}; 49178481Sjb 50178481Sjbstk_t * 51178481Sjbstack_new(void (*freep)(void *)) 52178481Sjb{ 53178481Sjb stk_t *sp; 54178481Sjb 55178481Sjb sp = xmalloc(sizeof (stk_t)); 56178481Sjb sp->st_nument = STACK_SEEDSIZE; 57178481Sjb sp->st_top = -1; 58178481Sjb sp->st_data = xmalloc(sizeof (void *) * sp->st_nument); 59178481Sjb sp->st_free = freep; 60178481Sjb 61178481Sjb return (sp); 62178481Sjb} 63178481Sjb 64178481Sjbvoid 65178481Sjbstack_free(stk_t *sp) 66178481Sjb{ 67178481Sjb int i; 68178481Sjb 69178481Sjb if (sp->st_free) { 70178481Sjb for (i = 0; i <= sp->st_top; i++) 71178481Sjb sp->st_free(sp->st_data[i]); 72178481Sjb } 73178481Sjb free(sp->st_data); 74178481Sjb free(sp); 75178481Sjb} 76178481Sjb 77178481Sjbvoid * 78178481Sjbstack_pop(stk_t *sp) 79178481Sjb{ 80178481Sjb assert(sp->st_top >= 0); 81178481Sjb 82178481Sjb return (sp->st_data[sp->st_top--]); 83178481Sjb} 84178481Sjb 85178481Sjbvoid * 86178481Sjbstack_peek(stk_t *sp) 87178481Sjb{ 88178481Sjb if (sp->st_top == -1) 89178481Sjb return (NULL); 90178481Sjb 91178481Sjb return (sp->st_data[sp->st_top]); 92178481Sjb} 93178481Sjb 94178481Sjbvoid 95178481Sjbstack_push(stk_t *sp, void *data) 96178481Sjb{ 97178481Sjb sp->st_top++; 98178481Sjb 99178481Sjb if (sp->st_top == sp->st_nument) { 100178481Sjb sp->st_nument += STACK_SEEDSIZE; 101178481Sjb sp->st_data = xrealloc(sp->st_data, 102178481Sjb sizeof (void *) * sp->st_nument); 103178481Sjb } 104178481Sjb 105178481Sjb sp->st_data[sp->st_top] = data; 106178481Sjb} 107178481Sjb 108178481Sjbint 109178481Sjbstack_level(stk_t *sp) 110178481Sjb{ 111178481Sjb return (sp->st_top + 1); 112178481Sjb} 113