attrstack.c revision 225736
1107665Simp/*-
2113785Simp * Copyright (c) 2006, Maxime Henrion <mux@FreeBSD.org>
3107665Simp * All rights reserved.
4107665Simp *
5107665Simp * Redistribution and use in source and binary forms, with or without
6107665Simp * modification, are permitted provided that the following conditions
7107665Simp * are met:
8107665Simp * 1. Redistributions of source code must retain the above copyright
9107665Simp *    notice, this list of conditions and the following disclaimer.
10107665Simp * 2. Redistributions in binary form must reproduce the above copyright
11107665Simp *    notice, this list of conditions and the following disclaimer in the
12107665Simp *    documentation and/or other materials provided with the distribution.
13107665Simp *
14107665Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15107665Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16107665Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17107665Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18107665Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19107665Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20107665Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21107665Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22107665Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23107665Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24107665Simp * SUCH DAMAGE.
25107665Simp *
26107665Simp * $Id$
27107665Simp */
28107665Simp
29107665Simp#include <assert.h>
30107665Simp#include <stdlib.h>
31107665Simp
32107665Simp#include "attrstack.h"
33131397Simp#include "fattr.h"
34107665Simp#include "misc.h"
35107665Simp
36107665Simp#define	ATTRSTACK_DEFSIZE	16	/* Initial size of the stack. */
37107665Simp
38107665Simpstruct attrstack {
39107665Simp	struct fattr **stack;
40131397Simp	size_t cur;
41131397Simp	size_t size;
42131397Simp};
43107665Simp
44131397Simpstruct attrstack *
45107665Simpattrstack_new(void)
46108014Simp{
47107665Simp	struct attrstack *as;
48107665Simp
49107665Simp	as = xmalloc(sizeof(struct attrstack));
50107665Simp	as->stack = xmalloc(sizeof(struct fattr *) * ATTRSTACK_DEFSIZE);
51108014Simp	as->size = ATTRSTACK_DEFSIZE;
52146306Simp	as->cur = 0;
53107665Simp	return (as);
54107665Simp}
55107665Simp
56107665Simpstruct fattr *
57107665Simpattrstack_pop(struct attrstack *as)
58108783Simp{
59107665Simp
60107665Simp	assert(as->cur > 0);
61131397Simp	return (as->stack[--as->cur]);
62107665Simp}
63107665Simp
64114086Simpvoid
65114086Simpattrstack_push(struct attrstack *as, struct fattr *fa)
66107665Simp{
67131397Simp
68107665Simp	if (as->cur >= as->size) {
69113787Simp		as->size *= 2;
70107665Simp		as->stack = xrealloc(as->stack,
71107665Simp		    sizeof(struct fattr *) * as->size);
72107665Simp	}
73107665Simp	as->stack[as->cur++] = fa;
74107665Simp}
75107665Simp
76121487Simpsize_t
77108783Simpattrstack_size(struct attrstack *as)
78108783Simp{
79108783Simp
80108783Simp	return (as->cur);
81113790Simp}
82107665Simp
83114000Simpvoid
84107665Simpattrstack_free(struct attrstack *as)
85107665Simp{
86107665Simp
87107665Simp	assert(as->cur == 0);
88107665Simp	free(as->stack);
89108783Simp	free(as);
90108783Simp}
91108783Simp