nghttp2_rcbuf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2016 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "nghttp2_rcbuf.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "nghttp2_mem.h"
  29. int nghttp2_rcbuf_new(nghttp2_rcbuf **rcbuf_ptr, size_t size,
  30. nghttp2_mem *mem) {
  31. uint8_t *p;
  32. p = nghttp2_mem_malloc(mem, sizeof(nghttp2_rcbuf) + size);
  33. if (p == NULL) {
  34. return NGHTTP2_ERR_NOMEM;
  35. }
  36. *rcbuf_ptr = (void *)p;
  37. (*rcbuf_ptr)->mem_user_data = mem->mem_user_data;
  38. (*rcbuf_ptr)->free = mem->free;
  39. (*rcbuf_ptr)->base = p + sizeof(nghttp2_rcbuf);
  40. (*rcbuf_ptr)->len = size;
  41. (*rcbuf_ptr)->ref = 1;
  42. return 0;
  43. }
  44. int nghttp2_rcbuf_new2(nghttp2_rcbuf **rcbuf_ptr, const uint8_t *src,
  45. size_t srclen, nghttp2_mem *mem) {
  46. int rv;
  47. rv = nghttp2_rcbuf_new(rcbuf_ptr, srclen + 1, mem);
  48. if (rv != 0) {
  49. return rv;
  50. }
  51. memcpy((*rcbuf_ptr)->base, src, srclen);
  52. (*rcbuf_ptr)->len = srclen;
  53. (*rcbuf_ptr)->base[srclen] = '\0';
  54. return 0;
  55. }
  56. /*
  57. * Frees |rcbuf| itself, regardless of its reference cout.
  58. */
  59. void nghttp2_rcbuf_del(nghttp2_rcbuf *rcbuf) {
  60. nghttp2_mem_free2(rcbuf->free, rcbuf, rcbuf->mem_user_data);
  61. }
  62. void nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf) {
  63. if (rcbuf->ref == -1) {
  64. return;
  65. }
  66. ++rcbuf->ref;
  67. }
  68. void nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf) {
  69. if (rcbuf == NULL || rcbuf->ref == -1) {
  70. return;
  71. }
  72. assert(rcbuf->ref > 0);
  73. if (--rcbuf->ref == 0) {
  74. nghttp2_rcbuf_del(rcbuf);
  75. }
  76. }
  77. nghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf) {
  78. nghttp2_vec res = {rcbuf->base, rcbuf->len};
  79. return res;
  80. }