On Fri, Jul 8, 2022 at 2:34 PM Péter Ujfalusi peter.ujfalusi@linux.intel.com wrote:
On 08/07/2022 15:30, Andy Shevchenko wrote:
On Fri, Jul 08, 2022 at 02:13:14PM +0200, Cezary Rojewski wrote:
...
It seems you are missing the (1). The code has checks for the case where you can do get number upfront, it would just require two passes, but it's nothing in comparison of heave realloc().
unsigned int *tokens; char *p; int num;
p = get_options(str, 0, &num); if (num == 0) // No numbers in the string!
tokens = kcalloc(num + 1, sizeof(*tokens), GFP_KERNEL); if (!tokens) return -ENOMEM;
p = get_oprions(str, num, &tokens); if (*p) // String was parsed only partially! // assuming it's not a fatal error
return tokens;
This diff is tested and works:
Thanks, Peter!
But at least you can memove() to avoid second allocation. ideally to refactor that the result of get_options is consumed as is (it may be casted to struct tokens { int n; u32 v[]; })
...
Could be made nicer with some brain work put to it, we need strict u32 within the IPC message for the array...
True, it needs to be thought through. But I guess you got the idea of how to use existing library routines.