11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * nghttp2 - HTTP/2 C Library 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Copyright (c) 2016 Tatsuhiro Tsujikawa 51cb0ef41Sopenharmony_ci * 61cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 71cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the 81cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 91cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 101cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 111cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 121cb0ef41Sopenharmony_ci * the following conditions: 131cb0ef41Sopenharmony_ci * 141cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be 151cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software. 161cb0ef41Sopenharmony_ci * 171cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 181cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 191cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 201cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 211cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 221cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 231cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 241cb0ef41Sopenharmony_ci */ 251cb0ef41Sopenharmony_ci#include "nghttp2_rcbuf.h" 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci#include <string.h> 281cb0ef41Sopenharmony_ci#include <assert.h> 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci#include "nghttp2_mem.h" 311cb0ef41Sopenharmony_ci#include "nghttp2_helper.h" 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciint nghttp2_rcbuf_new(nghttp2_rcbuf **rcbuf_ptr, size_t size, 341cb0ef41Sopenharmony_ci nghttp2_mem *mem) { 351cb0ef41Sopenharmony_ci uint8_t *p; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci p = nghttp2_mem_malloc(mem, sizeof(nghttp2_rcbuf) + size); 381cb0ef41Sopenharmony_ci if (p == NULL) { 391cb0ef41Sopenharmony_ci return NGHTTP2_ERR_NOMEM; 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci *rcbuf_ptr = (void *)p; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci (*rcbuf_ptr)->mem_user_data = mem->mem_user_data; 451cb0ef41Sopenharmony_ci (*rcbuf_ptr)->free = mem->free; 461cb0ef41Sopenharmony_ci (*rcbuf_ptr)->base = p + sizeof(nghttp2_rcbuf); 471cb0ef41Sopenharmony_ci (*rcbuf_ptr)->len = size; 481cb0ef41Sopenharmony_ci (*rcbuf_ptr)->ref = 1; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci return 0; 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ciint nghttp2_rcbuf_new2(nghttp2_rcbuf **rcbuf_ptr, const uint8_t *src, 541cb0ef41Sopenharmony_ci size_t srclen, nghttp2_mem *mem) { 551cb0ef41Sopenharmony_ci int rv; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci rv = nghttp2_rcbuf_new(rcbuf_ptr, srclen + 1, mem); 581cb0ef41Sopenharmony_ci if (rv != 0) { 591cb0ef41Sopenharmony_ci return rv; 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci (*rcbuf_ptr)->len = srclen; 631cb0ef41Sopenharmony_ci *nghttp2_cpymem((*rcbuf_ptr)->base, src, srclen) = '\0'; 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci return 0; 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci/* 691cb0ef41Sopenharmony_ci * Frees |rcbuf| itself, regardless of its reference cout. 701cb0ef41Sopenharmony_ci */ 711cb0ef41Sopenharmony_civoid nghttp2_rcbuf_del(nghttp2_rcbuf *rcbuf) { 721cb0ef41Sopenharmony_ci nghttp2_mem_free2(rcbuf->free, rcbuf, rcbuf->mem_user_data); 731cb0ef41Sopenharmony_ci} 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_civoid nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf) { 761cb0ef41Sopenharmony_ci if (rcbuf->ref == -1) { 771cb0ef41Sopenharmony_ci return; 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci ++rcbuf->ref; 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_civoid nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf) { 841cb0ef41Sopenharmony_ci if (rcbuf == NULL || rcbuf->ref == -1) { 851cb0ef41Sopenharmony_ci return; 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci assert(rcbuf->ref > 0); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci if (--rcbuf->ref == 0) { 911cb0ef41Sopenharmony_ci nghttp2_rcbuf_del(rcbuf); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci} 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cinghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf) { 961cb0ef41Sopenharmony_ci nghttp2_vec res = {rcbuf->base, rcbuf->len}; 971cb0ef41Sopenharmony_ci return res; 981cb0ef41Sopenharmony_ci} 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ciint nghttp2_rcbuf_is_static(const nghttp2_rcbuf *rcbuf) { 1011cb0ef41Sopenharmony_ci return rcbuf->ref == -1; 1021cb0ef41Sopenharmony_ci} 103