diff --git a/lib/context.c b/lib/context.c index 88e7fe455..2a56c6962 100644 --- a/lib/context.c +++ b/lib/context.c @@ -606,6 +606,9 @@ lws_create_context(struct lws_context_creation_info *info) context->pt_serv_buf_size = 4096; context->reject_service_keywords = info->reject_service_keywords; + if (info->external_baggage_free_on_destroy) + context->external_baggage_free_on_destroy = + info->external_baggage_free_on_destroy; context->time_up = time(NULL); #ifndef LWS_NO_DAEMONIZE @@ -960,5 +963,8 @@ lws_context_destroy(struct lws_context *context) lws_plat_context_late_destroy(context); + if (context->external_baggage_free_on_destroy) + free(context->external_baggage_free_on_destroy); + lws_free(context); } diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 8a2ae4be1..489db6f84 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1715,6 +1715,13 @@ struct lws_context_creation_info { * * Eg, "badrobot" "404 Not Found" */ + void *external_baggage_free_on_destroy; + /**< CONTEXT: NULL, or pointer to something externally malloc'd, that + * should be freed when the context is destroyed. This allows you to + * automatically sync the freeing action to the context destruction + * action, so there is no need for an external free() if the context + * succeeded to create. + */ /* Add new things just above here ---^ * This is part of the ABI, don't needlessly break compatibility diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 6a3ad8cb6..346abbdd7 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -833,6 +833,7 @@ struct lws_context { #endif struct lws_vhost *vhost_list; struct lws_plugin *plugin_list; + void *external_baggage_free_on_destroy; const struct lws_token_limits *token_limits; void *user_space; const char *server_string; diff --git a/lwsws/main.c b/lwsws/main.c index b4979192b..f94887890 100644 --- a/lwsws/main.c +++ b/lwsws/main.c @@ -161,6 +161,7 @@ int main(int argc, char **argv) memset(&info, 0, sizeof(info)); + info.external_baggage_free_on_destroy = config_strings; info.max_http_header_pool = 16; info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 | LWS_SERVER_OPTION_EXPLICIT_VHOSTS | @@ -200,7 +201,6 @@ int main(int argc, char **argv) } lws_context_destroy(context); - free(config_strings); fprintf(stderr, "lwsws exited cleanly\n");