[PATCH 08/17] Convert dix/* to new *allocarray functions

Matt Turner mattst88 at gmail.com
Tue Mar 31 20:05:14 PDT 2015


On Tue, Mar 31, 2015 at 5:50 PM, Alan Coopersmith
<alan.coopersmith at oracle.com> wrote:
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
>  dix/atom.c       |    4 +--
>  dix/colormap.c   |   71 +++++++++++++++++++++++++++---------------------------
>  dix/devices.c    |    4 +--
>  dix/dispatch.c   |    4 +--
>  dix/dixfonts.c   |   18 ++++++--------
>  dix/enterleave.c |    2 +-
>  dix/events.c     |    4 +--
>  dix/extension.c  |    6 ++---
>  dix/grabs.c      |    8 +++---
>  dix/property.c   |   10 ++++----
>  dix/region.c     |    2 +-
>  dix/resource.c   |    6 ++---
>  dix/touch.c      |    8 +++---
>  13 files changed, 71 insertions(+), 76 deletions(-)
>
> diff --git a/dix/atom.c b/dix/atom.c
> index 22cd0dc..4596545 100644
> --- a/dix/atom.c
> +++ b/dix/atom.c
> @@ -119,7 +119,7 @@ MakeAtom(const char *string, unsigned len, Bool makeit)
>          if ((lastAtom + 1) >= tableLength) {
>              NodePtr *table;
>
> -            table = realloc(nodeTable, tableLength * (2 * sizeof(NodePtr)));
> +            table = reallocarray(nodeTable, tableLength, (2 * sizeof(NodePtr)));

Remove extra parentheses.

>              if (!table) {
>                  if (nd->string != string) {
>                      /* nd->string has been strdup'ed */
> @@ -200,7 +200,7 @@ InitAtoms(void)
>  {
>      FreeAllAtoms();
>      tableLength = InitialTableSize;
> -    nodeTable = malloc(InitialTableSize * sizeof(NodePtr));
> +    nodeTable = xallocarray(InitialTableSize, sizeof(NodePtr));
>      if (!nodeTable)
>          AtomError();
>      nodeTable[None] = NULL;
> diff --git a/dix/colormap.c b/dix/colormap.c
> index 4d408d5..36df08f 100644
> --- a/dix/colormap.c
> +++ b/dix/colormap.c
> @@ -296,7 +296,7 @@ CreateColormap(Colormap mid, ScreenPtr pScreen, VisualPtr pVisual,
>          for (pent = &pmap->red[size - 1]; pent >= pmap->red; pent--)
>              pent->refcnt = AllocPrivate;
>          pmap->freeRed = 0;
> -        ppix = malloc(size * sizeof(Pixel));
> +        ppix = xallocarray(size, sizeof(Pixel));
>          if (!ppix) {
>              free(pmap);
>              return BadAlloc;
> @@ -337,7 +337,7 @@ CreateColormap(Colormap mid, ScreenPtr pScreen, VisualPtr pVisual,
>              for (pent = &pmap->green[size - 1]; pent >= pmap->green; pent--)
>                  pent->refcnt = AllocPrivate;
>              pmap->freeGreen = 0;
> -            ppix = malloc(size * sizeof(Pixel));
> +            ppix = xallocarray(size, sizeof(Pixel));
>              if (!ppix) {
>                  free(pmap->clientPixelsRed[client]);
>                  free(pmap);
> @@ -352,7 +352,7 @@ CreateColormap(Colormap mid, ScreenPtr pScreen, VisualPtr pVisual,
>              for (pent = &pmap->blue[size - 1]; pent >= pmap->blue; pent--)
>                  pent->refcnt = AllocPrivate;
>              pmap->freeBlue = 0;
> -            ppix = malloc(size * sizeof(Pixel));
> +            ppix = xallocarray(size, sizeof(Pixel));
>              if (!ppix) {
>                  free(pmap->clientPixelsGreen[client]);
>                  free(pmap->clientPixelsRed[client]);
> @@ -702,7 +702,7 @@ UpdateColors(ColormapPtr pmap)
>
>      pVisual = pmap->pVisual;
>      size = pVisual->ColormapEntries;
> -    defs = malloc(size * sizeof(xColorItem));
> +    defs = xallocarray(size, sizeof(xColorItem));
>      if (!defs)
>          return;
>      n = 0;
> @@ -792,8 +792,8 @@ AllocColor(ColormapPtr pmap,
>          *pgreen = pmap->red[pixR].co.local.green;
>          *pblue = pmap->red[pixR].co.local.blue;
>          npix = pmap->numPixelsRed[client];
> -        ppix = (Pixel *) realloc(pmap->clientPixelsRed[client],
> -                                 (npix + 1) * sizeof(Pixel));
> +        ppix = reallocarray(pmap->clientPixelsRed[client],
> +                            (npix + 1), sizeof(Pixel));

And here.

>          if (!ppix)
>              return BadAlloc;
>          ppix[npix] = pixR;
> @@ -814,22 +814,22 @@ AllocColor(ColormapPtr pmap,
>          *pgreen = pmap->green[pixG].co.local.green;
>          *pblue = pmap->blue[pixB].co.local.blue;
>          npix = pmap->numPixelsRed[client];
> -        ppix = (Pixel *) realloc(pmap->clientPixelsRed[client],
> -                                 (npix + 1) * sizeof(Pixel));
> +        ppix = reallocarray(pmap->clientPixelsRed[client],
> +                            (npix + 1), sizeof(Pixel));

And here.

>          if (!ppix)
>              return BadAlloc;
>          ppix[npix] = pixR;
>          pmap->clientPixelsRed[client] = ppix;
>          npix = pmap->numPixelsGreen[client];
> -        ppix = (Pixel *) realloc(pmap->clientPixelsGreen[client],
> -                                 (npix + 1) * sizeof(Pixel));
> +        ppix = reallocarray(pmap->clientPixelsGreen[client],
> +                            (npix + 1), sizeof(Pixel));

And here. (and other places below. I don't think you need me to point them out)


More information about the xorg-devel mailing list