Path: blob/main/cddl/contrib/opensolaris/tools/ctf/cvt/stack.c
39586 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2001 by Sun Microsystems, Inc.23* All rights reserved.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* Routines for manipulating stacks30*/3132#include <stdio.h>33#include <assert.h>34#include <stdlib.h>3536#include "stack.h"37#include "memory.h"3839#define STACK_SEEDSIZE 54041struct stk {42int st_nument;43int st_top;44void **st_data;4546void (*st_free)(void *);47};4849stk_t *50stack_new(void (*freep)(void *))51{52stk_t *sp;5354sp = xmalloc(sizeof (stk_t));55sp->st_nument = STACK_SEEDSIZE;56sp->st_top = -1;57sp->st_data = xmalloc(sizeof (void *) * sp->st_nument);58sp->st_free = freep;5960return (sp);61}6263void64stack_free(stk_t *sp)65{66int i;6768if (sp->st_free) {69for (i = 0; i <= sp->st_top; i++)70sp->st_free(sp->st_data[i]);71}72free(sp->st_data);73free(sp);74}7576void *77stack_pop(stk_t *sp)78{79assert(sp->st_top >= 0);8081return (sp->st_data[sp->st_top--]);82}8384void *85stack_peek(stk_t *sp)86{87if (sp->st_top == -1)88return (NULL);8990return (sp->st_data[sp->st_top]);91}9293void94stack_push(stk_t *sp, void *data)95{96sp->st_top++;9798if (sp->st_top == sp->st_nument) {99sp->st_nument += STACK_SEEDSIZE;100sp->st_data = xrealloc(sp->st_data,101sizeof (void *) * sp->st_nument);102}103104sp->st_data[sp->st_top] = data;105}106107int108stack_level(stk_t *sp)109{110return (sp->st_top + 1);111}112113114