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