xext: replace xallocarray() by calloc()

Only key difference that calloc(), in contrast to rellocarray(),
is zero-initializing. The overhead is hard to measure on today's
machines, and it's safer programming practise to always allocate
zero-initialized, so one can't forget to do it explicitly.

Cocci rule:

    @@
    expression COUNT;
    expression LEN;
    @@
    - xallocarray(COUNT,LEN)
    + calloc(COUNT,LEN)

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-02-24 12:15:01 +01:00
parent 3d60cfbb68
commit dda64fcadf
9 changed files with 22 additions and 22 deletions

View File

@ -55,7 +55,7 @@ ht_create(int keySize,
ht->elements = 0; ht->elements = 0;
ht->bucketBits = INITHASHSIZE; ht->bucketBits = INITHASHSIZE;
numBuckets = 1 << ht->bucketBits; numBuckets = 1 << ht->bucketBits;
ht->buckets = xallocarray(numBuckets, sizeof(*ht->buckets)); ht->buckets = calloc(numBuckets, sizeof(*ht->buckets));
ht->cdata = cdata; ht->cdata = cdata;
if (ht->buckets) { if (ht->buckets) {
@ -96,7 +96,7 @@ double_size(HashTable ht)
int newNumBuckets = 1 << newBucketBits; int newNumBuckets = 1 << newBucketBits;
int c; int c;
newBuckets = xallocarray(newNumBuckets, sizeof(*ht->buckets)); newBuckets = calloc(newNumBuckets, sizeof(*ht->buckets));
if (newBuckets) { if (newBuckets) {
for (c = 0; c < newNumBuckets; ++c) { for (c = 0; c < newNumBuckets; ++c) {
xorg_list_init(&newBuckets[c]); xorg_list_init(&newBuckets[c]);

View File

@ -1410,7 +1410,7 @@ PanoramiXPolyPoint(ClientPtr client)
isRoot = (draw->type == XRT_WINDOW) && draw->u.win.root; isRoot = (draw->type == XRT_WINDOW) && draw->u.win.root;
npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyPointReq)); npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyPointReq));
if (npoint > 0) { if (npoint > 0) {
origPts = xallocarray(npoint, sizeof(xPoint)); origPts = calloc(npoint, sizeof(xPoint));
memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint)); memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1475,7 +1475,7 @@ PanoramiXPolyLine(ClientPtr client)
isRoot = IS_ROOT_DRAWABLE(draw); isRoot = IS_ROOT_DRAWABLE(draw);
npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyLineReq)); npoint = bytes_to_int32((client->req_len << 2) - sizeof(xPolyLineReq));
if (npoint > 0) { if (npoint > 0) {
origPts = xallocarray(npoint, sizeof(xPoint)); origPts = calloc(npoint, sizeof(xPoint));
memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint)); memcpy((char *) origPts, (char *) &stuff[1], npoint * sizeof(xPoint));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1544,7 +1544,7 @@ PanoramiXPolySegment(ClientPtr client)
return BadLength; return BadLength;
nsegs >>= 3; nsegs >>= 3;
if (nsegs > 0) { if (nsegs > 0) {
origSegs = xallocarray(nsegs, sizeof(xSegment)); origSegs = calloc(nsegs, sizeof(xSegment));
memcpy((char *) origSegs, (char *) &stuff[1], nsegs * sizeof(xSegment)); memcpy((char *) origSegs, (char *) &stuff[1], nsegs * sizeof(xSegment));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1612,7 +1612,7 @@ PanoramiXPolyRectangle(ClientPtr client)
return BadLength; return BadLength;
nrects >>= 3; nrects >>= 3;
if (nrects > 0) { if (nrects > 0) {
origRecs = xallocarray(nrects, sizeof(xRectangle)); origRecs = calloc(nrects, sizeof(xRectangle));
memcpy((char *) origRecs, (char *) &stuff[1], memcpy((char *) origRecs, (char *) &stuff[1],
nrects * sizeof(xRectangle)); nrects * sizeof(xRectangle));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1679,7 +1679,7 @@ PanoramiXPolyArc(ClientPtr client)
return BadLength; return BadLength;
narcs /= sizeof(xArc); narcs /= sizeof(xArc);
if (narcs > 0) { if (narcs > 0) {
origArcs = xallocarray(narcs, sizeof(xArc)); origArcs = calloc(narcs, sizeof(xArc));
memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc)); memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1741,7 +1741,7 @@ PanoramiXFillPoly(ClientPtr client)
count = bytes_to_int32((client->req_len << 2) - sizeof(xFillPolyReq)); count = bytes_to_int32((client->req_len << 2) - sizeof(xFillPolyReq));
if (count > 0) { if (count > 0) {
locPts = xallocarray(count, sizeof(DDXPointRec)); locPts = calloc(count, sizeof(DDXPointRec));
memcpy((char *) locPts, (char *) &stuff[1], memcpy((char *) locPts, (char *) &stuff[1],
count * sizeof(DDXPointRec)); count * sizeof(DDXPointRec));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1810,7 +1810,7 @@ PanoramiXPolyFillRectangle(ClientPtr client)
return BadLength; return BadLength;
things >>= 3; things >>= 3;
if (things > 0) { if (things > 0) {
origRects = xallocarray(things, sizeof(xRectangle)); origRects = calloc(things, sizeof(xRectangle));
memcpy((char *) origRects, (char *) &stuff[1], memcpy((char *) origRects, (char *) &stuff[1],
things * sizeof(xRectangle)); things * sizeof(xRectangle));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -1877,7 +1877,7 @@ PanoramiXPolyFillArc(ClientPtr client)
return BadLength; return BadLength;
narcs /= sizeof(xArc); narcs /= sizeof(xArc);
if (narcs > 0) { if (narcs > 0) {
origArcs = xallocarray(narcs, sizeof(xArc)); origArcs = calloc(narcs, sizeof(xArc));
memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc)); memcpy((char *) origArcs, (char *) &stuff[1], narcs * sizeof(xArc));
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -2063,7 +2063,7 @@ PanoramiXGetImage(ClientPtr client)
if (linesPerBuf > h) if (linesPerBuf > h)
linesPerBuf = h; linesPerBuf = h;
} }
if (!(pBuf = xallocarray(linesPerBuf, widthBytesLine))) if (!(pBuf = calloc(linesPerBuf, widthBytesLine)))
return BadAlloc; return BadAlloc;
if (client->swapped) { if (client->swapped) {

View File

@ -822,7 +822,7 @@ ScreenSaverSetAttributes(ClientPtr client, xScreenSaverSetAttributesReq *stuff)
goto bail; goto bail;
} }
/* over allocate for override redirect */ /* over allocate for override redirect */
pAttr->values = values = xallocarray(len + 1, sizeof(unsigned long)); pAttr->values = values = calloc(len + 1, sizeof(unsigned long));
if (!values) { if (!values) {
ret = BadAlloc; ret = BadAlloc;
goto bail; goto bail;

View File

@ -650,7 +650,7 @@ SyncAwaitTriggerFired(SyncTrigger * pTrigger)
pAwaitUnion = (SyncAwaitUnion *) pAwait->pHeader; pAwaitUnion = (SyncAwaitUnion *) pAwait->pHeader;
numwaits = pAwaitUnion->header.num_waitconditions; numwaits = pAwaitUnion->header.num_waitconditions;
ppAwait = xallocarray(numwaits, sizeof(SyncAwait *)); ppAwait = calloc(numwaits, sizeof(SyncAwait *));
if (!ppAwait) if (!ppAwait)
goto bail; goto bail;
@ -1546,7 +1546,7 @@ SyncAwaitPrologue(ClientPtr client, int items)
/* all the memory for the entire await list is allocated /* all the memory for the entire await list is allocated
* here in one chunk * here in one chunk
*/ */
pAwaitUnion = xallocarray(items + 1, sizeof(SyncAwaitUnion)); pAwaitUnion = calloc(items + 1, sizeof(SyncAwaitUnion));
if (!pAwaitUnion) if (!pAwaitUnion)
return NULL; return NULL;

View File

@ -1202,11 +1202,11 @@ ProcVidModeGetMonitor(ClientPtr client)
pad_to_int32(rep.modelLength)); pad_to_int32(rep.modelLength));
rep.nhsync = nHsync; rep.nhsync = nHsync;
rep.nvsync = nVrefresh; rep.nvsync = nVrefresh;
hsyncdata = xallocarray(nHsync, sizeof(CARD32)); hsyncdata = calloc(nHsync, sizeof(CARD32));
if (!hsyncdata) { if (!hsyncdata) {
return BadAlloc; return BadAlloc;
} }
vsyncdata = xallocarray(nVrefresh, sizeof(CARD32)); vsyncdata = calloc(nVrefresh, sizeof(CARD32));
if (!vsyncdata) { if (!vsyncdata) {
free(hsyncdata); free(hsyncdata);
@ -1524,7 +1524,7 @@ ProcVidModeGetGammaRamp(ClientPtr client)
length = (stuff->size + 1) & ~1; length = (stuff->size + 1) & ~1;
if (stuff->size) { if (stuff->size) {
if (!(ramp = xallocarray(length, 3 * sizeof(CARD16)))) if (!(ramp = calloc(length, 3 * sizeof(CARD16))))
return BadAlloc; return BadAlloc;
ramplen = length * 3 * sizeof(CARD16); ramplen = length * 3 * sizeof(CARD16);

View File

@ -101,7 +101,7 @@ ProcXCMiscGetXIDList(ClientPtr client)
if (stuff->count > UINT32_MAX / sizeof(XID)) if (stuff->count > UINT32_MAX / sizeof(XID))
return BadAlloc; return BadAlloc;
pids = xallocarray(stuff->count, sizeof(XID)); pids = calloc(stuff->count, sizeof(XID));
if (!pids) { if (!pids) {
return BadAlloc; return BadAlloc;
} }

View File

@ -389,7 +389,7 @@ ProcXF86BigfontQueryFont(ClientPtr client)
} }
else { else {
#endif #endif
pCI = xallocarray(nCharInfos, sizeof(xCharInfo)); pCI = calloc(nCharInfos, sizeof(xCharInfo));
if (!pCI) if (!pCI)
return BadAlloc; return BadAlloc;
#ifdef MITSHM #ifdef MITSHM
@ -451,7 +451,7 @@ ProcXF86BigfontQueryFont(ClientPtr client)
if (hashModulus > nCharInfos + 1) if (hashModulus > nCharInfos + 1)
hashModulus = nCharInfos + 1; hashModulus = nCharInfos + 1;
tmp = xallocarray(4 * nCharInfos + 1, sizeof(CARD16)); tmp = calloc(4 * nCharInfos + 1, sizeof(CARD16));
if (!tmp) { if (!tmp) {
if (!pDesc) if (!pDesc)
free(pCI); free(pCI);

View File

@ -226,7 +226,7 @@ ProcXResQueryClients(ClientPtr client)
REQUEST_SIZE_MATCH(xXResQueryClientsReq); REQUEST_SIZE_MATCH(xXResQueryClientsReq);
current_clients = xallocarray(currentMaxClients, sizeof(int)); current_clients = calloc(currentMaxClients, sizeof(int));
num_clients = 0; num_clients = 0;
for (i = 0; i < currentMaxClients; i++) { for (i = 0; i < currentMaxClients; i++) {

View File

@ -1048,7 +1048,7 @@ XvFillColorKey(DrawablePtr pDraw, CARD32 key, RegionPtr region)
(void) ChangeGC(NullClient, gc, GCForeground | GCSubwindowMode, pval); (void) ChangeGC(NullClient, gc, GCForeground | GCSubwindowMode, pval);
ValidateGC(pDraw, gc); ValidateGC(pDraw, gc);
rects = xallocarray(nbox, sizeof(xRectangle)); rects = calloc(nbox, sizeof(xRectangle));
if (rects) { if (rects) {
for (i = 0; i < nbox; i++, pbox++) { for (i = 0; i < nbox; i++, pbox++) {
rects[i].x = pbox->x1 - pDraw->x; rects[i].x = pbox->x1 - pDraw->x;