On Wed, May 06, 2015 at 04:45:14PM +0000, Jie Liu <[email protected]> wrote:
> When I'm reading the code of libev, I find this definition of 
> "array_needsize":
> #define array_needsize(type,base,cur,cnt,init)            \
>   if (expect_false ((cnt) > (cur)))                \
>     {                                \
>       int ecb_unused ocur_ = (cur);                    \
>       (base) = (type *)array_realloc                \
>          (sizeof (type), (base), &(cur), (cnt));        \
>       init ((base) + (ocur_), (cur) - ocur_);            \
>     }
> According to the comments, memories are allocated in chunks of 
> "MALLOC_ROUND". It is possible that the size needed is larger than 
> "MALLOC_ROUND". In this case, the "array_needsize" may not allocate enough 
> memories because it is using "if" instead of "while". I though there would be 
> a "while" loop but I didn't find it. The library is proved by many projects, 
> so I must miss something here. Could anyone point out how it works?

You missed that the macro does not call malloc but array_realloc, which
contains a while (via array_nextsize).

The if in array_needsize exists merely to decide whether an allocation
is needed at all, and MALLOC_ROUND is used to round allocations up to a
multiple of this size.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      [email protected]
      -=====/_/_//_/\_,_/ /_/\_\

_______________________________________________
libev mailing list
[email protected]
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev

Reply via email to