nghttp3_rcbuf.c 2.7 KB

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