[PATCH] hg_close: close the connection for the given handle

Iulian Stana julian.stana at gmail.com
Wed Sep 18 15:42:32 CDT 2013


# HG changeset patch
# User Iulian Stana <julian.stana at gmail.com>
# Date 1379451734 -10800
#      Wed Sep 18 00:02:14 2013 +0300
# Node ID 408d6488f726fc88bba3e64dfcc881f1651f3b91
# Parent  5f658e94c158620549af557dba47fcab0ba9696f
hg_close: close the connection for the given handle

Erase the handle and free the memory.

The hg_close function will receive the handle by address, in this way after all
memory is released the handle is set to NULL. It's a defensive action to prevent
calling other functions after hg_close is called.

diff --git a/hglib/client.c b/hglib/client.c
--- a/hglib/client.c
+++ b/hglib/client.c
@@ -168,3 +168,34 @@
 	handle->out_data_size = 0;
 	return handle;
 }
+
+/* Release data from handle pointers. */
+void free_data(hg_handle *handle){
+	if (handle->out_data) {
+		free(handle->out_data);
+		handle->out_data = NULL;
+		handle->out_data_size = 0;
+	}
+}
+
+/*
+ * Close the connection for the given handle.
+ * */
+int hg_close(hg_handle **handle)
+{
+	if (!(*handle)) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	close((*handle)->p_read);
+	close((*handle)->p_write);
+
+	free((*handle)->header);
+	free_data(*handle);
+	free(*handle);
+	*handle = NULL;
+
+	return 0;
+}
+


More information about the Mercurial-devel mailing list