[PATCH Xlib 1/3] Hoist most of _XConnectXCB into XOpenDisplay, and simplify

Mike Blumenkrantz zmike at samsung.com
Thu Jun 11 11:24:47 PDT 2015


Commit by Josh Triplett and Jamey Sharp.

Signed-off-by: Josh Triplett <josh at joshtriplett.org>
Signed-off-by: Jamey Sharp <jamey at minilop.net>
Signed-off-by: Mike Blumenkrantz <zmike at osg.samsung.com>
---
 src/OpenDis.c  | 44 +++++++++++++++++++++++++++++++++++---------
 src/Xxcbint.h  |  3 +--
 src/xcb_disp.c | 38 +++-----------------------------------
 3 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/src/OpenDis.c b/src/OpenDis.c
index 636860e..8f6d1ef 100644
--- a/src/OpenDis.c
+++ b/src/OpenDis.c
@@ -64,6 +64,7 @@ Display *
 XOpenDisplay (
 	register _Xconst char *display)
 {
+	xcb_connection_t *c;
 	register Display *dpy;		/* New Display object
being created. */ register int i;
 	int j, k;			/* random iterator indexes */
@@ -110,26 +111,42 @@ XOpenDisplay (
 	if (_XIOErrorFunction == NULL) (void) XSetIOErrorHandler
(NULL); 
 /*
+ * Call the Connect routine to get the transport connection object.
+ * If NULL is returned, the connection failed.
+ */
+
+	c = _XConnectXCB(display, &iscreen);
+	if(!c || xcb_connection_has_error(c)) {
+		return NULL;
+	}
+
+/*
  * Attempt to allocate a display structure. Return NULL if allocation
fails. */
-	if ((dpy = Xcalloc(1, sizeof(Display))) == NULL) {
-		return(NULL);
+	if ((dpy = (Display *)Xcalloc(1, sizeof(Display) +
sizeof(_X11XCBPrivate))) == NULL) {
+		xcb_disconnect(c);
+		return NULL ;
 	}
 
+	dpy->fd = xcb_get_file_descriptor(c);
+	dpy->xcb = (_X11XCBPrivate *) (dpy + 1);
+	dpy->xcb->connection = c;
+
 	if ((dpy->display_name = strdup(display_name)) == NULL) {
 		OutOfMemory(dpy);
-		return(NULL);
+		return NULL;
 	}
 
-/*
- * Call the Connect routine to get the transport connection object.
- * If NULL is returned, the connection failed.
- */
+	dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection);
 
-	if(!_XConnectXCB(dpy, display, &iscreen)) {
+	dpy->xcb->event_notify = xcondition_malloc();
+	dpy->xcb->reply_notify = xcondition_malloc();
+	if (!dpy->xcb->event_notify || !dpy->xcb->reply_notify) {
 		OutOfMemory(dpy);
 		return NULL;
 	}
+	xcondition_init(dpy->xcb->event_notify);
+	xcondition_init(dpy->xcb->reply_notify);
 
 	/* Initialize as much of the display structure as we can.
 	 * Initialize pointers to NULL so that XFreeDisplayStructure
will @@ -691,7 +708,16 @@ void _XFreeDisplayStructure(Display *dpy)
 
 	Xfree (dpy->filedes);
 
-	_XFreeX11XCBStructure(dpy);
+	/* reply_data was allocated by system malloc, not Xmalloc */
+	free(dpy->xcb->reply_data);
+	while(dpy->xcb->pending_requests)
+	{
+		PendingRequest *tmp = dpy->xcb->pending_requests;
+		dpy->xcb->pending_requests = tmp->next;
+		free(tmp);
+	}
+	xcondition_free(dpy->xcb->event_notify);
+	xcondition_free(dpy->xcb->reply_notify);
 
 	Xfree (dpy);
 }
diff --git a/src/Xxcbint.h b/src/Xxcbint.h
index bf41c23..b4f8988 100644
--- a/src/Xxcbint.h
+++ b/src/Xxcbint.h
@@ -43,8 +43,7 @@ typedef struct _X11XCBPrivate {
 
 /* xcb_disp.c */
 
-int _XConnectXCB(Display *dpy, _Xconst char *display, int *screenp);
-void _XFreeX11XCBStructure(Display *dpy);
+xcb_connection_t *_XConnectXCB(_Xconst char *display, int *screenp);
 
 unsigned long _XNextRequest(Display *dpy);
 
diff --git a/src/xcb_disp.c b/src/xcb_disp.c
index 0fa40de..74f87c0 100644
--- a/src/xcb_disp.c
+++ b/src/xcb_disp.c
@@ -54,20 +54,14 @@ void XSetAuthorization(char *name, int namelen,
char *data, int datalen) _XUnlockMutex(_Xglobal_lock);
 }
 
-int _XConnectXCB(Display *dpy, _Xconst char *display, int *screenp)
+xcb_connection_t *_XConnectXCB(_Xconst char *display, int *screenp)
 {
 	char *host;
 	int n = 0;
 	xcb_connection_t *c;
 
-	dpy->fd = -1;
-
-	dpy->xcb = Xcalloc(1, sizeof(_X11XCBPrivate));
-	if(!dpy->xcb)
-		return 0;
-
 	if(!xcb_parse_display(display, &host, &n, screenp))
-		return 0;
+		return NULL;
 	/* host and n are unused, but xcb_parse_display requires them
*/ free(host);
 
@@ -78,31 +72,5 @@ int _XConnectXCB(Display *dpy, _Xconst char
*display, int *screenp) c = xcb_connect(display, NULL);
 	_XUnlockMutex(_Xglobal_lock);
 
-	dpy->fd = xcb_get_file_descriptor(c);
-
-	dpy->xcb->connection = c;
-	dpy->xcb->next_xid = xcb_generate_id(dpy->xcb->connection);
-
-	dpy->xcb->event_notify = xcondition_malloc();
-	dpy->xcb->reply_notify = xcondition_malloc();
-	if (!dpy->xcb->event_notify || !dpy->xcb->reply_notify)
-		return 0;
-	xcondition_init(dpy->xcb->event_notify);
-	xcondition_init(dpy->xcb->reply_notify);
-	return !xcb_connection_has_error(c);
-}
-
-void _XFreeX11XCBStructure(Display *dpy)
-{
-	/* reply_data was allocated by system malloc, not Xmalloc */
-	free(dpy->xcb->reply_data);
-	while(dpy->xcb->pending_requests)
-	{
-		PendingRequest *tmp = dpy->xcb->pending_requests;
-		dpy->xcb->pending_requests = tmp->next;
-		free(tmp);
-	}
-	xcondition_free(dpy->xcb->event_notify);
-	xcondition_free(dpy->xcb->reply_notify);
-	Xfree(dpy->xcb);
+	return c;
 }
-- 
2.4.2



More information about the xorg-devel mailing list