[PATCH v2 15/17] Convert hw/xnest & hw/vfb to new *allocarray functions

Alan Coopersmith alan.coopersmith at oracle.com
Thu Apr 16 18:49:23 PDT 2015


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>
---
 hw/vfb/InitOutput.c |    8 ++++----
 hw/xnest/Color.c    |   17 +++++++----------
 hw/xnest/Display.c  |    4 ++--
 hw/xnest/GC.c       |    5 ++---
 hw/xnest/Keyboard.c |    2 +-
 hw/xnest/Screen.c   |    4 ++--
 6 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/hw/vfb/InitOutput.c b/hw/vfb/InitOutput.c
index bcaaa85..8b867e3 100644
--- a/hw/vfb/InitOutput.c
+++ b/hw/vfb/InitOutput.c
@@ -292,7 +292,7 @@ ddxProcessArgument(int argc, char *argv[], int i)
 
         if (vfbNumScreens <= screenNum) {
             vfbScreens =
-                realloc(vfbScreens, sizeof(*vfbScreens) * (screenNum + 1));
+                reallocarray(vfbScreens, screenNum + 1, sizeof(*vfbScreens));
             if (!vfbScreens)
                 FatalError("Not enough memory for screen %d\n", screenNum);
             for (; vfbNumScreens <= screenNum; ++vfbNumScreens)
@@ -407,9 +407,9 @@ vfbInstallColormap(ColormapPtr pmap)
         swapcopy32(pXWDHeader->bits_per_rgb, pVisual->bitsPerRGBValue);
         swapcopy32(pXWDHeader->colormap_entries, pVisual->ColormapEntries);
 
-        ppix = (Pixel *) malloc(entries * sizeof(Pixel));
-        prgb = (xrgb *) malloc(entries * sizeof(xrgb));
-        defs = (xColorItem *) malloc(entries * sizeof(xColorItem));
+        ppix = xallocarray(entries, sizeof(Pixel));
+        prgb = xallocarray(entries, sizeof(xrgb));
+        defs = xallocarray(entries, sizeof(xColorItem));
 
         for (i = 0; i < entries; i++)
             ppix[i] = i;
diff --git a/hw/xnest/Color.c b/hw/xnest/Color.c
index 8d9d356..3a9e422 100644
--- a/hw/xnest/Color.c
+++ b/hw/xnest/Color.c
@@ -62,7 +62,7 @@ xnestCreateColormap(ColormapPtr pCmap)
 
     switch (pVisual->class) {
     case StaticGray:           /* read only */
-        colors = (XColor *) malloc(ncolors * sizeof(XColor));
+        colors = xallocarray(ncolors, sizeof(XColor));
         for (i = 0; i < ncolors; i++)
             colors[i].pixel = i;
         XQueryColors(xnestDisplay, xnestColormap(pCmap), colors, ncolors);
@@ -75,7 +75,7 @@ xnestCreateColormap(ColormapPtr pCmap)
         break;
 
     case StaticColor:          /* read only */
-        colors = (XColor *) malloc(ncolors * sizeof(XColor));
+        colors = xallocarray(ncolors, sizeof(XColor));
         for (i = 0; i < ncolors; i++)
             colors[i].pixel = i;
         XQueryColors(xnestDisplay, xnestColormap(pCmap), colors, ncolors);
@@ -88,7 +88,7 @@ xnestCreateColormap(ColormapPtr pCmap)
         break;
 
     case TrueColor:            /* read only */
-        colors = (XColor *) malloc(ncolors * sizeof(XColor));
+        colors = xallocarray(ncolors, sizeof(XColor));
         red = green = blue = 0L;
         redInc = lowbit(pVisual->redMask);
         greenInc = lowbit(pVisual->greenMask);
@@ -194,14 +194,12 @@ xnestSetInstalledColormapWindows(ScreenPtr pScreen)
     xnestInstalledColormapWindows icws;
     int numWindows;
 
-    icws.cmapIDs = (Colormap *) malloc(pScreen->maxInstalledCmaps *
-                                       sizeof(Colormap));
+    icws.cmapIDs = xallocarray(pScreen->maxInstalledCmaps, sizeof(Colormap));
     icws.numCmapIDs = xnestListInstalledColormaps(pScreen, icws.cmapIDs);
     icws.numWindows = 0;
     WalkTree(pScreen, xnestCountInstalledColormapWindows, (void *) &icws);
     if (icws.numWindows) {
-        icws.windows =
-            (Window *) malloc((icws.numWindows + 1) * sizeof(Window));
+        icws.windows = xallocarray(icws.numWindows + 1, sizeof(Window));
         icws.index = 0;
         WalkTree(pScreen, xnestGetInstalledColormapWindows, (void *) &icws);
         icws.windows[icws.numWindows] = xnestDefaultWindows[pScreen->myNum];
@@ -220,8 +218,7 @@ xnestSetInstalledColormapWindows(ScreenPtr pScreen)
 #ifdef _XSERVER64
         {
             int i;
-            Window64 *windows =
-                (Window64 *) malloc(numWindows * sizeof(Window64));
+            Window64 *windows = xallocarray(numWindows, sizeof(Window64));
 
             for (i = 0; i < numWindows; ++i)
                 windows[i] = icws.windows[i];
@@ -393,7 +390,7 @@ xnestStoreColors(ColormapPtr pCmap, int nColors, xColorItem * pColors)
 #ifdef _XSERVER64
     {
         int i;
-        XColor *pColors64 = (XColor *) malloc(nColors * sizeof(XColor));
+        XColor *pColors64 = xallocarray(nColors, sizeof(XColor));
 
         for (i = 0; i < nColors; ++i) {
             pColors64[i].pixel = pColors[i].pixel;
diff --git a/hw/xnest/Display.c b/hw/xnest/Display.c
index a2f8acb..e6d07df 100644
--- a/hw/xnest/Display.c
+++ b/hw/xnest/Display.c
@@ -121,8 +121,8 @@ xnestOpenDisplay(int argc, char *argv[])
     }
 
     xnestNumDefaultColormaps = xnestNumVisuals;
-    xnestDefaultColormaps = (Colormap *) malloc(xnestNumDefaultColormaps *
-                                                sizeof(Colormap));
+    xnestDefaultColormaps = xallocarray(xnestNumDefaultColormaps,
+                                        sizeof(Colormap));
     for (i = 0; i < xnestNumDefaultColormaps; i++)
         xnestDefaultColormaps[i] = XCreateColormap(xnestDisplay,
                                                    DefaultRootWindow
diff --git a/hw/xnest/GC.c b/hw/xnest/GC.c
index 96af6eb..ecfa61e 100644
--- a/hw/xnest/GC.c
+++ b/hw/xnest/GC.c
@@ -190,7 +190,7 @@ xnestDestroyGC(GCPtr pGC)
 void
 xnestChangeClip(GCPtr pGC, int type, void *pValue, int nRects)
 {
-    int i, size;
+    int i;
     BoxPtr pBox;
     XRectangle *pRects;
 
@@ -204,8 +204,7 @@ xnestChangeClip(GCPtr pGC, int type, void *pValue, int nRects)
 
     case CT_REGION:
         nRects = RegionNumRects((RegionPtr) pValue);
-        size = nRects * sizeof(*pRects);
-        pRects = (XRectangle *) malloc(size);
+        pRects = xallocarray(nRects, sizeof(*pRects));
         pBox = RegionRects((RegionPtr) pValue);
         for (i = nRects; i-- > 0;) {
             pRects[i].x = pBox[i].x1;
diff --git a/hw/xnest/Keyboard.c b/hw/xnest/Keyboard.c
index ee3f68e..002885f 100644
--- a/hw/xnest/Keyboard.c
+++ b/hw/xnest/Keyboard.c
@@ -134,7 +134,7 @@ xnestKeyboardProc(DeviceIntPtr pDev, int onoff)
                                            max_keycode - min_keycode + 1,
                                            &mapWidth);
             len = (max_keycode - min_keycode + 1) * mapWidth;
-            keymap = (KeySym *) malloc(len * sizeof(KeySym));
+            keymap = xallocarray(len, sizeof(KeySym));
             for (i = 0; i < len; ++i)
                 keymap[i] = keymap64[i];
             XFree(keymap64);
diff --git a/hw/xnest/Screen.c b/hw/xnest/Screen.c
index abb4d37..214b550 100644
--- a/hw/xnest/Screen.c
+++ b/hw/xnest/Screen.c
@@ -158,7 +158,7 @@ xnestOpenScreen(ScreenPtr pScreen, int argc, char *argv[])
     if (!dixRegisterPrivateKey(&xnestCursorScreenKeyRec, PRIVATE_SCREEN, 0))
         return FALSE;
 
-    visuals = (VisualPtr) malloc(xnestNumVisuals * sizeof(VisualRec));
+    visuals = xallocarray(xnestNumVisuals, sizeof(VisualRec));
     numVisuals = 0;
 
     depths = (DepthPtr) malloc(MAXDEPTH * sizeof(DepthRec));
@@ -224,7 +224,7 @@ xnestOpenScreen(ScreenPtr pScreen, int argc, char *argv[])
 
         numVisuals++;
     }
-    visuals = (VisualPtr) realloc(visuals, numVisuals * sizeof(VisualRec));
+    visuals = reallocarray(visuals, numVisuals, sizeof(VisualRec));
 
     defaultVisual = visuals[xnestDefaultVisualIndex].vid;
     rootDepth = visuals[xnestDefaultVisualIndex].nplanes;
-- 
1.7.9.2



More information about the xorg-devel mailing list