1*6b6d114aSBen Gras /* $NetBSD: prop_stack.c,v 1.2 2007/08/30 12:23:54 joerg Exp $ */
2*6b6d114aSBen Gras
3*6b6d114aSBen Gras /*-
4*6b6d114aSBen Gras * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
5*6b6d114aSBen Gras * All rights reserved.
6*6b6d114aSBen Gras *
7*6b6d114aSBen Gras * Redistribution and use in source and binary forms, with or without
8*6b6d114aSBen Gras * modification, are permitted provided that the following conditions
9*6b6d114aSBen Gras * are met:
10*6b6d114aSBen Gras *
11*6b6d114aSBen Gras * 1. Redistributions of source code must retain the above copyright
12*6b6d114aSBen Gras * notice, this list of conditions and the following disclaimer.
13*6b6d114aSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
14*6b6d114aSBen Gras * notice, this list of conditions and the following disclaimer in
15*6b6d114aSBen Gras * the documentation and/or other materials provided with the
16*6b6d114aSBen Gras * distribution.
17*6b6d114aSBen Gras *
18*6b6d114aSBen Gras * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*6b6d114aSBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*6b6d114aSBen Gras * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*6b6d114aSBen Gras * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*6b6d114aSBen Gras * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*6b6d114aSBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*6b6d114aSBen Gras * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*6b6d114aSBen Gras * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*6b6d114aSBen Gras * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27*6b6d114aSBen Gras * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28*6b6d114aSBen Gras * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*6b6d114aSBen Gras * SUCH DAMAGE.
30*6b6d114aSBen Gras */
31*6b6d114aSBen Gras
32*6b6d114aSBen Gras #include "prop_stack.h"
33*6b6d114aSBen Gras #include "prop_object_impl.h"
34*6b6d114aSBen Gras
35*6b6d114aSBen Gras void
_prop_stack_init(prop_stack_t stack)36*6b6d114aSBen Gras _prop_stack_init(prop_stack_t stack)
37*6b6d114aSBen Gras {
38*6b6d114aSBen Gras stack->used_intern_elems = 0;
39*6b6d114aSBen Gras SLIST_INIT(&stack->extern_elems);
40*6b6d114aSBen Gras }
41*6b6d114aSBen Gras
42*6b6d114aSBen Gras bool
_prop_stack_push(prop_stack_t stack,prop_object_t obj,void * data1,void * data2,void * data3)43*6b6d114aSBen Gras _prop_stack_push(prop_stack_t stack, prop_object_t obj, void *data1,
44*6b6d114aSBen Gras void *data2, void *data3)
45*6b6d114aSBen Gras {
46*6b6d114aSBen Gras struct _prop_stack_extern_elem *eelem;
47*6b6d114aSBen Gras struct _prop_stack_intern_elem *ielem;
48*6b6d114aSBen Gras
49*6b6d114aSBen Gras if (stack->used_intern_elems == PROP_STACK_INTERN_ELEMS) {
50*6b6d114aSBen Gras eelem = _PROP_MALLOC(sizeof(*eelem), M_TEMP);
51*6b6d114aSBen Gras
52*6b6d114aSBen Gras if (eelem == NULL)
53*6b6d114aSBen Gras return false;
54*6b6d114aSBen Gras
55*6b6d114aSBen Gras eelem->object = obj;
56*6b6d114aSBen Gras eelem->object_data[0] = data1;
57*6b6d114aSBen Gras eelem->object_data[1] = data2;
58*6b6d114aSBen Gras eelem->object_data[2] = data3;
59*6b6d114aSBen Gras
60*6b6d114aSBen Gras SLIST_INSERT_HEAD(&stack->extern_elems, eelem, stack_link);
61*6b6d114aSBen Gras
62*6b6d114aSBen Gras return true;
63*6b6d114aSBen Gras }
64*6b6d114aSBen Gras
65*6b6d114aSBen Gras _PROP_ASSERT(stack->used_intern_elems < PROP_STACK_INTERN_ELEMS);
66*6b6d114aSBen Gras _PROP_ASSERT(SLIST_EMPTY(&stack->extern_elems));
67*6b6d114aSBen Gras
68*6b6d114aSBen Gras ielem = &stack->intern_elems[stack->used_intern_elems];
69*6b6d114aSBen Gras ielem->object = obj;
70*6b6d114aSBen Gras ielem->object_data[0] = data1;
71*6b6d114aSBen Gras ielem->object_data[1] = data2;
72*6b6d114aSBen Gras ielem->object_data[2] = data3;
73*6b6d114aSBen Gras
74*6b6d114aSBen Gras ++stack->used_intern_elems;
75*6b6d114aSBen Gras
76*6b6d114aSBen Gras return true;
77*6b6d114aSBen Gras }
78*6b6d114aSBen Gras
79*6b6d114aSBen Gras bool
_prop_stack_pop(prop_stack_t stack,prop_object_t * obj,void ** data1,void ** data2,void ** data3)80*6b6d114aSBen Gras _prop_stack_pop(prop_stack_t stack, prop_object_t *obj, void **data1,
81*6b6d114aSBen Gras void **data2, void **data3)
82*6b6d114aSBen Gras {
83*6b6d114aSBen Gras struct _prop_stack_extern_elem *eelem;
84*6b6d114aSBen Gras struct _prop_stack_intern_elem *ielem;
85*6b6d114aSBen Gras
86*6b6d114aSBen Gras if (stack->used_intern_elems == 0)
87*6b6d114aSBen Gras return false;
88*6b6d114aSBen Gras
89*6b6d114aSBen Gras if ((eelem = SLIST_FIRST(&stack->extern_elems)) != NULL) {
90*6b6d114aSBen Gras _PROP_ASSERT(stack->used_intern_elems == PROP_STACK_INTERN_ELEMS);
91*6b6d114aSBen Gras
92*6b6d114aSBen Gras SLIST_REMOVE_HEAD(&stack->extern_elems, stack_link);
93*6b6d114aSBen Gras if (obj)
94*6b6d114aSBen Gras *obj = eelem->object;
95*6b6d114aSBen Gras if (data1)
96*6b6d114aSBen Gras *data1 = eelem->object_data[0];
97*6b6d114aSBen Gras if (data2)
98*6b6d114aSBen Gras *data2 = eelem->object_data[1];
99*6b6d114aSBen Gras if (data3)
100*6b6d114aSBen Gras *data3 = eelem->object_data[2];
101*6b6d114aSBen Gras _PROP_FREE(eelem, M_TEMP);
102*6b6d114aSBen Gras return true;
103*6b6d114aSBen Gras }
104*6b6d114aSBen Gras
105*6b6d114aSBen Gras --stack->used_intern_elems;
106*6b6d114aSBen Gras ielem = &stack->intern_elems[stack->used_intern_elems];
107*6b6d114aSBen Gras
108*6b6d114aSBen Gras if (obj)
109*6b6d114aSBen Gras *obj = ielem->object;
110*6b6d114aSBen Gras if (data1)
111*6b6d114aSBen Gras *data1 = ielem->object_data[0];
112*6b6d114aSBen Gras if (data2)
113*6b6d114aSBen Gras *data2 = ielem->object_data[1];
114*6b6d114aSBen Gras if (data3)
115*6b6d114aSBen Gras *data3 = ielem->object_data[2];
116*6b6d114aSBen Gras
117*6b6d114aSBen Gras return true;
118*6b6d114aSBen Gras }
119