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 #ifndef NGHTTP2_RCBUF_H
26 #define NGHTTP2_RCBUF_H
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif /* HAVE_CONFIG_H */
31 
32 #include "nghttp2.h"
33 
34 struct nghttp2_rcbuf {
35     /* custom memory allocator belongs to the mem parameter when
36        creating this object. */
37     void *mem_user_data;
38     nghttp2_free free;
39     /* The pointer to the underlying buffer */
40     uint8_t *base;
41     /* Size of buffer pointed by |base|. */
42     size_t len;
43     /* Reference count */
44     int32_t ref;
45 };
46 
47 /*
48  * Allocates nghttp2_rcbuf object with |size| as initial buffer size.
49  * When the function succeeds, the reference count becomes 1.
50  *
51  * This function returns 0 if it succeeds, or one of the following
52  * negative error codes:
53  *
54  * NGHTTP2_ERR_NOMEM:
55  *     Out of memory.
56  */
57 int nghttp2_rcbuf_new(nghttp2_rcbuf **rcbuf_ptr, size_t size, nghttp2_mem *mem);
58 
59 /*
60  * Like nghttp2_rcbuf_new(), but initializes the buffer with |src| of
61  * length |srclen|.  This function allocates additional byte at the
62  * end and puts '\0' into it, so that the resulting buffer could be
63  * used as NULL-terminated string.  Still (*rcbuf_ptr)->len equals to
64  * |srclen|.
65  *
66  * This function returns 0 if it succeeds, or one of the following
67  * negative error codes:
68  *
69  * NGHTTP2_ERR_NOMEM:
70  *     Out of memory.
71  */
72 int nghttp2_rcbuf_new2(nghttp2_rcbuf **rcbuf_ptr, const uint8_t *src,
73                        size_t srclen, nghttp2_mem *mem);
74 
75 /*
76  * Frees |rcbuf| itself, regardless of its reference cout.
77  */
78 void nghttp2_rcbuf_del(nghttp2_rcbuf *rcbuf);
79 
80 #endif /* NGHTTP2_RCBUF_H */
81