[Mesa-dev] [PATCH 6/9] Use calloc instead of malloc/memset-0

Matt Turner mattst88 at gmail.com
Tue Sep 4 23:42:47 PDT 2012


This patch has been generated by the following Coccinelle semantic
patch:

@@
expression E;
identifier I;
@@
- I = malloc(E);
+ I = calloc(1, E);
...
- memset(I, 0, sizeof *I);
---
 src/egl/drivers/dri2/egl_dri2.c         |    4 +---
 src/egl/drivers/dri2/platform_drm.c     |    7 ++-----
 src/egl/drivers/dri2/platform_wayland.c |    4 +---
 src/egl/drivers/dri2/platform_x11.c     |    8 ++------
 src/glx/applegl_glx.c                   |    3 +--
 src/glx/dri2_glx.c                      |   12 ++++--------
 src/glx/dri_glx.c                       |    6 ++----
 src/glx/drisw_glx.c                     |    9 +++------
 src/glx/indirect_glx.c                  |    6 ++----
 9 files changed, 18 insertions(+), 41 deletions(-)

diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index f99bcad..4b58c35 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -1477,12 +1477,10 @@ _eglBuiltInDriverDRI2(const char *args)
 
    (void) args;
 
-   dri2_drv = malloc(sizeof *dri2_drv);
+   dri2_drv = calloc(1, sizeof *dri2_drv);
    if (!dri2_drv)
       return NULL;
 
-   memset(dri2_drv, 0, sizeof *dri2_drv);
-
    if (!dri2_load(&dri2_drv->base)) {
       free(dri2_drv);
       return NULL;
diff --git a/src/egl/drivers/dri2/platform_drm.c b/src/egl/drivers/dri2/platform_drm.c
index 9aafb52..3e04a6c 100644
--- a/src/egl/drivers/dri2/platform_drm.c
+++ b/src/egl/drivers/dri2/platform_drm.c
@@ -96,13 +96,12 @@ dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
 
    (void) drv;
 
-   dri2_surf = malloc(sizeof *dri2_surf);
+   dri2_surf = calloc(1, sizeof *dri2_surf);
    if (!dri2_surf) {
       _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
       return NULL;
    }
 
-   memset(dri2_surf, 0, sizeof *dri2_surf);
    if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
       goto cleanup_surf;
 
@@ -399,12 +398,10 @@ dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
    int fd = -1;
    int i;
 
-   dri2_dpy = malloc(sizeof *dri2_dpy);
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   memset(dri2_dpy, 0, sizeof *dri2_dpy);
-
    disp->DriverData = (void *) dri2_dpy;
 
    gbm = disp->PlatformDisplay;
diff --git a/src/egl/drivers/dri2/platform_wayland.c b/src/egl/drivers/dri2/platform_wayland.c
index d291f0f..d9b45f1 100644
--- a/src/egl/drivers/dri2/platform_wayland.c
+++ b/src/egl/drivers/dri2/platform_wayland.c
@@ -813,12 +813,10 @@ dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
    drv->API.CreateImageKHR = dri2_wayland_create_image_khr;
    drv->API.Terminate = dri2_terminate;
 
-   dri2_dpy = malloc(sizeof *dri2_dpy);
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   memset(dri2_dpy, 0, sizeof *dri2_dpy);
-
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
       dri2_dpy->wl_dpy = wl_display_connect(NULL);
diff --git a/src/egl/drivers/dri2/platform_x11.c b/src/egl/drivers/dri2/platform_x11.c
index f7dc89d..2533774 100644
--- a/src/egl/drivers/dri2/platform_x11.c
+++ b/src/egl/drivers/dri2/platform_x11.c
@@ -980,12 +980,10 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
    drv->API.CreateDRMImageMESA = NULL;
    drv->API.ExportDRMImageMESA = NULL;
 
-   dri2_dpy = malloc(sizeof *dri2_dpy);
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   memset(dri2_dpy, 0, sizeof *dri2_dpy);
-
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
       dri2_dpy->conn = xcb_connect(0, 0);
@@ -1056,12 +1054,10 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    drv->API.PostSubBufferNV = dri2_post_sub_buffer;
    drv->API.SwapInterval = dri2_swap_interval;
 
-   dri2_dpy = malloc(sizeof *dri2_dpy);
+   dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   memset(dri2_dpy, 0, sizeof *dri2_dpy);
-
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
       dri2_dpy->conn = xcb_connect(0, 0);
diff --git a/src/glx/applegl_glx.c b/src/glx/applegl_glx.c
index c3597ce..a67416e 100644
--- a/src/glx/applegl_glx.c
+++ b/src/glx/applegl_glx.c
@@ -173,11 +173,10 @@ applegl_create_screen(int screen, struct glx_display * priv)
 {
    struct glx_screen *psc;
 
-   psc = malloc(sizeof *psc);
+   psc = calloc(1, sizeof *psc);
    if (psc == NULL)
       return NULL;
 
-   memset(psc, 0, sizeof *psc);
    glx_screen_init(psc, screen, priv);
    psc->vtable = &applegl_screen_vtable;
 
diff --git a/src/glx/dri2_glx.c b/src/glx/dri2_glx.c
index 5644f66..f2fc187 100644
--- a/src/glx/dri2_glx.c
+++ b/src/glx/dri2_glx.c
@@ -198,11 +198,10 @@ dri2_create_context(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = malloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL)
       return NULL;
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
       free(pcp);
       return NULL;
@@ -261,13 +260,12 @@ dri2_create_context_attribs(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = malloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL) {
       *error = __DRI_CTX_ERROR_NO_MEMORY;
       goto error_exit;
    }
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base))
       goto error_exit;
 
@@ -352,11 +350,10 @@ dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
    struct dri2_display *pdp;
    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
 
-   pdraw = malloc(sizeof(*pdraw));
+   pdraw = calloc(1, sizeof(*pdraw));
    if (!pdraw)
       return NULL;
 
-   memset(pdraw, 0, sizeof *pdraw);
    pdraw->base.destroyDrawable = dri2DestroyDrawable;
    pdraw->base.xDrawable = xDrawable;
    pdraw->base.drawable = drawable;
@@ -1018,11 +1015,10 @@ dri2CreateScreen(int screen, struct glx_display * priv)
    drm_magic_t magic;
    int i;
 
-   psc = malloc(sizeof *psc);
+   psc = calloc(1, sizeof *psc);
    if (psc == NULL)
       return NULL;
 
-   memset(psc, 0, sizeof *psc);
    psc->fd = -1;
 
    if (!glx_screen_init(&psc->base, screen, priv)) {
diff --git a/src/glx/dri_glx.c b/src/glx/dri_glx.c
index a158de0..3a727e5 100644
--- a/src/glx/dri_glx.c
+++ b/src/glx/dri_glx.c
@@ -593,11 +593,10 @@ dri_create_context(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = malloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL)
       return NULL;
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
       free(pcp);
       return NULL;
@@ -651,11 +650,10 @@ driCreateDrawable(struct glx_screen *base,
    if (xDrawable != drawable)
       return NULL;
 
-   pdp = malloc(sizeof *pdp);
+   pdp = calloc(1, sizeof *pdp);
    if (!pdp)
       return NULL;
 
-   memset(pdp, 0, sizeof *pdp);
    pdp->base.drawable = drawable;
    pdp->base.psc = &psc->base;
 
diff --git a/src/glx/drisw_glx.c b/src/glx/drisw_glx.c
index b68e622..920a9ab 100644
--- a/src/glx/drisw_glx.c
+++ b/src/glx/drisw_glx.c
@@ -392,11 +392,10 @@ drisw_create_context(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = malloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL)
       return NULL;
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
       free(pcp);
       return NULL;
@@ -457,11 +456,10 @@ drisw_create_context_attribs(struct glx_screen *base,
       shared = pcp_shared->driContext;
    }
 
-   pcp = malloc(sizeof *pcp);
+   pcp = calloc(1, sizeof *pcp);
    if (pcp == NULL)
       return NULL;
 
-   memset(pcp, 0, sizeof *pcp);
    if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
       free(pcp);
       return NULL;
@@ -522,11 +520,10 @@ driswCreateDrawable(struct glx_screen *base, XID xDrawable,
    Bool ret;
    const __DRIswrastExtension *swrast = psc->swrast;
 
-   pdp = malloc(sizeof(*pdp));
+   pdp = calloc(1, sizeof(*pdp));
    if (!pdp)
       return NULL;
 
-   memset(pdp, 0, sizeof *pdp);
    pdp->base.xDrawable = xDrawable;
    pdp->base.drawable = drawable;
    pdp->base.psc = &psc->base;
diff --git a/src/glx/indirect_glx.c b/src/glx/indirect_glx.c
index 5eef59c..f3cc1e7 100644
--- a/src/glx/indirect_glx.c
+++ b/src/glx/indirect_glx.c
@@ -352,12 +352,11 @@ indirect_create_context(struct glx_screen *psc,
    }
 
    /* Allocate our context record */
-   gc = malloc(sizeof *gc);
+   gc = calloc(1, sizeof *gc);
    if (!gc) {
       /* Out of memory */
       return NULL;
    }
-   memset(gc, 0, sizeof *gc);
 
    glx_context_init(gc, psc, mode);
    gc->isDirect = GL_FALSE;
@@ -464,11 +463,10 @@ indirect_create_screen(int screen, struct glx_display * priv)
 {
    struct glx_screen *psc;
 
-   psc = malloc(sizeof *psc);
+   psc = calloc(1, sizeof *psc);
    if (psc == NULL)
       return NULL;
 
-   memset(psc, 0, sizeof *psc);
    glx_screen_init(psc, screen, priv);
    psc->vtable = &indirect_screen_vtable;
 
-- 
1.7.8.6



More information about the mesa-dev mailing list