xref: /openbsd-src/lib/libcbor/src/cbor/internal/stack.c (revision 4dcc46c4d04180142eda526ce521dfb137776d05)
1da0d961cSdjm /*
2d3425be1Sdjm  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3da0d961cSdjm  *
4da0d961cSdjm  * libcbor is free software; you can redistribute it and/or modify
5da0d961cSdjm  * it under the terms of the MIT license. See LICENSE for details.
6da0d961cSdjm  */
7da0d961cSdjm 
8da0d961cSdjm #include "stack.h"
9da0d961cSdjm 
_cbor_stack_init(void)10*4dcc46c4Sdjm struct _cbor_stack _cbor_stack_init(void) {
11da0d961cSdjm   return (struct _cbor_stack){.top = NULL, .size = 0};
12da0d961cSdjm }
13da0d961cSdjm 
_cbor_stack_pop(struct _cbor_stack * stack)149e5c2ddcSdjm void _cbor_stack_pop(struct _cbor_stack *stack) {
15da0d961cSdjm   struct _cbor_stack_record *top = stack->top;
16da0d961cSdjm   stack->top = stack->top->lower;
17*4dcc46c4Sdjm   _cbor_free(top);
18da0d961cSdjm   stack->size--;
19da0d961cSdjm }
20da0d961cSdjm 
_cbor_stack_push(struct _cbor_stack * stack,cbor_item_t * item,size_t subitems)219e5c2ddcSdjm struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *stack,
229e5c2ddcSdjm                                             cbor_item_t *item,
239e5c2ddcSdjm                                             size_t subitems) {
24d3425be1Sdjm   if (stack->size == CBOR_MAX_STACK_SIZE) return NULL;
259e5c2ddcSdjm   struct _cbor_stack_record *new_top =
26*4dcc46c4Sdjm       _cbor_malloc(sizeof(struct _cbor_stack_record));
279e5c2ddcSdjm   if (new_top == NULL) return NULL;
28da0d961cSdjm 
29da0d961cSdjm   *new_top = (struct _cbor_stack_record){stack->top, item, subitems};
30da0d961cSdjm   stack->top = new_top;
31da0d961cSdjm   stack->size++;
32da0d961cSdjm   return new_top;
33da0d961cSdjm }
34