Path: blob/master/Utilities/cmnghttp2/lib/nghttp2_rcbuf.c
3153 views
/*1* nghttp2 - HTTP/2 C Library2*3* Copyright (c) 2016 Tatsuhiro Tsujikawa4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sublicense, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice shall be14* included in all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE20* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*/24#include "nghttp2_rcbuf.h"2526#include <string.h>27#include <assert.h>2829#include "nghttp2_mem.h"30#include "nghttp2_helper.h"3132int nghttp2_rcbuf_new(nghttp2_rcbuf **rcbuf_ptr, size_t size,33nghttp2_mem *mem) {34uint8_t *p;3536p = nghttp2_mem_malloc(mem, sizeof(nghttp2_rcbuf) + size);37if (p == NULL) {38return NGHTTP2_ERR_NOMEM;39}4041*rcbuf_ptr = (void *)p;4243(*rcbuf_ptr)->mem_user_data = mem->mem_user_data;44(*rcbuf_ptr)->free = mem->free;45(*rcbuf_ptr)->base = p + sizeof(nghttp2_rcbuf);46(*rcbuf_ptr)->len = size;47(*rcbuf_ptr)->ref = 1;4849return 0;50}5152int nghttp2_rcbuf_new2(nghttp2_rcbuf **rcbuf_ptr, const uint8_t *src,53size_t srclen, nghttp2_mem *mem) {54int rv;5556rv = nghttp2_rcbuf_new(rcbuf_ptr, srclen + 1, mem);57if (rv != 0) {58return rv;59}6061(*rcbuf_ptr)->len = srclen;62*nghttp2_cpymem((*rcbuf_ptr)->base, src, srclen) = '\0';6364return 0;65}6667/*68* Frees |rcbuf| itself, regardless of its reference cout.69*/70void nghttp2_rcbuf_del(nghttp2_rcbuf *rcbuf) {71nghttp2_mem_free2(rcbuf->free, rcbuf, rcbuf->mem_user_data);72}7374void nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf) {75if (rcbuf->ref == -1) {76return;77}7879++rcbuf->ref;80}8182void nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf) {83if (rcbuf == NULL || rcbuf->ref == -1) {84return;85}8687assert(rcbuf->ref > 0);8889if (--rcbuf->ref == 0) {90nghttp2_rcbuf_del(rcbuf);91}92}9394nghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf) {95nghttp2_vec res = {rcbuf->base, rcbuf->len};96return res;97}9899int nghttp2_rcbuf_is_static(const nghttp2_rcbuf *rcbuf) {100return rcbuf->ref == -1;101}102103104