/src/nghttp2/lib/nghttp2_rcbuf.c
Line | Count | Source |
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 | | |
27 | | #include <string.h> |
28 | | #include <assert.h> |
29 | | |
30 | | #include "nghttp2_mem.h" |
31 | | #include "nghttp2_helper.h" |
32 | | |
33 | | int nghttp2_rcbuf_new(nghttp2_rcbuf **rcbuf_ptr, size_t size, |
34 | 165k | nghttp2_mem *mem) { |
35 | 165k | uint8_t *p; |
36 | | |
37 | 165k | p = nghttp2_mem_malloc(mem, sizeof(nghttp2_rcbuf) + size); |
38 | 165k | if (p == NULL) { |
39 | 0 | return NGHTTP2_ERR_NOMEM; |
40 | 0 | } |
41 | | |
42 | 165k | *rcbuf_ptr = (void *)p; |
43 | | |
44 | 165k | **rcbuf_ptr = (nghttp2_rcbuf){ |
45 | 165k | .mem_user_data = mem->mem_user_data, |
46 | 165k | .free = mem->free, |
47 | 165k | .base = p + sizeof(nghttp2_rcbuf), |
48 | 165k | .len = size, |
49 | 165k | .ref = 1, |
50 | 165k | }; |
51 | | |
52 | 165k | return 0; |
53 | 165k | } |
54 | | |
55 | | int nghttp2_rcbuf_new2(nghttp2_rcbuf **rcbuf_ptr, const uint8_t *src, |
56 | 0 | size_t srclen, nghttp2_mem *mem) { |
57 | 0 | int rv; |
58 | |
|
59 | 0 | rv = nghttp2_rcbuf_new(rcbuf_ptr, srclen + 1, mem); |
60 | 0 | if (rv != 0) { |
61 | 0 | return rv; |
62 | 0 | } |
63 | | |
64 | 0 | (*rcbuf_ptr)->len = srclen; |
65 | 0 | *nghttp2_cpymem((*rcbuf_ptr)->base, src, srclen) = '\0'; |
66 | |
|
67 | 0 | return 0; |
68 | 0 | } |
69 | | |
70 | | /* |
71 | | * Frees |rcbuf| itself, regardless of its reference cout. |
72 | | */ |
73 | 165k | void nghttp2_rcbuf_del(nghttp2_rcbuf *rcbuf) { |
74 | 165k | nghttp2_mem_free2(rcbuf->free, rcbuf, rcbuf->mem_user_data); |
75 | 165k | } |
76 | | |
77 | 69.4k | void nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf) { |
78 | 69.4k | if (rcbuf->ref == -1) { |
79 | 32.6k | return; |
80 | 32.6k | } |
81 | | |
82 | 36.8k | ++rcbuf->ref; |
83 | 36.8k | } |
84 | | |
85 | 559k | void nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf) { |
86 | 559k | if (rcbuf == NULL || rcbuf->ref == -1) { |
87 | 356k | return; |
88 | 356k | } |
89 | | |
90 | 559k | assert(rcbuf->ref > 0); |
91 | | |
92 | 202k | if (--rcbuf->ref == 0) { |
93 | 165k | nghttp2_rcbuf_del(rcbuf); |
94 | 165k | } |
95 | 202k | } |
96 | | |
97 | 0 | nghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf) { |
98 | 0 | nghttp2_vec res = {rcbuf->base, rcbuf->len}; |
99 | 0 | return res; |
100 | 0 | } |
101 | | |
102 | 0 | int nghttp2_rcbuf_is_static(const nghttp2_rcbuf *rcbuf) { |
103 | 0 | return rcbuf->ref == -1; |
104 | 0 | } |