valgrind introduce protocol init and destroy user callbacks

To get a clean bill of health from valgrind, we have to have a way to
inform the user code that we're going down and it should free everything
it is holding that was malloc'd.

This patch introduces LWS_CALLBACK_PROTOCOL_DESTROY which each protocol
gets when the context is being destroyed and no more activity will come
after that call.  They can get rid of everything there.

To match it, LWS_CALLBACK_PROTOCOL_INIT is introduced which would allow
one-time init per protocol too.

Signed-off-by: Andy Green <andy.green@linaro.org>
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index e5d148a..49a51fe 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -1041,6 +1041,7 @@
 	int n;
 	int m;
 	struct libwebsocket_extension *ext;
+	struct libwebsocket_protocols *protocol = context->protocols;
 
 #ifdef LWS_LATENCY
 	if (context->worst_latency_info[0])
@@ -1067,6 +1068,18 @@
 		ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
 		ext++;
 	}
+
+	/*
+	 * inform all the protocols that they are done and will have no more
+	 * callbacks
+	 */
+
+	while (protocol->callback) {
+		protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
+				NULL, NULL, 0);
+		protocol++;
+	}
+
 #endif
 
 #ifdef WIN32
@@ -2023,6 +2036,13 @@
 									context;
 		info->protocols[context->count_protocols].protocol_index =
 						       context->count_protocols;
+
+		/*
+		 * inform all the protocols that they are doing their one-time
+		 * initialization if they want to
+		 */
+		info->protocols[context->count_protocols].callback(context,
+			       NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
 	}
 
 #ifndef LWS_NO_EXTENSIONS