On Sun, Jul 13, 2014 at 11:29:22AM -0600, dera...@cvs.openbsd.org wrote:
> This is the first pass of mallocarray() in sys/dev.  Please proofread.

[...]
> ===================================================================
> RCS file: /cvs/src/sys/dev/softraid.c,v
> retrieving revision 1.334
> diff -u -p -u -r1.334 softraid.c
> --- softraid.c        12 Jul 2014 18:48:51 -0000      1.334
> +++ softraid.c        13 Jul 2014 15:48:56 -0000
> @@ -252,7 +252,7 @@ sr_meta_attach(struct sr_discipline *sd,
>  
>       /* we have a valid list now create an array index */
>       cl = &sd->sd_vol.sv_chunk_list;
> -     sd->sd_vol.sv_chunks = malloc(sizeof(struct sr_chunk *) * chunk_no,
> +     sd->sd_vol.sv_chunks = mallocarray(chunk_no, sizeof(struct sr_chunk *),
>           M_DEVBUF, M_WAITOK | M_ZERO);
>  
>       /* fill out chunk array */
> @@ -1284,13 +1284,13 @@ sr_boot_assembly(struct sr_softc *sc)
>       }
>  
>       /* Allocate memory for device and ondisk version arrays. */
> -     devs = malloc(BIOC_CRMAXLEN * sizeof(dev_t), M_DEVBUF,
> +     devs = mallocarray(BIOC_CRMAXLEN, sizeof(dev_t), M_DEVBUF,
>           M_NOWAIT | M_CANFAIL);
>       if (devs == NULL) {
>               printf("%s: failed to allocate device array\n", DEVNAME(sc));
>               goto unwind;
>       }
> -     ondisk = malloc(BIOC_CRMAXLEN * sizeof(u_int64_t), M_DEVBUF,
> +     ondisk = mallocarray(BIOC_CRMAXLEN, sizeof(u_int64_t), M_DEVBUF,
>           M_NOWAIT | M_CANFAIL);
>       if (ondisk == NULL) {
>               printf("%s: failed to allocate ondisk array\n", DEVNAME(sc));
> @@ -1934,8 +1934,9 @@ sr_ccb_alloc(struct sr_discipline *sd)
>       if (sd->sd_ccb)
>               return (1);
>  
> -     sd->sd_ccb = malloc(sizeof(struct sr_ccb) *
> -         sd->sd_max_wu * sd->sd_max_ccb_per_wu, M_DEVBUF, M_WAITOK | M_ZERO);
> +     sd->sd_ccb = mallocarray(sd->sd_max_wu,
> +         sd->sd_max_ccb_per_wu * sizeof(struct sr_ccb),
> +         M_DEVBUF, M_WAITOK | M_ZERO);

I don't have the knowledge, but I'd hope sd->sd_max_ccb_per_wu
is smaller than sd->sd_max_wu?


>       TAILQ_INIT(&sd->sd_ccb_freeq);
>       for (i = 0; i < sd->sd_max_wu * sd->sd_max_ccb_per_wu; i++) {
>               ccb = &sd->sd_ccb[i];

[...]
> Index: ic/ciss.c
> ===================================================================
> RCS file: /cvs/src/sys/dev/ic/ciss.c,v
> retrieving revision 1.69
> diff -u -p -u -r1.69 ciss.c
> --- ic/ciss.c 12 Jul 2014 18:48:17 -0000      1.69
> +++ ic/ciss.c 13 Jul 2014 16:20:29 -0000
> @@ -347,7 +347,7 @@ ciss_attach(struct ciss_softc *sc)
>               return -1;
>       }
>  
> -     if (!(sc->sc_lds = malloc(sc->maxunits * sizeof(*sc->sc_lds),
> +     if (!(sc->sc_lds = mallocarray(sc->maxunits, sizeof(*sc->sc_lds),
>           M_DEVBUF, M_NOWAIT | M_ZERO))) {
>               bus_dmamem_free(sc->dmat, sc->cmdseg, 1);
>               bus_dmamap_destroy(sc->dmat, sc->cmdmap);
> @@ -385,7 +385,7 @@ ciss_attach(struct ciss_softc *sc)
>  
>       sc->sc_flags |= CISS_BIO;
>  #ifndef SMALL_KERNEL
> -     sc->sensors = malloc(sizeof(struct ksensor) * sc->maxunits,
> +     sc->sensors = mallocarray(sc->maxunits, sizeof(struct ksensor),
>           M_DEVBUF, M_NOWAIT | M_ZERO);
>       if (sc->sensors) {
>               struct device *dev;
> @@ -1311,7 +1311,7 @@ ciss_pdscan(struct ciss_softc *sc, int l
>       if (!k)
>               return NULL;
>  
> -     ldp = malloc(sizeof(*ldp) + (k-1), M_DEVBUF, M_NOWAIT);
> +     ldp = mallocarray(k-1, sizeof(*ldp), M_DEVBUF, M_NOWAIT);

Oops!
That's not a multiplication. It's an addition.
... or is/was the original code incorrect?


>       if (!ldp)
>               return NULL;
>  

[...]
> Index: ic/qla.c
> ===================================================================
> RCS file: /cvs/src/sys/dev/ic/qla.c,v
> retrieving revision 1.42
> diff -u -p -u -r1.42 qla.c
> --- ic/qla.c  12 Jul 2014 18:48:17 -0000      1.42
> +++ ic/qla.c  13 Jul 2014 16:02:02 -0000
> @@ -2547,7 +2547,7 @@ qla_alloc_ccbs(struct qla_softc *sc)
>       mtx_init(&sc->sc_port_mtx, IPL_BIO);
>       mtx_init(&sc->sc_mbox_mtx, IPL_BIO);
>  
> -     sc->sc_ccbs = malloc(sizeof(struct qla_ccb) * sc->sc_maxcmds,
> +     sc->sc_ccbs = mallocarray(sizeof(struct qla_ccb), sc->sc_maxcmds,
>           M_DEVBUF, M_WAITOK | M_CANFAIL | M_ZERO);

typically put count as first and size as second param?


>       if (sc->sc_ccbs == NULL) {
>               printf("%s: unable to allocate ccbs\n", DEVNAME(sc));
> Index: ic/qlw.c
> ===================================================================
> RCS file: /cvs/src/sys/dev/ic/qlw.c,v
> retrieving revision 1.23
> diff -u -p -u -r1.23 qlw.c
> --- ic/qlw.c  12 Jul 2014 18:48:17 -0000      1.23
> +++ ic/qlw.c  13 Jul 2014 16:01:53 -0000
> @@ -1699,7 +1699,7 @@ qlw_alloc_ccbs(struct qlw_softc *sc)
>       mtx_init(&sc->sc_ccb_mtx, IPL_BIO);
>       mtx_init(&sc->sc_queue_mtx, IPL_BIO);
>  
> -     sc->sc_ccbs = malloc(sizeof(struct qlw_ccb) * sc->sc_maxccbs,
> +     sc->sc_ccbs = mallocarray(sizeof(struct qlw_ccb), sc->sc_maxccbs,


param order of count and size?


>           M_DEVBUF, M_WAITOK | M_CANFAIL | M_ZERO);
>       if (sc->sc_ccbs == NULL) {
>               printf("%s: unable to allocate ccbs\n", DEVNAME(sc));

[...]
> Index: ic/siop.c
> ===================================================================
> RCS file: /cvs/src/sys/dev/ic/siop.c,v
> retrieving revision 1.67
> diff -u -p -u -r1.67 siop.c
> --- ic/siop.c 12 Jul 2014 18:48:17 -0000      1.67
> +++ ic/siop.c 13 Jul 2014 16:00:11 -0000
> @@ -1833,7 +1833,7 @@ siop_morecbd(sc)
>       }
>  
>       /* allocate cmd list */
> -     newcbd->cmds = malloc(sizeof(struct siop_cmd) * SIOP_NCMDPB,
> +     newcbd->cmds = mallocarray(sizeof(struct siop_cmd), SIOP_NCMDPB,
>           M_DEVBUF, M_NOWAIT | M_ZERO);

param order of count and size?


[...]
>       if (newcbd->cmds == NULL) {
>               printf("%s: can't allocate memory for command descriptors\n",
> Index: ic/vga.c
> ===================================================================
> RCS file: /cvs/src/sys/dev/ic/vga.c,v
> retrieving revision 1.62
> diff -u -p -u -r1.62 vga.c
> --- ic/vga.c  12 Jul 2014 18:48:17 -0000      1.62
> +++ ic/vga.c  13 Jul 2014 15:59:48 -0000
> @@ -663,9 +663,8 @@ vga_alloc_screen(void *v, const struct w
>                * XXX We could be more clever and use video RAM.
>                */
>               scr = LIST_FIRST(&vc->screens);
> -             scr->pcs.mem =
> -               malloc(scr->pcs.type->ncols * scr->pcs.type->nrows * 2,
> -                 M_DEVBUF, M_WAITOK);
> +             scr->pcs.mem = mallocarray(scr->pcs.type->ncols,
> +                 scr->pcs.type->nrows * 2, M_DEVBUF, M_WAITOK);

again, assuming that nrows < ncols?

>       }
>  
>       scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
> @@ -676,8 +675,8 @@ vga_alloc_screen(void *v, const struct w
>               vc->active = scr;
>               vc->currenttype = type;
>       } else {
> -             scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
> -                                   M_DEVBUF, M_WAITOK);
> +             scr->pcs.mem = mallocarray(type->ncols,
> +                 type->nrows * 2, M_DEVBUF, M_WAITOK);
>               pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
>       }
>  
> Index: ic/z8530tty.c
> ===================================================================
> RCS file: /cvs/src/sys/dev/ic/z8530tty.c,v
> retrieving revision 1.24
> diff -u -p -u -r1.24 z8530tty.c
> --- ic/z8530tty.c     21 Apr 2013 14:44:16 -0000      1.24
> +++ ic/z8530tty.c     13 Jul 2014 15:51:21 -0000
> @@ -342,7 +342,7 @@ zstty_attach(struct device *parent, stru
>       tp->t_hwiflow = zshwiflow;
>  
>       zst->zst_tty = tp;
> -     zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
> +     zst->zst_rbuf = mallocarray(zstty_rbuf_size, 2, M_DEVBUF, M_WAITOK);

param order of count and size?


--patrick

Reply via email to