commit 8aa773d253789bbbbec845bb6732ae66ce415e24
Author: Adam Jackson <ajax@redhat.com>
Date:   Mon Mar 23 13:42:09 2009 -0400

    Bludgeon MAXCLIENTS to be runtime-configurable.

diff --git a/dix/dispatch.c b/dix/dispatch.c
index 0ed95df..ce56e62 100644
--- a/dix/dispatch.c
+++ b/dix/dispatch.c
@@ -142,7 +142,7 @@ typedef const char *string;
 #include "Xserver-dtrace.h"
 #endif
 
-#define mskcnt ((MAXCLIENTS + 31) / 32)
+#define mskcnt ((512 /* "MAXCLIENTS" */ + 31) / 32)
 #define BITMASK(i) (1U << ((i) & 31))
 #define MASKIDX(i) ((i) >> 5)
 #define MASKWORD(buf, i) buf[MASKIDX(i)]
diff --git a/dix/globals.c b/dix/globals.c
index 2585798..7ee2dfe 100644
--- a/dix/globals.c
+++ b/dix/globals.c
@@ -78,7 +78,8 @@ PtrCtrl defaultPointerControl = {
 	DEFAULT_PTR_THRESHOLD,
 	0};
 
-ClientPtr  clients[MAXCLIENTS];
+int MAXCLIENTS = 256;
+ClientPtr  clients[512]; /* XXX ick ick ick */
 ClientPtr  serverClient;
 int  currentMaxClients;   /* current size of clients array */
 long maxBigRequestSize = MAX_BIG_REQUEST_SIZE;
diff --git a/dix/resource.c b/dix/resource.c
index dc6945d..daf8f4e 100644
--- a/dix/resource.c
+++ b/dix/resource.c
@@ -235,7 +235,7 @@ CreateNewResourceClass(void)
     return next;
 }
 
-static ClientResourceRec clientTable[MAXCLIENTS];
+static ClientResourceRec clientTable[512];
 
 /*****************
  * InitClientResources
diff --git a/include/dix.h b/include/dix.h
index 552a2d0..e78bb6b 100644
--- a/include/dix.h
+++ b/include/dix.h
@@ -120,7 +120,8 @@ typedef struct _Client *ClientPtr; /* also in misc.h */
 
 typedef struct _WorkQueue	*WorkQueuePtr;
 
-extern _X_EXPORT ClientPtr clients[MAXCLIENTS];
+extern _X_EXPORT int MAXCLIENTS;
+extern _X_EXPORT ClientPtr clients[512];
 extern _X_EXPORT ClientPtr serverClient;
 extern _X_EXPORT int currentMaxClients;
 extern _X_EXPORT char dispatchExceptionAtReset;
diff --git a/include/misc.h b/include/misc.h
index 6a920cc..4f5bcbf 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -83,7 +83,6 @@ OF THIS SOFTWARE.
 #ifndef MAXSCREENS
 #define MAXSCREENS	16
 #endif
-#define MAXCLIENTS	256
 #define MAXEXTENSIONS   128
 #define MAXFORMATS	8
 #define MAXDEVICES	20 /* input devices */
diff --git a/include/resource.h b/include/resource.h
index 774f81b..d35d33d 100644
--- a/include/resource.h
+++ b/include/resource.h
@@ -88,18 +88,26 @@ typedef unsigned long RESTYPE;
 
 /* bits and fields within a resource id */
 #define RESOURCE_AND_CLIENT_COUNT   29			/* 29 bits for XIDs */
-#if MAXCLIENTS == 64
-#define RESOURCE_CLIENT_BITS	6
-#endif
-#if MAXCLIENTS == 128
-#define RESOURCE_CLIENT_BITS	7
-#endif
-#if MAXCLIENTS == 256
-#define RESOURCE_CLIENT_BITS	8
-#endif
-#if MAXCLIENTS == 512
-#define RESOURCE_CLIENT_BITS	9
-#endif
+static inline int
+ilog2(int val)
+{
+    int bits;
+
+    if (val <= 0)
+	return 0;
+    for (bits = 0; val != 0; bits++)
+	val >>= 1;
+    return bits - 1;
+}
+
+extern int MAXCLIENTS; /* XXX ick */
+static inline int
+resource_client_bits(void)
+{
+    return ilog2(MAXCLIENTS);
+}
+#define RESOURCE_CLIENT_BITS resource_client_bits()
+
 /* client field offset */
 #define CLIENTOFFSET	    (RESOURCE_AND_CLIENT_COUNT - RESOURCE_CLIENT_BITS)
 /* resource field */
diff --git a/os/utils.c b/os/utils.c
index 56095b1..88664c7 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -513,6 +513,7 @@ void UseMsg(void)
     ErrorF("-logo                  enable logo in screen saver\n");
     ErrorF("nologo                 disable logo in screen saver\n");
 #endif
+    ErrorF("-maxclients n          set maximum number of clients (power of two)\n");
     ErrorF("-nolisten string       don't listen on protocol\n");
     ErrorF("-noreset               don't reset after last client exists\n");
     ErrorF("-reset                 reset after last client exists\n");
@@ -778,6 +779,19 @@ ProcessCommandLine(int argc, char *argv[])
 	    logoScreenSaver = 0;
 	}
 #endif
+	else if ( strcmp( argv[i], "-maxclients") == 0)
+	{
+	    if (++i < argc) {
+		MAXCLIENTS = atoi(argv[i]);
+		if (MAXCLIENTS != 64 &&
+		    MAXCLIENTS != 128 &&
+		    MAXCLIENTS != 256 &&
+		    MAXCLIENTS != 512) {
+		    FatalError("maxclients must be one of 64, 128, 256, or 512\n");
+		}
+	    } else
+		UseMsg();
+	}
 	else if ( strcmp( argv[i], "-nolisten") == 0)
 	{
             if(++i < argc) {
