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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult
2025-07-30 18:06:27 +02:00
committed by Enrico Weigelt
parent 1298653ddb
commit e6e2b88cff
3 changed files with 17 additions and 83 deletions

View File

@@ -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);

92
os/io.c
View File

@@ -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

View File

@@ -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 */
);