yajl_bytestack.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. * A header only implementation of a simple stack of bytes, used in YAJL
  18. * to maintain parse state.
  19. */
  20. #ifndef __YAJL_BYTESTACK_H__
  21. #define __YAJL_BYTESTACK_H__
  22. #include "api/yajl_common.h"
  23. #define YAJL_BS_INC 128
  24. typedef struct yajl_bytestack_t
  25. {
  26. unsigned char * stack;
  27. size_t size;
  28. size_t used;
  29. yajl_alloc_funcs * yaf;
  30. } yajl_bytestack;
  31. /* initialize a bytestack */
  32. #define yajl_bs_init(obs, _yaf) { \
  33. (obs).stack = NULL; \
  34. (obs).size = 0; \
  35. (obs).used = 0; \
  36. (obs).yaf = (_yaf); \
  37. } \
  38. /* initialize a bytestack */
  39. #define yajl_bs_free(obs) \
  40. if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
  41. #define yajl_bs_current(obs) \
  42. (assert((obs).used > 0), (obs).stack[(obs).used - 1])
  43. #define yajl_bs_push(obs, byte) { \
  44. if (((obs).size - (obs).used) == 0) { \
  45. (obs).size += YAJL_BS_INC; \
  46. (obs).stack = (obs).yaf->realloc((obs).yaf->ctx,\
  47. (void *) (obs).stack, (obs).size);\
  48. } \
  49. (obs).stack[((obs).used)++] = (byte); \
  50. }
  51. /* removes the top item of the stack, returns nothing */
  52. #define yajl_bs_pop(obs) { ((obs).used)--; }
  53. #define yajl_bs_set(obs, byte) \
  54. (obs).stack[((obs).used) - 1] = (byte);
  55. #endif