From: Florian Weimer <fw@deneb.enyo.de>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] vfprintf: Allocate the user argument buffer on the heap
Date: Tue, 27 Jun 2017 19:13:00 -0000 [thread overview]
Message-ID: <87efu5nt0i.fsf@mid.deneb.enyo.de> (raw)
In-Reply-To: <2be1d31c-cd05-71fe-5ab6-11e5371c1c09@linaro.org> (Adhemerval Zanella's message of "Tue, 27 Jun 2017 15:45:50 -0300")
* Adhemerval Zanella:
>> - args_value[cnt].pa_user = alloca (args_size[cnt]);
>> + /* Allocate from user_args_buffer. */
>> + size_t allocation_size = args_size[cnt];
>> + void *allocation;
>> + if (allocation_size == 0)
>> + /* Nothing to allocate. */
>> + allocation = NULL;
>> + else
>> + {
>> + if (user_args_buffer == NULL)
>> + {
>> + /* First user argument. Allocate the complete
>> + buffer. */
>> + user_args_buffer = allocate_user_args_buffer
>> + (nargs, args_size, args_type);
>> + if (user_args_buffer == NULL)
>> + {
>> + done = -1;
>> + goto all_done;
>> + }
>> + user_args_buffer_next = user_args_buffer;
>> + }
>> + allocation = user_args_buffer_next;
>> + user_args_buffer_next
>> + += roundup (allocation_size, _Alignof (max_align_t));
>> + }
>> + /* Install the allocated pointer and use the callback to
>> + extract the argument. */
>> + args_value[cnt].pa_user = allocation;
>> (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
>> (args_value[cnt].pa_user, ap_savep);
>
> I am trying to convince myself it is worth to add all this complexity
> to allocate for user defined types, but I am failing to understand why
> can we just simplify it to a malloc using 'args_size[cnt]' (as the alloca
> is already using it). And why do we need to keep track of the buffer
> allocation after the callback track? Could we just free it after the
> call?
We need to delay the deallocation until the string has been formatted
because the data is later passed to the formatting function.
We could use separate malloc allocations and a second pass through the
argument array to free the user allocations (if any). This might be
simpler, but I would have to write it down to be certain.
next prev parent reply other threads:[~2017-06-27 19:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-19 16:20 Florian Weimer
2017-06-27 18:45 ` Adhemerval Zanella
2017-06-27 19:13 ` Florian Weimer [this message]
2017-06-27 20:04 ` Adhemerval Zanella
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87efu5nt0i.fsf@mid.deneb.enyo.de \
--to=fw@deneb.enyo.de \
--cc=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).