From 69c88d9f0c8612e00c31d86bf5ae54f0232ca811 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sun, 4 Dec 2016 07:34:05 +0800 Subject: [PATCH] context: external_baggage_free_on_destroy This adds a context creation-time member that points to something that should be freed when the context is destroyed. It's in preparation for context deprecation, when a context might be destroyed asynchronously... a related external with the lifetime of thee context should also be freed at that time. Adapt lwsws to use it with the context "strings" (also used for aligned structs created by the config) allocation. --- lib/context.c | 6 ++++++ lib/libwebsockets.h | 7 +++++++ lib/private-libwebsockets.h | 1 + lwsws/main.c | 2 +- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/context.c b/lib/context.c index 88e7fe45..2a56c696 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 8a2ae4be..489db6f8 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 6a3ad8cb..346abbdd 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 b4979192..f9488789 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");