This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] vfprintf: Allocate the user argument buffer on the heap
On 27/06/2017 16:13, Florian Weimer wrote:
> * 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.
Ack.
>
> 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.
If you could simplify it as cost of a slight worse performance/memory
utilization for this specific code path (user provided hooks) I think
it would be better. This code is already somewhat complex and convoluted.