]> granicus.if.org Git - strace/commitdiff
Reduce memory usage while managing tcbs allocation.
authorNahim El Atmani <naam@lse.epita.fr>
Sat, 5 Mar 2016 12:35:57 +0000 (13:35 +0100)
committerDmitry V. Levin <ldv@altlinux.org>
Sat, 5 Mar 2016 17:02:05 +0000 (17:02 +0000)
* strace.c (init):  Remove initial memory allocation for tcbtab.
(expand_tcbtab): Do initial memory allocation when tcbtabsize == 0.

Signed-off-by: Nahim El Atmani <nahim+dev@naam.me>
Reviewed-By: Gabriel Laskar <gabriel@lse.epita.fr>
Reported-by: haris iqbal <haris.phnx@gmail.com>
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
strace.c

index 785cc605ffac3e3e703c82fed007940a7b3d4950..49d6f3d6ad65f9805017786f40b9103d59840fa2 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -691,19 +691,25 @@ newoutf(struct tcb *tcp)
 static void
 expand_tcbtab(void)
 {
-       /* Allocate some more TCBs and expand the table.
+       /* Allocate some (more) TCBs (and expand the table).
           We don't want to relocate the TCBs because our
           callers have pointers and it would be a pain.
           So tcbtab is a table of pointers.  Since we never
           free the TCBs, we allocate a single chunk of many.  */
-       unsigned int i = tcbtabsize;
-       struct tcb *newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
-       struct tcb **newtab = xreallocarray(tcbtab, tcbtabsize * 2,
-                                           sizeof(tcbtab[0]));
-       tcbtabsize *= 2;
-       tcbtab = newtab;
-       while (i < tcbtabsize)
-               tcbtab[i++] = newtcbs++;
+       unsigned int new_tcbtabsize, alloc_tcbtabsize;
+       struct tcb *newtcbs;
+
+       if (tcbtabsize) {
+               alloc_tcbtabsize = tcbtabsize;
+               new_tcbtabsize = tcbtabsize * 2;
+       } else {
+               new_tcbtabsize = alloc_tcbtabsize = 1;
+       }
+
+       newtcbs = xcalloc(alloc_tcbtabsize, sizeof(newtcbs[0]));
+       tcbtab = xreallocarray(tcbtab, new_tcbtabsize, sizeof(tcbtab[0]));
+       while (tcbtabsize < new_tcbtabsize)
+               tcbtab[tcbtabsize++] = newtcbs++;
 }
 
 static struct tcb *
@@ -1484,10 +1490,8 @@ get_os_release(void)
 static void ATTRIBUTE_NOINLINE
 init(int argc, char *argv[])
 {
-       struct tcb *tcp;
        int c, i;
        int optF = 0;
-       unsigned int tcbi;
        struct sigaction sa;
 
        progname = argv[0] ? argv[0] : "strace";
@@ -1503,13 +1507,6 @@ init(int argc, char *argv[])
 
        os_release = get_os_release();
 
-       /* Allocate the initial tcbtab.  */
-       tcbtabsize = argc;      /* Surely enough for all -p args.  */
-       tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0]));
-       tcp = xcalloc(tcbtabsize, sizeof(*tcp));
-       for (tcbi = 0; tcbi < tcbtabsize; tcbi++)
-               tcbtab[tcbi] = tcp++;
-
        shared_log = stderr;
        set_sortby(DEFAULT_SORTBY);
        set_personality(DEFAULT_PERSONALITY);