• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 #include <assert.h>
29 
30 #include "nghttp3_mem.h"
31 #include "nghttp3_str.h"
32 
nghttp3_rcbuf_new(nghttp3_rcbuf ** rcbuf_ptr,size_t size,const nghttp3_mem * mem)33 int nghttp3_rcbuf_new(nghttp3_rcbuf **rcbuf_ptr, size_t size,
34                       const nghttp3_mem *mem) {
35   uint8_t *p;
36 
37   p = nghttp3_mem_malloc(mem, sizeof(nghttp3_rcbuf) + size);
38   if (p == NULL) {
39     return NGHTTP3_ERR_NOMEM;
40   }
41 
42   *rcbuf_ptr = (void *)p;
43 
44   (*rcbuf_ptr)->mem = mem;
45   (*rcbuf_ptr)->base = p + sizeof(nghttp3_rcbuf);
46   (*rcbuf_ptr)->len = size;
47   (*rcbuf_ptr)->ref = 1;
48 
49   return 0;
50 }
51 
nghttp3_rcbuf_new2(nghttp3_rcbuf ** rcbuf_ptr,const uint8_t * src,size_t srclen,const nghttp3_mem * mem)52 int nghttp3_rcbuf_new2(nghttp3_rcbuf **rcbuf_ptr, const uint8_t *src,
53                        size_t srclen, const nghttp3_mem *mem) {
54   int rv;
55   uint8_t *p;
56 
57   rv = nghttp3_rcbuf_new(rcbuf_ptr, srclen + 1, mem);
58   if (rv != 0) {
59     return rv;
60   }
61 
62   (*rcbuf_ptr)->len = srclen;
63   p = (*rcbuf_ptr)->base;
64 
65   if (srclen) {
66     p = nghttp3_cpymem(p, src, srclen);
67   }
68 
69   *p = '\0';
70 
71   return 0;
72 }
73 
74 /*
75  * Frees |rcbuf| itself, regardless of its reference cout.
76  */
nghttp3_rcbuf_del(nghttp3_rcbuf * rcbuf)77 void nghttp3_rcbuf_del(nghttp3_rcbuf *rcbuf) {
78   nghttp3_mem_free(rcbuf->mem, rcbuf);
79 }
80 
nghttp3_rcbuf_incref(nghttp3_rcbuf * rcbuf)81 void nghttp3_rcbuf_incref(nghttp3_rcbuf *rcbuf) {
82   if (rcbuf->ref == -1) {
83     return;
84   }
85 
86   ++rcbuf->ref;
87 }
88 
nghttp3_rcbuf_decref(nghttp3_rcbuf * rcbuf)89 void nghttp3_rcbuf_decref(nghttp3_rcbuf *rcbuf) {
90   if (rcbuf == NULL || rcbuf->ref == -1) {
91     return;
92   }
93 
94   assert(rcbuf->ref > 0);
95 
96   if (--rcbuf->ref == 0) {
97     nghttp3_rcbuf_del(rcbuf);
98   }
99 }
100 
nghttp3_rcbuf_get_buf(const nghttp3_rcbuf * rcbuf)101 nghttp3_vec nghttp3_rcbuf_get_buf(const nghttp3_rcbuf *rcbuf) {
102   nghttp3_vec res = {rcbuf->base, rcbuf->len};
103   return res;
104 }
105 
nghttp3_rcbuf_is_static(const nghttp3_rcbuf * rcbuf)106 int nghttp3_rcbuf_is_static(const nghttp3_rcbuf *rcbuf) {
107   return rcbuf->ref == -1;
108 }
109