On 28 January 2015 at 12:55, Savolainen, Petri (NSN - FI/Espoo)
<[email protected]> wrote:
>
>
>> -----Original Message-----
>> From: ext Ola Liljedahl [mailto:[email protected]]
>> Sent: Wednesday, January 28, 2015 1:29 PM
>> To: Savolainen, Petri (NSN - FI/Espoo)
>> Cc: LNG ODP Mailman List
>> Subject: Re: [lng-odp] odp_cpumask.h
>>
>> On 28 January 2015 at 12:21, Savolainen, Petri (NSN - FI/Espoo)
>> <[email protected]> wrote:
>> >
>> >
>> >> -----Original Message-----
>> >> From: [email protected] [mailto:lng-odp-
>> >> [email protected]] On Behalf Of ext Ola Liljedahl
>> >> Sent: Wednesday, January 28, 2015 12:48 PM
>> >> To: LNG ODP Mailman List
>> >> Subject: [lng-odp] odp_cpumask.h
>> >>
>> >>  * @param str    Output buffer
>> >>  * @param len    Size of string length (incl. ending zero)
>> >>  */
>> >> void odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int len);
>> >>
>> >> Why don't we use size_t for the"len" parameter?
>> >>
>> >> -- Ola
>> >
>> >
>> > It's an object size, so it can be changed to size_t (similar to e.g.
>> snprintf()).
>> >
>> > Packet data len/offsets, buffer sizes, packet/bytes counts etc, should
>> be uintxx_t - but object sizes should be size_t.
>> >
>> >
>> > Answer to: What happens if len < output length? Maybe we copy the
>> snprintf definition (except output also ending zero). Return number chars
>> written (incl. str ending zero), otherwise return number of chars that
>> would have been outputted (return >len and clip output).
>> This is more complicated to check for and goes against the convention
>> of returning <0 for errors.
>> I would prefer my suggestion with returning -(minimum required buffer
>> size), there's a clear separation between success and error.
>>
>> And that we add a define with a suggested buffer size which will be
>> usable when the CPU mask is full (all bits set), this seems to be 128
>> when using Linux cpu_set_t for the implementation. Of course ODP could
>> use a smaller max size as well.
>
>
> About error from functions that return len. I'd have input param and return 
> type the same, so that compiler don't complain when comparing those (maybe 
> your audio was breaking on call yesterday on that part).
I have tested mixing size_t and ssize_t and neither gcc nor clang
complained. I was using -Wall -ansi -pedantic.
Thus we can specify the buffer size is size_t and the return type
ssize_t and when comparing the return value with a size_t variable or
value there won't be any warning.

size_t len = 10;//Start with a small value so that a retry will be forced
char *str:

for (;;) {
    str = malloc(len);
    if (str == NULL)
        perror("malloc"), exit(EXIT_FAILURE);

    ssize_t out = odp_cpumask_to_str(mask, str, len);
    if (out > 0) //success
        break;

    //failure, try with the recommended buffer size
    free(str);
    len = -out;
}
//do something with str
free(str);

>
> e.g.
>
> size_t odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, size_t len);
>
>
> size_t len, out;
> char str[SIZE];
>
> len = sizeof(str);
>
> out = odp_cpumask_to_str(mask, str, len);
>
> if (out > len) {
>     printf("output needs %z chars", out);
>    ABORT();
> }
>
>
> -Petri
>
>

_______________________________________________
lng-odp mailing list
[email protected]
http://lists.linaro.org/mailman/listinfo/lng-odp

Reply via email to