[prev in list] [next in list] [prev in thread] [next in thread]
List: strace
Subject: Re: [PATCH] Avoid some memory waste while allocating tcbtab.
From: "Dmitry V. Levin" <ldv () altlinux ! org>
Date: 2016-02-29 23:27:09
Message-ID: 20160229232709.GA25777 () altlinux ! org
[Download RAW message or body]
[Attachment #2 (multipart/signed)]
On Wed, Feb 24, 2016 at 02:22:34PM +0100, Nahim El Atmani wrote:
[...]
> --- a/strace.c
> +++ b/strace.c
> @@ -689,6 +689,18 @@ newoutf(struct tcb *tcp)
> }
>
> static void
> +create_tcbtab(void)
> +{
> + struct tcb *tcp;
> +
> + /* Allocate the initial tcbtab. */
> + tcbtabsize = 1; /* We don't need much more at first */
> + tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0]));
> + tcp = xcalloc(tcbtabsize, sizeof(*tcp));
> + tcbtab[0] = tcp;
> +}
> +
> +static void
> expand_tcbtab(void)
> {
> /* Allocate some more TCBs and expand the table.
> @@ -696,14 +708,22 @@ expand_tcbtab(void)
> 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 i;
> + struct tcb *newtcbs;
> + struct tcb **newtab;
> +
> + if (!tcbtabsize) {
> + create_tcbtab();
> + } else {
> + i = tcbtabsize;
> + newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
> + newtab = xreallocarray(tcbtab, tcbtabsize * 2,
> + sizeof(tcbtab[0]));
> + tcbtabsize *= 2;
> + tcbtab = newtab;
> + while (i < tcbtabsize)
> + tcbtab[i++] = newtcbs++;
> + }
> }
This is better. Do you think create_tcbtab that's called just once
from expand_tcbtab worth to be a separate function?
What if these two branches of code were merged, e.g.
unsigned int new_tcbtabsize, alloc_tcbtabsize;
if (tcbtabsize) {
alloc_tcbtabsize = tcbtabsize;
new_tcbtabsize = tcbtabsize << 1;
} else {
alloc_tcbtabsize = new_tcbtabsize = 1;
}
etc.
Wouldn't the result code be more compact and somewhat more readable?
--
ldv
[Attachment #5 (application/pgp-signature)]
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Strace-devel mailing list
Strace-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/strace-devel
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic