1156230Smux/*- 2156230Smux * Copyright (c) 2006, Maxime Henrion <mux@FreeBSD.org> 3156230Smux * All rights reserved. 4156230Smux * 5156230Smux * Redistribution and use in source and binary forms, with or without 6156230Smux * modification, are permitted provided that the following conditions 7156230Smux * are met: 8156230Smux * 1. Redistributions of source code must retain the above copyright 9156230Smux * notice, this list of conditions and the following disclaimer. 10156230Smux * 2. Redistributions in binary form must reproduce the above copyright 11156230Smux * notice, this list of conditions and the following disclaimer in the 12156230Smux * documentation and/or other materials provided with the distribution. 13156230Smux * 14156230Smux * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15156230Smux * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16156230Smux * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17156230Smux * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18156230Smux * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19156230Smux * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20156230Smux * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21156230Smux * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22156230Smux * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23156230Smux * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24156230Smux * SUCH DAMAGE. 25156230Smux * 26156230Smux * $Id$ 27156230Smux */ 28156230Smux 29156230Smux#include <assert.h> 30156230Smux#include <stdlib.h> 31156230Smux 32156230Smux#include "attrstack.h" 33156230Smux#include "fattr.h" 34156230Smux#include "misc.h" 35156230Smux 36156230Smux#define ATTRSTACK_DEFSIZE 16 /* Initial size of the stack. */ 37156230Smux 38156230Smuxstruct attrstack { 39156230Smux struct fattr **stack; 40156230Smux size_t cur; 41156230Smux size_t size; 42156230Smux}; 43156230Smux 44156230Smuxstruct attrstack * 45156230Smuxattrstack_new(void) 46156230Smux{ 47156230Smux struct attrstack *as; 48156230Smux 49156230Smux as = xmalloc(sizeof(struct attrstack)); 50156230Smux as->stack = xmalloc(sizeof(struct fattr *) * ATTRSTACK_DEFSIZE); 51156230Smux as->size = ATTRSTACK_DEFSIZE; 52156230Smux as->cur = 0; 53156230Smux return (as); 54156230Smux} 55156230Smux 56156230Smuxstruct fattr * 57156230Smuxattrstack_pop(struct attrstack *as) 58156230Smux{ 59156230Smux 60156230Smux assert(as->cur > 0); 61156230Smux return (as->stack[--as->cur]); 62156230Smux} 63156230Smux 64156230Smuxvoid 65156230Smuxattrstack_push(struct attrstack *as, struct fattr *fa) 66156230Smux{ 67156230Smux 68156230Smux if (as->cur >= as->size) { 69156230Smux as->size *= 2; 70156230Smux as->stack = xrealloc(as->stack, 71156230Smux sizeof(struct fattr *) * as->size); 72156230Smux } 73156230Smux as->stack[as->cur++] = fa; 74156230Smux} 75156230Smux 76156230Smuxsize_t 77156230Smuxattrstack_size(struct attrstack *as) 78156230Smux{ 79156230Smux 80156230Smux return (as->cur); 81156230Smux} 82156230Smux 83156230Smuxvoid 84156230Smuxattrstack_free(struct attrstack *as) 85156230Smux{ 86156230Smux 87156230Smux assert(as->cur == 0); 88156230Smux free(as->stack); 89156230Smux free(as); 90156230Smux} 91