[syslinux] [RFC] function to parse string to argc/argv pair

Gene Cumm gene.cumm at gmail.com
Fri Sep 17 20:25:19 PDT 2010


On Thu, Sep 16, 2010 at 12:38, Gene Cumm <gene.cumm at gmail.com> wrote:

At this time, the code works but I'm looking for advise on which
alternatives I should use to optimize this function, especially in the
context of Syslinux/COM32.

> int parse_args1(char ***iargv, const char *istr)

Two contemplations here.

1) Pass a length argument at which point processing will stop which
could eliminate the need for the length scan.
2) To reuse the incoming array, eliminating the need to allocate it in
the parser (malloc, calloc, etc.).

>    /* Scan 1: Length */

>    /* Scan 2: Copy, nullify and make argc */

Pre-allocate an array for the full length (needing a length by scan or
argument; requires only 1 call to free()) or allocate multiple arrays
(one per argument; still requires a length scan or a lot of recopying
of the array).

>    /* Scan 3: Build array of pointers */

Allocate the array of pointers after copying/nullifying the array or
take the conservative approach of assuming the maximum possible value
of argc ( (n+1)/2 ), which could allow the copying/nullifying and
filling of the pointers to be done at the same time (requiring only a
length scan and a copy scan).  Alternatively, pre-allocate a maximally
sized pointer array, fill the pointers while copying/nullifying then
allocate an optimally sized pointer array, copy active pointers and
free the large pointer array.

> void free_args1(char ***argv)
> {
>    char *s;
>    s = **argv;
>    free(*argv);
>    free(s);

The order here shouldn't matter as it's on the heap.  Reversing the
order would prevent the need to allocate a char * on the stack (so
small it shouldn't matter either).

-- 
-Gene




More information about the Syslinux mailing list