ngtcp2: use custom mem funcs

Pass curl's memory functions to the nghttp3 and ngtcp2 functions that
allow them. This allows custom memory functions passed by the curl user
to be used in nghttp3 and ngtcp2.

Closes #18196
This commit is contained in:
David Zhuang 2025-08-05 17:45:06 -07:00 committed by Daniel Stenberg
parent fc4ae23cc2
commit 7dafe10db2
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 68 additions and 3 deletions

View file

@ -1212,7 +1212,7 @@ static CURLcode init_ngh3_conn(struct Curl_cfilter *cf,
rc = nghttp3_conn_client_new(&ctx->h3conn,
&ngh3_callbacks,
&ctx->h3settings,
nghttp3_mem_default(),
Curl_nghttp3_mem(),
cf);
if(rc) {
failf(data, "error creating nghttp3 connection instance");
@ -2475,7 +2475,7 @@ static const struct alpn_spec ALPN_SPEC_H3 = {
&ctx->connected_path,
NGTCP2_PROTO_VER_V1, &ng_callbacks,
&ctx->settings, &ctx->transport_params,
NULL, cf);
Curl_ngtcp2_mem(), cf);
if(rc)
return CURLE_QUIC_CONNECT_ERROR;

View file

@ -1106,7 +1106,7 @@ static CURLcode cf_osslq_h3conn_init(struct cf_osslq_ctx *ctx, SSL *conn,
rc = nghttp3_conn_client_new(&h3->conn,
&ngh3_callbacks,
&h3->settings,
nghttp3_mem_default(),
Curl_nghttp3_mem(),
user_data);
if(rc) {
result = CURLE_OUT_OF_MEMORY;

View file

@ -724,6 +724,62 @@ CURLcode Curl_conn_may_http3(struct Curl_easy *data,
return CURLE_OK;
}
#if defined(USE_NGTCP2) || defined(USE_NGHTTP3)
static void *Curl_ngtcp2_malloc(size_t size, void *user_data)
{
(void)user_data;
return Curl_cmalloc(size);
}
static void Curl_ngtcp2_free(void *ptr, void *user_data)
{
(void)user_data;
Curl_cfree(ptr);
}
static void *Curl_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data)
{
(void)user_data;
return Curl_ccalloc(nmemb, size);
}
static void *Curl_ngtcp2_realloc(void *ptr, size_t size, void *user_data)
{
(void)user_data;
return Curl_crealloc(ptr, size);
}
#ifdef USE_NGTCP2
static struct ngtcp2_mem curl_ngtcp2_mem = {
NULL,
Curl_ngtcp2_malloc,
Curl_ngtcp2_free,
Curl_ngtcp2_calloc,
Curl_ngtcp2_realloc
};
struct ngtcp2_mem *Curl_ngtcp2_mem(void)
{
return &curl_ngtcp2_mem;
}
#endif
#ifdef USE_NGHTTP3
static struct nghttp3_mem curl_nghttp3_mem = {
NULL,
Curl_ngtcp2_malloc,
Curl_ngtcp2_free,
Curl_ngtcp2_calloc,
Curl_ngtcp2_realloc
};
struct nghttp3_mem *Curl_nghttp3_mem(void)
{
return &curl_nghttp3_mem;
}
#endif
#endif /* USE_NGTCP2 || USE_NGHTTP3 */
#else /* CURL_DISABLE_HTTP || !USE_HTTP3 */
CURLcode Curl_conn_may_http3(struct Curl_easy *data,

View file

@ -91,4 +91,13 @@ CURLcode vquic_recv_packets(struct Curl_cfilter *cf,
#endif /* !USE_HTTP3 */
#ifdef USE_NGTCP2
struct ngtcp2_mem;
struct ngtcp2_mem *Curl_ngtcp2_mem(void);
#endif
#ifdef USE_NGHTTP3
struct nghttp3_mem;
struct nghttp3_mem *Curl_nghttp3_mem(void);
#endif
#endif /* HEADER_CURL_VQUIC_QUIC_INT_H */