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