patch 7.4.1315
Problem:    Using a channel handle does not allow for freeing it when unused.
Solution:   Add the Channel variable type.
diff --git a/src/structs.h b/src/structs.h
index 65900e4..c8334cb 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -1127,7 +1127,8 @@
     VAR_DICT,	 /* "v_dict" is used */
     VAR_FLOAT,	 /* "v_float" is used */
     VAR_SPECIAL, /* "v_number" is used */
-    VAR_JOB	 /* "v_job" is used */
+    VAR_JOB,	 /* "v_job" is used */
+    VAR_CHANNEL	 /* "v_channel" is used */
 } vartype_T;
 
 /*
@@ -1149,6 +1150,9 @@
 #ifdef FEAT_JOB
 	job_T		*v_job;		/* job value (can be NULL!) */
 #endif
+#ifdef FEAT_CHANNEL
+	channel_T	*v_channel;	/* channel value (can be NULL!) */
+#endif
     }		vval;
 } typval_T;
 
@@ -1260,7 +1264,7 @@
     jobstatus_T	jv_status;
 
     int		jv_refcount;	/* reference count */
-    int		jv_channel;	/* channel for I/O */
+    channel_T	*jv_channel;	/* channel for I/O, reference counted */
 };
 
 /*
@@ -1268,35 +1272,41 @@
  */
 struct readq_S
 {
-    char_u	*buffer;
-    readq_T	*next;
-    readq_T	*prev;
+    char_u	*rq_buffer;
+    readq_T	*rq_next;
+    readq_T	*rq_prev;
 };
 
 struct jsonq_S
 {
-    typval_T	*value;
-    jsonq_T	*next;
-    jsonq_T	*prev;
+    typval_T	*jq_value;
+    jsonq_T	*jq_next;
+    jsonq_T	*jq_prev;
 };
 
 struct cbq_S
 {
-    char_u	*callback;
-    int		seq_nr;
-    cbq_T	*next;
-    cbq_T	*prev;
+    char_u	*cq_callback;
+    int		cq_seq_nr;
+    cbq_T	*cq_next;
+    cbq_T	*cq_prev;
 };
 
 /* mode for a channel */
 typedef enum
 {
-    MODE_RAW = 0,
+    MODE_NL = 0,
+    MODE_RAW,
     MODE_JSON,
     MODE_JS
 } ch_mode_T;
 
 struct channel_S {
+    channel_T	*ch_next;
+    channel_T	*ch_prev;
+
+    int		ch_id;		/* ID of the channel */
+
     sock_T	ch_sock;	/* the socket, -1 for a closed channel */
 
 #ifdef UNIX
@@ -1342,7 +1352,11 @@
 
     int		ch_timeout;	/* request timeout in msec */
 
-    job_T	*ch_job;	/* job that uses this channel */
+    job_T	*ch_job;	/* Job that uses this channel; this does not
+				 * count as a reference to avoid a circular
+				 * reference. */
+
+    int		ch_refcount;	/* reference count */
 };