From e6e2b88cff693abbb297b69c40c2ce7f87de7b5f Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 30 Jul 2025 18:06:27 +0200 Subject: [PATCH] io: os: simplify FlushClient() Since nobody's passing in extra data here anymore, this function can be radically simplified now. Signed-off-by: Enrico Weigelt, metux IT consult --- os/connection.c | 2 +- os/io.c | 92 ++++++++----------------------------------------- os/osdep.h | 6 +--- 3 files changed, 17 insertions(+), 83 deletions(-) diff --git a/os/connection.c b/os/connection.c index 7a07670be6..b8e87ef2d0 100644 --- a/os/connection.c +++ b/os/connection.c @@ -784,7 +784,7 @@ CloseDownConnection(ClientPtr client) CallCallbacks(&FlushCallback, client); if (oc->output) - FlushClient(client, oc, (char *) NULL, 0); + FlushClient(client, oc); CloseDownFileDescriptor(oc); FreeOsBuffers(oc); free(client->osPrivate); diff --git a/os/io.c b/os/io.c index ff6d62cc4c..18cad3a473 100644 --- a/os/io.c +++ b/os/io.c @@ -631,7 +631,7 @@ FlushAllOutput(void) continue; if (!client_is_ready(client)) { oc = (OsCommPtr) client->osPrivate; - (void) FlushClient(client, oc, (char *) NULL, 0); + (void) FlushClient(client, oc); } else NewOutputPending = TRUE; } @@ -687,7 +687,7 @@ static bool OutputBufferMakeRoom(ClientPtr who, OsCommPtr oc, size_t sz) return true; /* try flushing the buffer */ - int ret = FlushClient(who, oc, NULL, 0); + int ret = FlushClient(who, oc); if (ret == -1) /* client was aborted */ return false; @@ -869,18 +869,12 @@ WriteToClient(ClientPtr who, int count, const void *__buf) **********************/ int -FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount) +FlushClient(ClientPtr who, OsCommPtr oc) { ConnectionOutputPtr oco = oc->output; XtransConnInfo trans_conn = oc->trans_conn; - struct iovec iov[3]; - static char padBuffer[3]; - const char *extraBuf = __extraBuf; - long written; - long padsize; - long notWritten; - long todo; + /* if no output buffer, then nothing to do */ if (!oco) return 0; @@ -892,55 +886,21 @@ FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount) return -1; } - written = 0; - padsize = padding_for_int32(extraCount); - notWritten = oco->count + extraCount + padsize; + size_t written = 0; + size_t notWritten = oco->count; + + /* do nothing if we haven't anything to write */ if (!notWritten) return 0; if (FlushCallback) CallCallbacks(&FlushCallback, who); - todo = notWritten; + size_t todo = notWritten; /* trying to write that much this time */ while (notWritten) { - long before = written; /* amount of whole thing written */ - long remain = todo; /* amount to try this time, <= notWritten */ - int i = 0; - long len; - - /* You could be very general here and have "in" and "out" iovecs - * and write a loop without using a macro, but what the heck. This - * translates to: - * - * how much of this piece is new? - * if more new then we are trying this time, clamp - * if nothing new - * then bump down amount already written, for next piece - * else put new stuff in iovec, will need all of next piece - * - * Note that todo had better be at least 1 or else we'll end up - * writing 0 iovecs. - */ -#define InsertIOV(pointer, length) \ - len = (length) - before; \ - if (len > remain) \ - len = remain; \ - if (len <= 0) { \ - before = (-len); \ - } else { \ - iov[i].iov_len = len; \ - iov[i].iov_base = (pointer) + before; \ - i++; \ - remain -= len; \ - before = 0; \ - } - - InsertIOV((char *) oco->buf, oco->count) - InsertIOV((char *) extraBuf, extraCount) - InsertIOV(padBuffer, padsize) - - errno = 0; - if ((len = _XSERVTransWritev(trans_conn, iov, i)) >= 0) { + errno = 0; + size_t len = _XSERVTransWrite(trans_conn, (const char*)oco->buf + written, todo); + if (len >= 0) { written += len; notWritten -= len; todo = notWritten; @@ -968,36 +928,14 @@ FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount) oco->count = 0; } - if (notWritten > oco->size) { - unsigned char *obuf = NULL; - - if (notWritten + BUFSIZE <= INT_MAX) { - obuf = realloc(oco->buf, notWritten + BUFSIZE); - } - if (!obuf) { - AbortClient(who); - dixMarkClientException(who); - oco->count = 0; - return -1; - } - oco->size = notWritten + BUFSIZE; - oco->buf = obuf; - } - - /* If the amount written extended into the padBuffer, then the - difference "extraCount - written" may be less than 0 */ - if ((len = extraCount - written) > 0) - memmove((char *) oco->buf + oco->count, - extraBuf + written, len); - - oco->count = notWritten; /* this will include the pad */ ospoll_listen(server_poll, oc->fd, X_NOTIFY_WRITE); /* return only the amount explicitly requested */ - return extraCount; + return 0; } #ifdef EMSGSIZE /* check for another brain-damaged OS bug */ else if (errno == EMSGSIZE) { + /* making separate try with half of the size */ todo >>= 1; } #endif @@ -1022,7 +960,7 @@ FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount) FreeOutputs = oco; } oc->output = (ConnectionOutputPtr) NULL; - return extraCount; /* return only the amount explicitly requested */ + return 0; /* return only the amount explicitly requested */ } static ConnectionInputPtr diff --git a/os/osdep.h b/os/osdep.h index f3e08d20e8..76202226b3 100644 --- a/os/osdep.h +++ b/os/osdep.h @@ -95,11 +95,7 @@ typedef struct _osComm { #define OS_COMM_GRAB_IMPERVIOUS 1 #define OS_COMM_IGNORED 2 -extern int FlushClient(ClientPtr /*who */ , - OsCommPtr /*oc */ , - const void * /*extraBuf */ , - int /*extraCount */ - ); +int FlushClient(ClientPtr who, OsCommPtr oc); extern void FreeOsBuffers(OsCommPtr /*oc */ );