[PATCH:xscope 09/14] Allocate buffers dynamically as needed instead of as part of fdinfo struct

Alan Coopersmith alan.coopersmith at ORACLE.COM
Sat Sep 24 08:48:27 PDT 2011


Previously we'd allocate a buffer of 32k each for each fd up to the
compiled in StaticMaxFD, which could be as large as 65535 on some
systems (that's the Solaris 64-bit default FD_SETSIZE), resulting in
gigabytes of bss allocation on startup.

Now we don't allocate until we're actually setting up the fdinfo for
use as part of a client<->server connection.
---
 scope.c |   16 ++++++++++++++++
 scope.h |    2 +-
 2 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/scope.c b/scope.c
index f85cdba..686be80 100644
--- a/scope.c
+++ b/scope.c
@@ -946,6 +946,12 @@ SetUpPair(
       FDinfo[client].Server = false;
       FDinfo[client].pair = server;
       FDinfo[client].ClientNumber = clientNumber;
+      if (FDinfo[client].buffer == NULL)
+	{
+	  FDinfo[client].buffer = calloc(1, BUFFER_SIZE);
+	  if (FDinfo[client].buffer == NULL)
+	    panic("unable to allocate client buffer");
+	}
       FDinfo[client].bufcount = 0;
       FDinfo[client].buflimit = -1;
       FDinfo[client].bufdelivered = 0;
@@ -954,6 +960,12 @@ SetUpPair(
 	  FDinfo[server].Server = true;
 	  FDinfo[server].pair = client;
 	  FDinfo[server].ClientNumber = FDinfo[client].ClientNumber;
+	  if (FDinfo[server].buffer == NULL)
+	    {
+	      FDinfo[server].buffer = calloc(1, BUFFER_SIZE);
+	      if (FDinfo[server].buffer == NULL)
+		panic("unable to allocate server buffer");
+	    }
 	  FDinfo[server].bufcount = 0;
 	  FDinfo[server].buflimit = -1;
 	  FDinfo[server].bufdelivered = 0;
@@ -973,12 +985,16 @@ ResetPair (
 {
   if (client >= 0)
   {
+    free(FDinfo[client].buffer);
+    FDinfo[client].buffer = NULL;
     FDinfo[client].bufcount = 0;
     FDinfo[client].buflimit = -1;
     FDinfo[client].bufdelivered = 0;
   }
   if (server >= 0)
   {
+    free(FDinfo[server].buffer);
+    FDinfo[server].buffer = NULL;
     FDinfo[server].bufcount = 0;
     FDinfo[server].buflimit = -1;
     FDinfo[server].bufdelivered = 0;
diff --git a/scope.h b/scope.h
index d440130..360cdc5 100644
--- a/scope.h
+++ b/scope.h
@@ -109,7 +109,7 @@ struct fdinfo
   Boolean Server;
   long    ClientNumber;
   FD	  pair;
-  unsigned char	  buffer[BUFFER_SIZE];
+  unsigned char	  *buffer;
   int	  bufcount;
   int	  bufstart;
   int	  buflimit;	/* limited writes */
-- 
1.7.3.2



More information about the xorg-devel mailing list