yajl_buf.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "yajl_buf.h"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #define YAJL_BUF_INIT_SIZE 2048
  21. struct yajl_buf_t {
  22. size_t len;
  23. size_t used;
  24. unsigned char * data;
  25. yajl_alloc_funcs * alloc;
  26. };
  27. static
  28. void yajl_buf_ensure_available(yajl_buf buf, size_t want)
  29. {
  30. size_t need;
  31. assert(buf != NULL);
  32. /* first call */
  33. if (buf->data == NULL) {
  34. buf->len = YAJL_BUF_INIT_SIZE;
  35. buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
  36. buf->data[0] = 0;
  37. }
  38. need = buf->len;
  39. while (want >= (need - buf->used)) need <<= 1;
  40. if (need != buf->len) {
  41. buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
  42. buf->len = need;
  43. }
  44. }
  45. yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
  46. {
  47. yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
  48. memset((void *) b, 0, sizeof(struct yajl_buf_t));
  49. b->alloc = alloc;
  50. return b;
  51. }
  52. void yajl_buf_free(yajl_buf buf)
  53. {
  54. assert(buf != NULL);
  55. if (buf->data) YA_FREE(buf->alloc, buf->data);
  56. YA_FREE(buf->alloc, buf);
  57. }
  58. void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
  59. {
  60. yajl_buf_ensure_available(buf, len);
  61. if (len > 0) {
  62. assert(data != NULL);
  63. memcpy(buf->data + buf->used, data, len);
  64. buf->used += len;
  65. buf->data[buf->used] = 0;
  66. }
  67. }
  68. void yajl_buf_clear(yajl_buf buf)
  69. {
  70. buf->used = 0;
  71. if (buf->data) buf->data[buf->used] = 0;
  72. }
  73. const unsigned char * yajl_buf_data(yajl_buf buf)
  74. {
  75. return buf->data;
  76. }
  77. size_t yajl_buf_len(yajl_buf buf)
  78. {
  79. return buf->used;
  80. }
  81. size_t yajl_buf_capacity(yajl_buf buf)
  82. {
  83. return buf->len;
  84. }
  85. void
  86. yajl_buf_truncate(yajl_buf buf, size_t len)
  87. {
  88. assert(len <= buf->used);
  89. buf->used = len;
  90. }