xref: /netbsd-src/external/mit/libcbor/dist/src/cbor/internal/stack.c (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /*
2  * Copyright (c) 2014-2019 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "stack.h"
9 
_cbor_stack_init()10 struct _cbor_stack _cbor_stack_init() {
11   return (struct _cbor_stack){.top = NULL, .size = 0};
12 }
13 
_cbor_stack_pop(struct _cbor_stack * stack)14 void _cbor_stack_pop(struct _cbor_stack *stack) {
15   struct _cbor_stack_record *top = stack->top;
16   stack->top = stack->top->lower;
17   _CBOR_FREE(top);
18   stack->size--;
19 }
20 
_cbor_stack_push(struct _cbor_stack * stack,cbor_item_t * item,size_t subitems)21 struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *stack,
22                                             cbor_item_t *item,
23                                             size_t subitems) {
24   struct _cbor_stack_record *new_top =
25       _CBOR_MALLOC(sizeof(struct _cbor_stack_record));
26   if (new_top == NULL) return NULL;
27 
28   *new_top = (struct _cbor_stack_record){stack->top, item, subitems};
29   stack->top = new_top;
30   stack->size++;
31   return new_top;
32 }
33