[PATCH v3] Reduce memory waste while managing tcbs

Dmitry V. Levin ldv at altlinux.org
Wed Mar 2 23:45:13 UTC 2016


On Tue, Mar 01, 2016 at 07:38:00PM +0100, Nahim El Atmani wrote:
> * strace.c: alloctcb() when no tcbs are available (first call and subsequent
> calls when tcbtab is full) call expand_tcbtab(). Now, expand_tcbtab() allocate
> the minimum required amount of memory to create the tcbtab if needed and expend
> otherwise instead of relying on the preallocation of a large amount of tcbs
> and expand if needed.

Now that the patch is almost ready, it's time to comment this commit
message.  The requirement is that after a summary and optional few lines
of text describing the change goes a ChangeLog-style entry (described in
detail e.g. at
https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html)

For example, a ChangeLog-style entry might be something like this:

* strace.c (expand_tcbtab): Do initial memory allocation when
tcbtabsize == 0.
(init): Remove initial memory allocation for tcbtab.

> -	unsigned int i = tcbtabsize;
> -	struct tcb *newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
> -	struct tcb **newtab = xreallocarray(tcbtab, tcbtabsize * 2,
> -					    sizeof(tcbtab[0]));
> -	tcbtabsize *= 2;
> +	unsigned int i, new_tcbtabsize, alloc_tcbtabsize;
> +	struct tcb *newtcbs;
> +	struct tcb **newtab;
> +
> +	if (tcbtabsize) {
> +		alloc_tcbtabsize = tcbtabsize;
> +		new_tcbtabsize = tcbtabsize * 2;
> +	} else {
> +		new_tcbtabsize = alloc_tcbtabsize = 1;
> +	}
> +
> +	i = tcbtabsize;
> +	newtcbs = xcalloc(alloc_tcbtabsize, sizeof(newtcbs[0]));
> +	newtab = xreallocarray(tcbtab, new_tcbtabsize, sizeof(tcbtab[0]));
> +	tcbtabsize = new_tcbtabsize;
>  	tcbtab = newtab;
>  	while (i < tcbtabsize)
>  		tcbtab[i++] = newtcbs++;

You can safely get rid of newtab, it is no longer needed after
commit v4.10-75-g3e9d71f.  Unfortunately, I missed the opportunity
to eliminate it along with that commit.

You can also write this code without using "i" if you like, e.g.

	newtcbs = xcalloc(alloc_tcbtabsize, sizeof(newtcbs[0]));
	tcbtab = xreallocarray(tcbtab, new_tcbtabsize, sizeof(tcbtab[0]));
	while (tcbtabsize < new_tcbtabsize)
		tcbtab[tcbtabsize++] = newtcbs++;


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160303/aef8dc3d/attachment.bin>


More information about the Strace-devel mailing list