xkb: ProcXkbGetKbdByName(): collect sub-replies in buffer and write at once

Instead of dozens of little WriteToClient() calls, collect the sub-replies in
a buffer and send the whole reply out at once. This also allows more upcoming
simplifications in the send path.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-07-25 20:32:07 +02:00
parent 9b3959214d
commit 7b40cc9718

View File

@ -6039,20 +6039,21 @@ ProcXkbGetKbdByName(ClientPtr client)
.length = payload_length, .length = payload_length,
}; };
char *payload_buffer = calloc(1, payload_length * 4);
if (!payload_buffer)
return BadAlloc;
char *payload_walk = payload_buffer;
if (client->swapped) { if (client->swapped) {
swaps(&rep.sequenceNumber); swaps(&rep.sequenceNumber);
swapl(&rep.length); swapl(&rep.length);
swaps(&rep.found); swaps(&rep.found);
swaps(&rep.reported); swaps(&rep.reported);
} }
WriteToClient(client, SIZEOF(xkbGetKbdByNameReply), &rep);
if (reported & (XkbGBN_SymbolsMask | XkbGBN_TypesMask)) { if (reported & (XkbGBN_SymbolsMask | XkbGBN_TypesMask)) {
int sz = (mrep.length * sizeof(CARD32)) - (sizeof(mrep) - sizeof(xGenericReply)); char *buf = payload_walk + sizeof(mrep);
char *buf = calloc(1, sz);
if (!buf)
return BadAlloc;
XkbAssembleMap(client, xkb, mrep, buf); XkbAssembleMap(client, xkb, mrep, buf);
if (client->swapped) { if (client->swapped) {
@ -6062,17 +6063,13 @@ ProcXkbGetKbdByName(ClientPtr client)
swaps(&mrep.totalSyms); swaps(&mrep.totalSyms);
swaps(&mrep.totalActs); swaps(&mrep.totalActs);
} }
WriteToClient(client, sizeof(mrep), &mrep);
WriteToClient(client, sz, buf); memcpy(payload_walk, &mrep, sizeof(mrep));
free(buf); payload_walk = buf + (mrep.length * 4) - (sizeof(mrep) - sizeof(xGenericReply));
} }
if (reported & XkbGBN_CompatMapMask) { if (reported & XkbGBN_CompatMapMask) {
int sz = crep.length * sizeof(CARD32); char *buf = payload_walk + sizeof(crep);
char *buf = calloc(1, sz);
if (!buf)
return BadAlloc;
XkbAssembleCompatMap(client, new->compat, crep, buf); XkbAssembleCompatMap(client, new->compat, crep, buf);
if (client->swapped) { if (client->swapped) {
@ -6083,17 +6080,12 @@ ProcXkbGetKbdByName(ClientPtr client)
swaps(&crep.nTotalSI); swaps(&crep.nTotalSI);
} }
WriteToClient(client, sizeof(crep), &crep); memcpy(payload_walk, &crep, sizeof(crep));
WriteToClient(client, sz, buf); payload_walk = buf + (crep.length * 4) - (sizeof(crep) - sizeof(xGenericReply));
free(buf);
} }
if (reported & XkbGBN_IndicatorMapMask) { if (reported & XkbGBN_IndicatorMapMask) {
int sz = irep.length * sizeof(CARD32); char *buf = payload_walk + sizeof(irep);
char *buf = calloc(1, sz);
if (!buf)
return BadAlloc;
XkbAssembleIndicatorMap(client, new->indicators, irep, buf); XkbAssembleIndicatorMap(client, new->indicators, irep, buf);
if (client->swapped) { if (client->swapped) {
@ -6103,15 +6095,12 @@ ProcXkbGetKbdByName(ClientPtr client)
swapl(&irep.realIndicators); swapl(&irep.realIndicators);
} }
WriteToClient(client, sizeof(irep), &irep); memcpy(payload_walk, &irep, sizeof(irep));
WriteToClient(client, sz, buf); payload_walk = buf + (irep.length * 4) - (sizeof(irep) - sizeof(xGenericReply));
free(buf);
} }
if (reported & (XkbGBN_KeyNamesMask | XkbGBN_OtherNamesMask)) { if (reported & (XkbGBN_KeyNamesMask | XkbGBN_OtherNamesMask)) {
int sz = nrep.length * sizeof(CARD32); char *buf = payload_walk + sizeof(nrep);
char *buf = calloc(1, sz);
XkbAssembleNames(client, new, nrep, buf); XkbAssembleNames(client, new, nrep, buf);
if (client->swapped) { if (client->swapped) {
@ -6122,14 +6111,12 @@ ProcXkbGetKbdByName(ClientPtr client)
swapl(&nrep.indicators); swapl(&nrep.indicators);
} }
WriteToClient(client, sizeof(nrep), &nrep); memcpy(payload_walk, &nrep, sizeof(nrep));
WriteToClient(client, sz, buf); payload_walk = buf + (nrep.length * 4) - (sizeof(nrep) - sizeof(xGenericReply));
free(buf);
} }
if (reported & XkbGBN_GeometryMask) { if (reported & XkbGBN_GeometryMask) {
char buf[grep.length * 4]; char *buf = payload_walk + sizeof(grep);
XkbAssembleGeometry(client, new->geom, grep, buf); XkbAssembleGeometry(client, new->geom, grep, buf);
if (client->swapped) { if (client->swapped) {
@ -6146,10 +6133,15 @@ ProcXkbGetKbdByName(ClientPtr client)
swaps(&grep.nKeyAliases); swaps(&grep.nKeyAliases);
} }
WriteToClient(client, sizeof(xkbGetGeometryReply), &grep); memcpy(payload_walk, &grep, sizeof(grep));
WriteToClient(client, sizeof(buf), buf); payload_walk = buf + (grep.length * 4) - (sizeof(grep) - sizeof(xGenericReply));
} }
WriteToClient(client, sizeof(xkbGetKbdByNameReply), &rep);
WriteToClient(client, payload_length * 4, payload_buffer);
free(payload_buffer);
if (loaded) { if (loaded) {
XkbDescPtr old_xkb; XkbDescPtr old_xkb;