[PATCH xserver 1/3] os: Collect copy-pasted code into functions.

Rami Ylimäki rami.ylimaki at vincit.fi
Mon Oct 3 05:16:25 PDT 2011


No functional changes.

Signed-off-by: Rami Ylimäki <rami.ylimaki at vincit.fi>
Reviewed-by: Erkki Seppälä <erkki.seppala at vincit.fi>
---
 os/io.c |  126 +++++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 67 insertions(+), 59 deletions(-)

diff --git a/os/io.c b/os/io.c
index 955bf8b..6bfddfc 100644
--- a/os/io.c
+++ b/os/io.c
@@ -190,11 +190,69 @@ YieldControlDeath(void)
     timesThisConnection = 0;
 }
 
+/**
+ * Check whether the previous request indicated that an input buffer
+ * can be reused. If the input buffer belongs to the same client, it
+ * can be trivially reused. If the input buffer belongs to some other
+ * client, it is stolen and marked as free so that clients without
+ * input buffers can reuse it. However, if the input buffer has grown
+ * too large in the latter case, it's just released.
+ *
+ * @param[in] oc connection that requires an input buffer
+ */
+static void FreeAvailableInput(OsCommPtr oc)
+{
+    if (AvailableInput)
+    {
+	if (AvailableInput != oc)
+	{
+	    register ConnectionInputPtr aci = AvailableInput->input;
+	    if (aci->size > BUFWATERMARK)
+	    {
+		free(aci->buffer);
+		free(aci);
+	    }
+	    else
+	    {
+		aci->next = FreeInputs;
+		FreeInputs = aci;
+	    }
+	    AvailableInput->input = NULL;
+	}
+	AvailableInput = NULL;
+    }
+}
+
+/**
+ * Find an input buffer for a request. If a client already has an
+ * input buffer, nothing needs to be done. Otherwise the buffer is
+ * taken from the list of reusable input buffers. If no input buffers
+ * can be reused, a new one is allocated.
+ *
+ * @param[in,out] oc connection that requires an input buffer
+ *
+ * @return the input buffer that was given for the connection
+ */
+static ConnectionInputPtr ReuseFreeInput(OsCommPtr oc)
+{
+    ConnectionInputPtr oci = oc->input;
+    if (!oci)
+    {
+	if ((oci = FreeInputs))
+	    FreeInputs = oci->next;
+	else
+	    oci = AllocateInputBuffer();
+    }
+    if (oci)
+	oc->input = oci;
+    return oci;
+}
+
 int
 ReadRequestFromClient(ClientPtr client)
 {
     OsCommPtr oc = (OsCommPtr)client->osPrivate;
-    ConnectionInputPtr oci = oc->input;
+    ConnectionInputPtr oci;
     int fd = oc->fd;
     unsigned int gotnow, needed;
     int result;
@@ -208,40 +266,14 @@ ReadRequestFromClient(ClientPtr client)
      * times).  This was done to save memory.
      */
 
-    if (AvailableInput)
-    {
-	if (AvailableInput != oc)
-	{
-	    register ConnectionInputPtr aci = AvailableInput->input;
-	    if (aci->size > BUFWATERMARK)
-	    {
-		free(aci->buffer);
-		free(aci);
-	    }
-	    else
-	    {
-		aci->next = FreeInputs;
-		FreeInputs = aci;
-	    }
-	    AvailableInput->input = (ConnectionInputPtr)NULL;
-	}
-	AvailableInput = (OsCommPtr)NULL;
-    }
+    FreeAvailableInput(oc);
 
     /* make sure we have an input buffer */
 
-    if (!oci)
+    if (!(oci = ReuseFreeInput(oc)))
     {
-	if ((oci = FreeInputs))
-	{
-	    FreeInputs = oci->next;
-	}
-	else if (!(oci = AllocateInputBuffer()))
-	{
-	    YieldControlDeath();
-	    return -1;
-	}
-	oc->input = oci;
+        YieldControlDeath();
+        return -1;
     }
 
     /* advance to start of next request */
@@ -501,37 +533,13 @@ Bool
 InsertFakeRequest(ClientPtr client, char *data, int count)
 {
     OsCommPtr oc = (OsCommPtr)client->osPrivate;
-    ConnectionInputPtr oci = oc->input;
+    ConnectionInputPtr oci;
     int fd = oc->fd;
     int gotnow, moveup;
 
-    if (AvailableInput)
-    {
-	if (AvailableInput != oc)
-	{
-	    ConnectionInputPtr aci = AvailableInput->input;
-	    if (aci->size > BUFWATERMARK)
-	    {
-		free(aci->buffer);
-		free(aci);
-	    }
-	    else
-	    {
-		aci->next = FreeInputs;
-		FreeInputs = aci;
-	    }
-	    AvailableInput->input = (ConnectionInputPtr)NULL;
-	}
-	AvailableInput = (OsCommPtr)NULL;
-    }
-    if (!oci)
-    {
-	if ((oci = FreeInputs))
-	    FreeInputs = oci->next;
-	else if (!(oci = AllocateInputBuffer()))
-	    return FALSE;
-	oc->input = oci;
-    }
+    FreeAvailableInput(oc);
+    if (!(oci = ReuseFreeInput(oc)))
+        return FALSE;
     oci->bufptr += oci->lenLastReq;
     oci->lenLastReq = 0;
     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
-- 
1.7.1



More information about the xorg-devel mailing list