<div dir="ltr">Thanks for all good explanation.<br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 9, 2013 at 11:24 PM, Sebastian Huber <span dir="ltr"><<a href="mailto:sebastian.huber@embedded-brains.de" target="_blank">sebastian.huber@embedded-brains.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="im">On 07/09/2013 05:29 AM, Ashi wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="im">
Hi, Sebastian, thanks for your review!<br>
<br>
在 2013-7-7 下午9:49,"Sebastian Huber" <<a href="mailto:sebastian.huber@embedded-brains.de" target="_blank">sebastian.huber@embedded-<u></u>brains.de</a><br></div>
<mailto:<a href="mailto:sebastian.huber@embedded-brains.de" target="_blank">sebastian.huber@<u></u>embedded-brains.de</a>>>写道:<div class="im"><br>
><br>
> Hello Ashi,<br>
><br>
><br>
> On 06/07/13 09:17, Ashi wrote:<br>
>><br>
>> Hi all,<br>
>><br>
>> this patch adds a data structure called freelist to score, there are no<br>
>> test cases yet and should be added later.<br>
><br>
><br>
> I would appreciate to have the test for this new stuff included in the patch.<br>
<br>
<br>
sure, I will update the patch with test cases.<br>
><br>
><br>
>><br>
>> Freelist is a structure, which contains a chain of nodes. It supports 2<br>
>> operation:<br>
>> - get node from freelist<br>
>> - put node to freelist.<br>
>> And when there is no enough node in freelist, it will automatically<br>
>> increase itself's size by allocate more nodes from heap or workspace(which<br>
>> is specified by user).<br>
><br>
><br>
> What can I do if I like to get the nodes from a magic space?<br>
<br>
<br>
sorry for the unclear, you can get nodes from freelist by 'get' operation. And<br>
if you mean get nodes from heap or workspace, it's done by<br>
_Freelist_Get_node(), which calls _Freelist_Bump() when there is no free node left.<br>
</div></blockquote>
<br>
Yes, the problem is that you limit your Freelist to the heap and workspace. If you use a handler function (or virtual method if you like) then you can avoid this limitation.<br>
<br>
[...]<div class="im"><br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>> +/**<br>
>> + * @typedef freelist_callout<br>
>> + */<br>
>> +typedef void (*freelist_callout)(<br>
>> + Freelist_Control *fc,<br>
>> + void *nodes<br>
>> +);<br>
>> +<br>
>> +/**<br>
>> + * @typedef Freelist_Control_struct<br>
>> + *<br>
>> + * This is used to manage each element.<br>
>> + */<br>
>> +struct Freelist_Control_struct {<br>
>> + Chain_Control Freelist;<br>
>> + size_t free_count;<br>
><br>
><br>
> Why do we need the free_count?<br>
<br>
free_count is used to keep track how many nodes there is in freelist. And if<br>
free_count is 0 when you try to get node from freelist by call<br>
_Freelist_Get_node(), _Freelist_Get_node() will call _Freelist_Bump() to<br>
allocate more node from heap or workspace.<br>
</blockquote>
<br></div>
The list knows if it is empty. There is not need to store this information in two ways.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
><br>
>> + size_t bump_count;<br>
>> + size_t node_size;<br>
><br>
><br>
>> + freelist_callout callout;<br>
>> + bool use_workspace;<br>
>> +};<br>
><br>
><br>
> I would replace this with an extend handler.<br>
><br>
> /**<br>
> * @brief Extends the freelist.<br>
> *<br>
> * @param[in] freelist The freelist control.<br>
> *<br>
> * @return The count of new nodes.<br>
> */<br>
> typedef size_t ( *Freelist_Extend )( Freelist_Control *freelist );<br>
><br>
> This is much more flexible since you only specify the interface and don't<br>
limit this to heap/workspace.<br>
><br>
> You can provide a _Freelist_Extend_with_nothing(<u></u>) which simply returns 0.<br>
<br>
Yeah, this Freelist_Extend is very flexible, but I feel the Freelist_Extend is<br>
a little complex. As it shows in _Freelist_Bump(), if users provides their own<br>
extension function, they have to append there new nodes to freelist's internal<br>
chain and call their callout function on new nodes. And since<br>
_Freelist_Initialize() also would call Freelist_Extend(), if we provided<br>
_Freelist_Extend_with_nothing(<u></u>), the initialization may fail.<br>
</blockquote>
<br></div>
Since the Freelist_Extend gets the Freelist as a first argument it can set the extend handler to _Freelist_Extend_with_nothing(<u></u>) after the first invocation.<br>
<br>
Example:<div class="im"><br>
<br>
/**<br>
* @brief Extends the freelist.<br>
*<br>
* @param[in] freelist The freelist control.<br></div>
*/<br>
typedef void ( *Freelist_Extend )( Freelist_Control *freelist );<br>
<br>
typedef struct {<br>
Objects_Control obj;<br>
int x;<br>
} my_obj;<br>
<br>
void my_extend( Freelist_Control *freelist )<br>
{<br>
size_t bump_count = freelist->bump_count;<br>
size_t size = bump_count * sizeof(my_obj);<br>
my_obj *objs = malloc(size);<br>
<br>
_Freelist_Set_extend_handler( freelist, _Freelist_Extend_with_nothing );<br>
_Chain_Initialize(<br>
_Freelist_Get_list( freelist ),<br>
objs,<br>
bump_count,<br>
size<br>
);<div class=""><div class="h5"><br>
}<br></div></div></blockquote><div>I'm a little confused by my_extend() function, is it only called after calling _Freelist_Initialize() by user? <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class=""><div class="h5">
<br>
[...]<br>
<br>
-- <br>
Sebastian Huber, embedded brains GmbH<br>
<br>
Address : Dornierstr. 4, D-82178 Puchheim, Germany<br>
Phone : +49 89 189 47 41-16<br>
Fax : +49 89 189 47 41-09<br>
E-Mail : <a href="mailto:sebastian.huber@embedded-brains.de" target="_blank">sebastian.huber@embedded-<u></u>brains.de</a><br>
PGP : Public key available on request.<br>
<br>
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.<br></div></div></blockquote><div><br>Cheers,<br>Zhongwei <br></div></div><br></div></div>