11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * ngtcp2 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Copyright (c) 2022 ngtcp2 contributors 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#ifndef NGTCP2_BALLOC_H 261cb0ef41Sopenharmony_ci#define NGTCP2_BALLOC_H 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci#ifdef HAVE_CONFIG_H 291cb0ef41Sopenharmony_ci# include <config.h> 301cb0ef41Sopenharmony_ci#endif /* HAVE_CONFIG_H */ 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci#include <ngtcp2/ngtcp2.h> 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci#include "ngtcp2_buf.h" 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_citypedef struct ngtcp2_memblock_hd ngtcp2_memblock_hd; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci/* 391cb0ef41Sopenharmony_ci * ngtcp2_memblock_hd is the header of memory block. 401cb0ef41Sopenharmony_ci */ 411cb0ef41Sopenharmony_cistruct ngtcp2_memblock_hd { 421cb0ef41Sopenharmony_ci ngtcp2_memblock_hd *next; 431cb0ef41Sopenharmony_ci}; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci/* 461cb0ef41Sopenharmony_ci * ngtcp2_balloc is a custom memory allocator. It allocates |blklen| 471cb0ef41Sopenharmony_ci * bytes of memory at once on demand, and returns its slice when the 481cb0ef41Sopenharmony_ci * allocation is requested. 491cb0ef41Sopenharmony_ci */ 501cb0ef41Sopenharmony_citypedef struct ngtcp2_balloc { 511cb0ef41Sopenharmony_ci /* mem is the underlying memory allocator. */ 521cb0ef41Sopenharmony_ci const ngtcp2_mem *mem; 531cb0ef41Sopenharmony_ci /* blklen is the size of memory block. */ 541cb0ef41Sopenharmony_ci size_t blklen; 551cb0ef41Sopenharmony_ci /* head points to the list of memory block allocated so far. */ 561cb0ef41Sopenharmony_ci ngtcp2_memblock_hd *head; 571cb0ef41Sopenharmony_ci /* buf wraps the current memory block for allocation requests. */ 581cb0ef41Sopenharmony_ci ngtcp2_buf buf; 591cb0ef41Sopenharmony_ci} ngtcp2_balloc; 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci/* 621cb0ef41Sopenharmony_ci * ngtcp2_balloc_init initializes |balloc| with |blklen| which is the 631cb0ef41Sopenharmony_ci * size of memory block. 641cb0ef41Sopenharmony_ci */ 651cb0ef41Sopenharmony_civoid ngtcp2_balloc_init(ngtcp2_balloc *balloc, size_t blklen, 661cb0ef41Sopenharmony_ci const ngtcp2_mem *mem); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci/* 691cb0ef41Sopenharmony_ci * ngtcp2_balloc_free releases all allocated memory blocks. 701cb0ef41Sopenharmony_ci */ 711cb0ef41Sopenharmony_civoid ngtcp2_balloc_free(ngtcp2_balloc *balloc); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci/* 741cb0ef41Sopenharmony_ci * ngtcp2_balloc_get allocates |n| bytes of memory and assigns its 751cb0ef41Sopenharmony_ci * pointer to |*pbuf|. 761cb0ef41Sopenharmony_ci * 771cb0ef41Sopenharmony_ci * It returns 0 if it succeeds, or one of the following negative error 781cb0ef41Sopenharmony_ci * codes: 791cb0ef41Sopenharmony_ci * 801cb0ef41Sopenharmony_ci * NGTCP2_ERR_NOMEM 811cb0ef41Sopenharmony_ci * Out of memory. 821cb0ef41Sopenharmony_ci */ 831cb0ef41Sopenharmony_ciint ngtcp2_balloc_get(ngtcp2_balloc *balloc, void **pbuf, size_t n); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci/* 861cb0ef41Sopenharmony_ci * ngtcp2_balloc_clear releases all allocated memory blocks and 871cb0ef41Sopenharmony_ci * initializes its state. 881cb0ef41Sopenharmony_ci */ 891cb0ef41Sopenharmony_civoid ngtcp2_balloc_clear(ngtcp2_balloc *balloc); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci#endif /* NGTCP2_BALLOC_H */ 92