xref: /5.5.2/couchdb/src/ejson/yajl/yajl_bytestack.h (revision 3925e856)
1/*
2 * Copyright 2010, Lloyd Hilaiel.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *  1. Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 *
11 *  2. Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in
13 *     the documentation and/or other materials provided with the
14 *     distribution.
15 *
16 *  3. Neither the name of Lloyd Hilaiel nor the names of its
17 *     contributors may be used to endorse or promote products derived
18 *     from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * A header only implementation of a simple stack of bytes, used in YAJL
35 * to maintain parse state.
36 */
37
38#ifndef __YAJL_BYTESTACK_H__
39#define __YAJL_BYTESTACK_H__
40
41#include "yajl_common.h"
42
43#define YAJL_BS_INC 128
44
45typedef struct yajl_bytestack_t
46{
47    unsigned char * stack;
48    unsigned int size;
49    unsigned int used;
50    yajl_alloc_funcs * yaf;
51} yajl_bytestack;
52
53/* initialize a bytestack */
54#define yajl_bs_init(obs, _yaf) {               \
55        (obs).stack = NULL;                     \
56        (obs).size = 0;                         \
57        (obs).used = 0;                         \
58        (obs).yaf = (_yaf);                     \
59    }                                           \
60
61
62/* initialize a bytestack */
63#define yajl_bs_free(obs)                 \
64    if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
65
66#define yajl_bs_current(obs)               \
67    (assert((obs).used > 0), (obs).stack[(obs).used - 1])
68
69#define yajl_bs_push(obs, byte) {                       \
70    if (((obs).size - (obs).used) == 0) {               \
71        (obs).size += YAJL_BS_INC;                      \
72        (obs).stack = (obs).yaf->realloc((obs).yaf->ctx,\
73                                         (void *) (obs).stack, (obs).size);\
74    }                                                   \
75    (obs).stack[((obs).used)++] = (byte);               \
76}
77
78/* removes the top item of the stack, returns nothing */
79#define yajl_bs_pop(obs) { ((obs).used)--; }
80
81#define yajl_bs_set(obs, byte)                          \
82    (obs).stack[((obs).used) - 1] = (byte);
83
84
85#endif
86