Skip to content

use custom memory functions for nghttp3 and ngtcp2#18196

Closed
dvdzhuang wants to merge 5 commits intocurl:masterfrom
dvdzhuang:http3-memfuncs
Closed

use custom memory functions for nghttp3 and ngtcp2#18196
dvdzhuang wants to merge 5 commits intocurl:masterfrom
dvdzhuang:http3-memfuncs

Conversation

@dvdzhuang
Copy link
Copy Markdown
Contributor

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.

I added these functions in vquic since it's common to both curl_ngtcp2 and curl_osslq, but please let me know if there's a better place to put them. These functions are also the same as those used for nghttp2: 9089ef1

@github-actions github-actions bot added the HTTP/3 h3 or quic related label Aug 6, 2025
@dvdzhuang
Copy link
Copy Markdown
Contributor Author

Sorry, changes broke tests 2500 - 2503. Closing while I investigate

@dvdzhuang dvdzhuang closed this Aug 6, 2025
@dvdzhuang dvdzhuang reopened this Aug 8, 2025
@icing
Copy link
Copy Markdown
Contributor

icing commented Aug 11, 2025

Thanks, good idea! As to the nitty, gritty details, I would tweak this a bit like the following. This does not need the (void *) and casts and puts the declarations into the internal header file:

diff --git a/lib/vquic/curl_ngtcp2.c b/lib/vquic/curl_ngtcp2.c
index 643c403fec..575ebb9022 100644
--- a/lib/vquic/curl_ngtcp2.c
+++ b/lib/vquic/curl_ngtcp2.c
@@ -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 *)Curl_nghttp3_mem(),
+                               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,
-                              (ngtcp2_mem *)Curl_ngtcp2_mem(), cf);
+                              Curl_ngtcp2_mem(), cf);
   if(rc)
     return CURLE_QUIC_CONNECT_ERROR;
 
diff --git a/lib/vquic/vquic.c b/lib/vquic/vquic.c
index 11f337dc3d..7091c58c88 100644
--- a/lib/vquic/vquic.c
+++ b/lib/vquic/vquic.c
@@ -751,25 +751,31 @@ void *Curl_ngtcp2_realloc(void *ptr, size_t size, void *user_data)
 }
 
 #ifdef USE_NGTCP2
-static ngtcp2_mem curl_ngtcp2_mem_ = {
+static struct ngtcp2_mem curl_ngtcp2_mem = {
   NULL,
   Curl_ngtcp2_malloc,
   Curl_ngtcp2_free,
   Curl_ngtcp2_calloc,
   Curl_ngtcp2_realloc
 };
-void *Curl_ngtcp2_mem(void) { return (void *)&curl_ngtcp2_mem_; }
+struct ngtcp2_mem *Curl_ngtcp2_mem(void)
+{
+  return (void *)&curl_ngtcp2_mem;
+}
 #endif
 
 #ifdef USE_NGHTTP3
-static nghttp3_mem curl_nghttp3_mem_ = {
+static struct nghttp3_mem curl_nghttp3_mem = {
   NULL,
   Curl_ngtcp2_malloc,
   Curl_ngtcp2_free,
   Curl_ngtcp2_calloc,
   Curl_ngtcp2_realloc
 };
-void *Curl_nghttp3_mem(void) { return (void *)&curl_nghttp3_mem_; }
+struct nghttp3_mem *Curl_nghttp3_mem(void)
+{
+  return &curl_nghttp3_mem;
+}
 #endif
 
 #endif /* USE_NGTCP2 || USE_NGHTTP3 */
diff --git a/lib/vquic/vquic.h b/lib/vquic/vquic.h
index 2d5f225a64..0f81334f29 100644
--- a/lib/vquic/vquic.h
+++ b/lib/vquic/vquic.h
@@ -49,22 +49,6 @@ CURLcode Curl_cf_quic_create(struct Curl_cfilter **pcf,
 
 extern struct Curl_cftype Curl_cft_http3;
 
-#if defined(USE_NGTCP2) || defined(USE_NGHTTP3)
-
-void *Curl_ngtcp2_malloc(size_t size, void *user_data);
-void Curl_ngtcp2_free(void *ptr, void *user_data);
-void *Curl_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data);
-void *Curl_ngtcp2_realloc(void *ptr, size_t size, void *user_data);
-
-#ifdef USE_NGTCP2
-void *Curl_ngtcp2_mem(void);
-#endif
-#ifdef USE_NGHTTP3
-void *Curl_nghttp3_mem(void);
-#endif
-
-#endif /* USE_NGTCP2 || USE_NGHTTP3 */
-
 #else
 #define Curl_vquic_init() 1
 #endif /* !CURL_DISABLE_HTTP && USE_HTTP3 */
diff --git a/lib/vquic/vquic_int.h b/lib/vquic/vquic_int.h
index d271368d29..7e26095d1a 100644
--- a/lib/vquic/vquic_int.h
+++ b/lib/vquic/vquic_int.h
@@ -91,4 +91,21 @@ CURLcode vquic_recv_packets(struct Curl_cfilter *cf,
 
 #endif /* !USE_HTTP3 */
 
+#if defined(USE_NGTCP2) || defined(USE_NGHTTP3)
+void *Curl_ngtcp2_malloc(size_t size, void *user_data);
+void Curl_ngtcp2_free(void *ptr, void *user_data);
+void *Curl_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data);
+void *Curl_ngtcp2_realloc(void *ptr, size_t size, void *user_data);
+
+#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 /* USE_NGTCP2 || USE_NGHTTP3 */
+
 #endif /* HEADER_CURL_VQUIC_QUIC_INT_H */

@icing
Copy link
Copy Markdown
Contributor

icing commented Aug 11, 2025

Just noticed, the void * Curl_ngtcp2_*() functions could be static.

@dvdzhuang
Copy link
Copy Markdown
Contributor Author

Thanks; I've added your suggestions 👍

@bagder bagder marked this pull request as ready for review August 18, 2025 11:12
@bagder bagder closed this in 7dafe10 Aug 18, 2025
@bagder
Copy link
Copy Markdown
Member

bagder commented Aug 18, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

HTTP/3 h3 or quic related

Development

Successfully merging this pull request may close these issues.

3 participants