Expand the macro

        #define sizeofN(type, n) (sizeof(type) * n)

whenever it occurs as an argument to `alloc' or `aresize' and replace
`alloc' by `allocarray' and `aresize' by `aresizearray'.

There remains one single call to sizeofN(a,b) (see the patch for exec.c)
which could safely be replaced by `a * b'.


Index: c_sh.c
===================================================================
RCS file: /cvs/src/bin/ksh/c_sh.c,v
retrieving revision 1.45
diff -u -p -r1.45 c_sh.c
--- c_sh.c      27 Aug 2014 08:26:04 -0000      1.45
+++ c_sh.c      23 May 2015 11:58:27 -0000
@@ -614,7 +614,8 @@ c_set(char **wp)
                while (*++wp != NULL)
                        *wp = str_save(*wp, &l->area);
                l->argc = wp - owp - 1;
-               l->argv = (char **) alloc(sizeofN(char *, l->argc+2), &l->area);
+               l->argv = (char **) allocarray(l->argc+2, sizeof(char *),
+                   &l->area);
                for (wp = l->argv; (*wp++ = *owp++) != NULL; )
                        ;
        }
Index: exec.c
===================================================================
RCS file: /cvs/src/bin/ksh/exec.c,v
retrieving revision 1.51
diff -u -p -r1.51 exec.c
--- exec.c      18 Apr 2015 18:28:36 -0000      1.51
+++ exec.c      23 May 2015 11:58:27 -0000
@@ -92,7 +92,8 @@ execute(struct op *volatile t,
        flags &= ~XTIME;
 
        if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
-               e->savefd = (short *) alloc(sizeofN(short, NUFILE), ATEMP);
+               e->savefd = (short *) allocarray(NUFILE, sizeof(short),
+                   ATEMP);
                /* initialize to not redirected */
                memset(e->savefd, 0, sizeofN(short, NUFILE));
        }
Index: expand.h
===================================================================
RCS file: /cvs/src/bin/ksh/expand.h,v
retrieving revision 1.6
diff -u -p -r1.6 expand.h
--- expand.h    30 Mar 2005 17:16:37 -0000      1.6
+++ expand.h    23 May 2015 11:58:27 -0000
@@ -82,7 +82,7 @@ typedef struct XPtrV {
 
 #define        XPinit(x, n) do { \
                        void **vp__; \
-                       vp__ = (void**) alloc(sizeofN(void*, n), ATEMP); \
+                       vp__ = (void**) allocarray(n, sizeof(void*), ATEMP); \
                        (x).cur = (x).beg = vp__; \
                        (x).end = vp__ + n; \
                } while (0)
@@ -90,8 +90,9 @@ typedef struct XPtrV {
 #define        XPput(x, p) do { \
                        if ((x).cur >= (x).end) { \
                                int n = XPsize(x); \
-                               (x).beg = (void**) aresize((void*) (x).beg, \
-                                                  sizeofN(void*, n*2), ATEMP); 
\
+                               (x).beg = (void**) \
+                                   aresizearray((void*) (x).beg, \
+                                   n, 2*sizeof(void*), ATEMP); \
                                (x).cur = (x).beg + n; \
                                (x).end = (x).cur + n; \
                        } \
@@ -101,7 +102,7 @@ typedef struct XPtrV {
 #define        XPptrv(x)       ((x).beg)
 #define        XPsize(x)       ((x).cur - (x).beg)
 
-#define        XPclose(x)      (void**) aresize((void*)(x).beg, \
-                                        sizeofN(void*, XPsize(x)), ATEMP)
+#define        XPclose(x)      (void**) aresizearray((void*)(x).beg, \
+                                        XPsize(x), sizeof(void*), ATEMP)
 
 #define        XPfree(x)       afree((void*) (x).beg, ATEMP)
Index: syn.c
===================================================================
RCS file: /cvs/src/bin/ksh/syn.c,v
retrieving revision 1.29
diff -u -p -r1.29 syn.c
--- syn.c       3 Jun 2013 18:40:05 -0000       1.29
+++ syn.c       23 May 2015 11:58:27 -0000
@@ -197,7 +197,7 @@ get_command(int cf)
        XPtrV args, vars;
        struct nesting_state old_nesting;
 
-       iops = (struct ioword **) alloc(sizeofN(struct ioword *, NUFILE+1),
+       iops = (struct ioword **) allocarray(NUFILE+1, sizeof(struct ioword *),
            ATEMP);
        XPinit(args, 16);
        XPinit(vars, 16);
@@ -389,8 +389,8 @@ get_command(int cf)
                t->ioact = NULL;
        } else {
                iops[iopn++] = NULL;
-               iops = (struct ioword **) aresize((void*) iops,
-                   sizeofN(struct ioword *, iopn), ATEMP);
+               iops = (struct ioword **) aresizearray((void*) iops,
+                   iopn, sizeof(struct ioword *), ATEMP);
                t->ioact = iops;
        }
 
@@ -565,8 +565,8 @@ function_body(char *name,
                 * be used as input), we pretend there is a colon here.
                 */
                t->left = newtp(TCOM);
-               t->left->args = (char **) alloc(sizeof(char *) * 2, ATEMP);
-               t->left->args[0] = alloc(sizeof(char) * 3, ATEMP);
+               t->left->args = (char **) allocarray(2, sizeof(char *), ATEMP);
+               t->left->args[0] = allocarray(3, sizeof(char), ATEMP);
                t->left->args[0][0] = CHAR;
                t->left->args[0][1] = ':';
                t->left->args[0][2] = EOS;
Index: table.c
===================================================================
RCS file: /cvs/src/bin/ksh/table.c,v
retrieving revision 1.15
diff -u -p -r1.15 table.c
--- table.c     19 Feb 2012 07:52:30 -0000      1.15
+++ table.c     23 May 2015 11:58:27 -0000
@@ -40,7 +40,8 @@ texpand(struct table *tp, int nsize)
        struct tbl **ntblp, **otblp = tp->tbls;
        int osize = tp->size;
 
-       ntblp = (struct tbl**) alloc(sizeofN(struct tbl *, nsize), tp->areap);
+       ntblp = (struct tbl**) allocarray(nsize, sizeof(struct tbl *),
+           tp->areap);
        for (i = 0; i < nsize; i++)
                ntblp[i] = NULL;
        tp->size = nsize;
@@ -170,7 +171,7 @@ ktsort(struct table *tp)
        int i;
        struct tbl **p, **sp, **dp;
 
-       p = (struct tbl **)alloc(sizeofN(struct tbl *, tp->size+1), ATEMP);
+       p = (struct tbl **) allocarray(tp->size+1, sizeof(struct tbl *), ATEMP);
        sp = tp->tbls;          /* source */
        dp = p;                 /* dest */
        for (i = 0; i < tp->size; i++)

Reply via email to