From ea618f9bff8d35314fec83cb5115915fc0a39c79 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Mon, 24 Feb 2025 12:15:01 +0100 Subject: [PATCH] xfree86: 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 --- hw/xfree86/common/xf86DGA.c | 2 +- hw/xfree86/common/xf86cmap.c | 6 +++--- hw/xfree86/common/xf86sbusBus.c | 2 +- hw/xfree86/common/xf86xvmc.c | 2 +- hw/xfree86/dri/xf86dri.c | 2 +- hw/xfree86/dri2/dri2.c | 4 ++-- hw/xfree86/drivers/modesetting/driver.c | 2 +- hw/xfree86/drivers/modesetting/drmmode_display.c | 2 +- hw/xfree86/int10/vbe.c | 4 ++-- hw/xfree86/loader/loadmod.c | 2 +- hw/xfree86/modes/xf86Crtc.c | 9 ++++----- hw/xfree86/modes/xf86DiDGA.c | 2 +- hw/xfree86/modes/xf86RandR12.c | 12 ++++++------ 13 files changed, 25 insertions(+), 26 deletions(-) diff --git a/hw/xfree86/common/xf86DGA.c b/hw/xfree86/common/xf86DGA.c index 2482530a1..55a251501 100644 --- a/hw/xfree86/common/xf86DGA.c +++ b/hw/xfree86/common/xf86DGA.c @@ -1322,7 +1322,7 @@ ProcXDGAQueryModes(ClientPtr client) return Success; } - if (!(mode = xallocarray(num, sizeof(XDGAModeRec)))) + if (!(mode = calloc(num, sizeof(XDGAModeRec)))) return BadAlloc; for (i = 0; i < num; i++) diff --git a/hw/xfree86/common/xf86cmap.c b/hw/xfree86/common/xf86cmap.c index 6d5f9ac90..bd3cbf778 100644 --- a/hw/xfree86/common/xf86cmap.c +++ b/hw/xfree86/common/xf86cmap.c @@ -160,10 +160,10 @@ xf86HandleColormaps(ScreenPtr pScreen, elements = 1 << sigRGBbits; - if (!(gamma = xallocarray(elements, sizeof(LOCO)))) + if (!(gamma = calloc(elements, sizeof(LOCO)))) return FALSE; - if (!(indices = xallocarray(maxColors, sizeof(int)))) { + if (!(indices = calloc(maxColors, sizeof(int)))) { free(gamma); return FALSE; } @@ -273,7 +273,7 @@ CMapAllocateColormapPrivate(ColormapPtr pmap) else numColors = 1 << pmap->pVisual->nplanes; - if (!(colors = xallocarray(numColors, sizeof(LOCO)))) + if (!(colors = calloc(numColors, sizeof(LOCO)))) return FALSE; if (!(pColPriv = malloc(sizeof(CMapColormapRec)))) { diff --git a/hw/xfree86/common/xf86sbusBus.c b/hw/xfree86/common/xf86sbusBus.c index 3beb17dfc..34cb0fac9 100644 --- a/hw/xfree86/common/xf86sbusBus.c +++ b/hw/xfree86/common/xf86sbusBus.c @@ -648,7 +648,7 @@ xf86SbusCmapLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices, return; fbcmap.count = 0; fbcmap.index = indices[0]; - fbcmap.red = data = xallocarray(numColors, 3); + fbcmap.red = data = calloc(numColors, 3); if (!data) return; fbcmap.green = data + numColors; diff --git a/hw/xfree86/common/xf86xvmc.c b/hw/xfree86/common/xf86xvmc.c index c67418bf4..b722e9903 100644 --- a/hw/xfree86/common/xf86xvmc.c +++ b/hw/xfree86/common/xf86xvmc.c @@ -155,7 +155,7 @@ xf86XvMCScreenInit(ScreenPtr pScreen, if (noXvExtension) return FALSE; - if (!(pAdapt = xallocarray(num_adaptors, sizeof(XvMCAdaptorRec)))) + if (!(pAdapt = calloc(num_adaptors, sizeof(XvMCAdaptorRec)))) return FALSE; if (!dixRegisterPrivateKey(&XF86XvMCScreenKeyRec, PRIVATE_SCREEN, 0)) { diff --git a/hw/xfree86/dri/xf86dri.c b/hw/xfree86/dri/xf86dri.c index 709420564..cde1383e3 100644 --- a/hw/xfree86/dri/xf86dri.c +++ b/hw/xfree86/dri/xf86dri.c @@ -423,7 +423,7 @@ ProcXF86DRIGetDrawableInfo(register ClientPtr client) if (rep.numClipRects) { /* Clip cliprects to screen dimensions (redirected windows) */ - pClippedRects = xallocarray(rep.numClipRects, sizeof(drm_clip_rect_t)); + pClippedRects = calloc(rep.numClipRects, sizeof(drm_clip_rect_t)); if (!pClippedRects) return BadAlloc; diff --git a/hw/xfree86/dri2/dri2.c b/hw/xfree86/dri2/dri2.c index 4ac0e144e..649b89b6a 100644 --- a/hw/xfree86/dri2/dri2.c +++ b/hw/xfree86/dri2/dri2.c @@ -1608,7 +1608,7 @@ DRI2ScreenInit(ScreenPtr pScreen, DRI2InfoPtr info) if (info->version == 3 || info->numDrivers == 0) { /* Driver too old: use the old-style driverName field */ ds->numDrivers = info->driverName ? 1 : 2; - ds->driverNames = xallocarray(ds->numDrivers, sizeof(*ds->driverNames)); + ds->driverNames = calloc(ds->numDrivers, sizeof(*ds->driverNames)); if (!ds->driverNames) goto err_out; @@ -1629,7 +1629,7 @@ DRI2ScreenInit(ScreenPtr pScreen, DRI2InfoPtr info) } else { ds->numDrivers = info->numDrivers; - ds->driverNames = xallocarray(info->numDrivers, sizeof(*ds->driverNames)); + ds->driverNames = calloc(info->numDrivers, sizeof(*ds->driverNames)); if (!ds->driverNames) goto err_out; memcpy(ds->driverNames, info->driverNames, diff --git a/hw/xfree86/drivers/modesetting/driver.c b/hw/xfree86/drivers/modesetting/driver.c index 264c6fe33..23ce276a0 100644 --- a/hw/xfree86/drivers/modesetting/driver.c +++ b/hw/xfree86/drivers/modesetting/driver.c @@ -588,7 +588,7 @@ dispatch_damages(ScrnInfoPtr scrn, xf86CrtcPtr crtc, RegionPtr dirty, return 0; if (num_cliprects) { - drmModeClip *clip = xallocarray(num_cliprects, sizeof(drmModeClip)); + drmModeClip *clip = calloc(num_cliprects, sizeof(drmModeClip)); BoxPtr rect = REGION_RECTS(dirty); int i; int c = 0; diff --git a/hw/xfree86/drivers/modesetting/drmmode_display.c b/hw/xfree86/drivers/modesetting/drmmode_display.c index cf4269403..3578e8da2 100644 --- a/hw/xfree86/drivers/modesetting/drmmode_display.c +++ b/hw/xfree86/drivers/modesetting/drmmode_display.c @@ -3799,7 +3799,7 @@ drmmode_create_lease(RRLeasePtr lease, int *fd) if (!lease_private) return BadAlloc; - objects = xallocarray(nobjects, sizeof (uint32_t)); + objects = calloc(nobjects, sizeof(uint32_t)); if (!objects) { free(lease_private); diff --git a/hw/xfree86/int10/vbe.c b/hw/xfree86/int10/vbe.c index a4f3af58c..5c093d73a 100644 --- a/hw/xfree86/int10/vbe.c +++ b/hw/xfree86/int10/vbe.c @@ -397,7 +397,7 @@ VBEGetVBEInfo(vbeInfoPtr pVbe) i = 0; while (modes[i] != 0xffff) i++; - block->VideoModePtr = xallocarray(i + 1, sizeof(CARD16)); + block->VideoModePtr = calloc(i + 1, sizeof(CARD16)); memcpy(block->VideoModePtr, modes, sizeof(CARD16) * i); block->VideoModePtr[i] = 0xffff; @@ -825,7 +825,7 @@ VBESetGetPaletteData(vbeInfoPtr pVbe, Bool set, int first, int num, if (set) return data; - data = xallocarray(num, sizeof(CARD32)); + data = calloc(num, sizeof(CARD32)); memcpy(data, pVbe->memory, num * sizeof(CARD32)); return data; diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c index 2edc14ac7..10ace089a 100644 --- a/hw/xfree86/loader/loadmod.c +++ b/hw/xfree86/loader/loadmod.c @@ -228,7 +228,7 @@ InitPatterns(const char **patternlist) for (i = 0, s = patternlist; *s; i++, s++) if (*s == DEFAULT_LIST) i += ARRAY_SIZE(stdPatterns) - 1 - 1; - patterns = xallocarray(i + 1, sizeof(PatternRec)); + patterns = calloc(i + 1, sizeof(PatternRec)); if (!patterns) { return NULL; } diff --git a/hw/xfree86/modes/xf86Crtc.c b/hw/xfree86/modes/xf86Crtc.c index 7b0401eae..a3d21bb92 100644 --- a/hw/xfree86/modes/xf86Crtc.c +++ b/hw/xfree86/modes/xf86Crtc.c @@ -114,7 +114,7 @@ xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs) /* Preallocate gamma at a sensible size. */ crtc->gamma_size = 256; - crtc->gamma_red = xallocarray(crtc->gamma_size, 3 * sizeof(CARD16)); + crtc->gamma_red = calloc(crtc->gamma_size, 3 * sizeof(CARD16)); if (!crtc->gamma_red) { free(crtc); return NULL; @@ -126,7 +126,7 @@ xf86CrtcCreate(ScrnInfoPtr scrn, const xf86CrtcFuncsRec * funcs) crtcs = reallocarray(xf86_config->crtc, xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr)); else - crtcs = xallocarray(xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr)); + crtcs = calloc(xf86_config->num_crtc + 1, sizeof(xf86CrtcPtr)); if (!crtcs) { free(crtc->gamma_red); free(crtc); @@ -670,8 +670,7 @@ xf86OutputCreate(ScrnInfoPtr scrn, xf86_config->num_output + 1, sizeof(xf86OutputPtr)); else - outputs = xallocarray(xf86_config->num_output + 1, - sizeof(xf86OutputPtr)); + outputs = calloc(xf86_config->num_output + 1, sizeof(xf86OutputPtr)); if (!outputs) { free(output); return NULL; @@ -992,7 +991,7 @@ xf86PickCrtcs(ScrnInfoPtr scrn, if (modes[n] == NULL) return best_score; - crtcs = xallocarray(config->num_output, sizeof(xf86CrtcPtr)); + crtcs = calloc(config->num_output, sizeof(xf86CrtcPtr)); if (!crtcs) return best_score; diff --git a/hw/xfree86/modes/xf86DiDGA.c b/hw/xfree86/modes/xf86DiDGA.c index 114b6932f..568d29a1e 100644 --- a/hw/xfree86/modes/xf86DiDGA.c +++ b/hw/xfree86/modes/xf86DiDGA.c @@ -57,7 +57,7 @@ xf86_dga_get_modes(ScreenPtr pScreen) if (!num) return FALSE; - modes = xallocarray(num, sizeof(DGAModeRec)); + modes = calloc(num, sizeof(DGAModeRec)); if (!modes) return FALSE; diff --git a/hw/xfree86/modes/xf86RandR12.c b/hw/xfree86/modes/xf86RandR12.c index ddcf5e748..aa50f6f25 100644 --- a/hw/xfree86/modes/xf86RandR12.c +++ b/hw/xfree86/modes/xf86RandR12.c @@ -1061,7 +1061,7 @@ xf86RandR12CrtcNotify(RRCrtcPtr randr_crtc) DisplayModePtr mode = &crtc->mode; Bool ret; - randr_outputs = xallocarray(config->num_output, sizeof(RROutputPtr)); + randr_outputs = calloc(config->num_output, sizeof(RROutputPtr)); if (!randr_outputs) return FALSE; x = crtc->x; @@ -1153,7 +1153,7 @@ xf86RandR12CrtcSet(ScreenPtr pScreen, if (!crtc->scrn->vtSema) return FALSE; - save_crtcs = xallocarray(config->num_output, sizeof(xf86CrtcPtr)); + save_crtcs = calloc(config->num_output, sizeof(xf86CrtcPtr)); if ((randr_mode != NULL) != crtc->enabled) changed = TRUE; else if (randr_mode && !xf86RandRModeMatches(randr_mode, &crtc->mode)) @@ -1429,7 +1429,7 @@ xf86RandR12CrtcInitGamma(xf86CrtcPtr crtc, float gamma_red, float gamma_green, (gamma_red != 1.0f || gamma_green != 1.0f || gamma_blue != 1.0f)) return FALSE; - red = xallocarray(size, 3 * sizeof(CARD16)); + red = calloc(size, 3 * sizeof(CARD16)); if (!red) return FALSE; @@ -1596,7 +1596,7 @@ xf86RROutputSetModes(RROutputPtr randr_output, DisplayModePtr modes) nmode++; if (nmode) { - rrmodes = xallocarray(nmode, sizeof(RRModePtr)); + rrmodes = calloc(nmode, sizeof(RRModePtr)); if (!rrmodes) return FALSE; @@ -1651,8 +1651,8 @@ xf86RandR12SetInfo12(ScreenPtr pScreen) int o, c, l; int nclone; - clones = xallocarray(config->num_output, sizeof(RROutputPtr)); - crtcs = xallocarray(config->num_crtc, sizeof(RRCrtcPtr)); + clones = calloc(config->num_output, sizeof(RROutputPtr)); + crtcs = calloc(config->num_crtc, sizeof(RRCrtcPtr)); for (o = 0; o < config->num_output; o++) { xf86OutputPtr output = config->output[o];