[PATCH v2 2/9] loop:refactor: extract subfunction get_next_free_loop()

Xiaoming Ni nixiaoming at huawei.com
Mon Nov 21 13:48:45 UTC 2022


On 2022/11/21 15:50, Kang-Che Sung wrote:
> On Mon, Nov 21, 2022 at 9:19 AM Xiaoming Ni <nixiaoming at huawei.com> wrote:
>> static int get_next_free_loop(char *dev, size_t dev_size, int id)
>> {
>>           int loopdevno = get_free_loop();
>>           if (loopdevno >= 0) {
>>                   snprintf(dev, dev_size, LOOP_FORMAT, loopdevno);
>>                   return 1; /* use /dev/loop-control */
>>           }
>>           if (loopdevno == -2) {
>>                   snprintf(dev, dev_size, LOOP_FORMAT, id);
>>                   return 2;
>>           }
>>           return -1; /* no free loop devices */
>> }
>>
>> If the dev_size parameter is added to get_next_free_loop(), the code
>> size increases, Is it worth?
>>
>> function                                             old     new   delta
>> set_loop                                             734     744     +10
>> ------------------------------------------------------------------------------
>> (add/remove: 0/0 grow/shrink: 1/0 up/down: 10/0)               Total: 10
>> bytes
> 
> No, that isn't what I mean. sprintf() is faster than snprintf() when
> we are sure the string buffer would never overflow.
> Just keep using sprintf() here but add a statement before it:
> `assert(dev_size >= LOOP_NAMESIZE);`
> .
> 

thanks
  I'll add const modifiers in the next version.

Thanks




More information about the busybox mailing list