xref: /freebsd-src/contrib/libcbor/src/cbor/internal/stack.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
110ff414cSEd Maste /*
210ff414cSEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste  *
410ff414cSEd Maste  * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste  * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste  */
710ff414cSEd Maste 
810ff414cSEd Maste #include "stack.h"
910ff414cSEd Maste 
_cbor_stack_init(void)10*5d3e7166SEd Maste struct _cbor_stack _cbor_stack_init(void) {
1110ff414cSEd Maste   return (struct _cbor_stack){.top = NULL, .size = 0};
1210ff414cSEd Maste }
1310ff414cSEd Maste 
_cbor_stack_pop(struct _cbor_stack * stack)1410ff414cSEd Maste void _cbor_stack_pop(struct _cbor_stack *stack) {
1510ff414cSEd Maste   struct _cbor_stack_record *top = stack->top;
1610ff414cSEd Maste   stack->top = stack->top->lower;
17*5d3e7166SEd Maste   _cbor_free(top);
1810ff414cSEd Maste   stack->size--;
1910ff414cSEd Maste }
2010ff414cSEd Maste 
_cbor_stack_push(struct _cbor_stack * stack,cbor_item_t * item,size_t subitems)2110ff414cSEd Maste struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *stack,
2210ff414cSEd Maste                                             cbor_item_t *item,
2310ff414cSEd Maste                                             size_t subitems) {
2410ff414cSEd Maste   if (stack->size == CBOR_MAX_STACK_SIZE) return NULL;
2510ff414cSEd Maste   struct _cbor_stack_record *new_top =
26*5d3e7166SEd Maste       _cbor_malloc(sizeof(struct _cbor_stack_record));
2710ff414cSEd Maste   if (new_top == NULL) return NULL;
2810ff414cSEd Maste 
2910ff414cSEd Maste   *new_top = (struct _cbor_stack_record){stack->top, item, subitems};
3010ff414cSEd Maste   stack->top = new_top;
3110ff414cSEd Maste   stack->size++;
3210ff414cSEd Maste   return new_top;
3310ff414cSEd Maste }
34