Wireshark-dev: [Wireshark-dev] ptvcursor : one step further?
From: Sebastien Tandel <[email protected]>
Date: Tue, 05 Dec 2006 22:05:10 +0100
I am seeing ptvcursor as an encapsulation allowing to forget about
proto_* structures/functions and helps to simplify the code generated
(in general). But for now, it is not possible to write all without
knowing any proto_* functions. I will present here a way to get rid of
some code and forget a little bit more of proto_*.

Tell me whether this idea is of any interest for you. If it is, I may do
the required changes and send the patch.

*** A technique used in NCP2222.inc when creating a subtree and using
ptvcursor :

  gint current_offset;
  current_offset = ptvcursor_current_offset(cursor);

  subtree = proto_item_add_subtree(it, ett_sub);
  ptvcursor_new(subtree, ptvcursor_tvb(cursor),  current_offset); /*
involves a malloc */
  ptvcursor(cursor); /* involves a free */

which could be summarised as :

  subtree = proto_item_add_subtree(it, ett_sub);
  ptvcursor_new(subtree, ptvcursor_tvb(cursor),
ptvcursor_current_offset(cursor)); /* involves a malloc */
  ptvcursor_free(cursor); /*"involves a free */

The idea behind ptvcursor : encapsulation of the tree, the tvb
structures and helps to get rid of the proto_FUNCs but here is a case in
which there is no more encapsulation. And we have to know proto_* API
again ...

*** Proposal :

** First step
the code hereabove is equivalent to (and even quicker because no
malloc/free involved!) the following :

  proto_tree * initial_tree = proto_tree(cursor);
  subtree = proto_item_add_subtree(it, ett_sub);
  ptvcursor_set_tree(cursor, subtree); => no malloc
  ptvcursor_set_tree(initial_tree); => no free

** Second Step
As we can notice, we already have proto_FUNCs and proto_tree appearing
... the encapsulation is not complete.

What is done hereabove when playing with subtrees is like acting as
push/pop actions => then add this feature in the ptvcursor API. It can
be done in the following way :
- add a field in the ptvcursor structure which retains the push/pop of
subtrees (fixed-length table, only one level of push/pop, malloc/free?)
- add two functions : ptvcursor_push_subtree(cursor, it, ett_sub),

*** Finally the code will then be only two functions and no more proto_*
involved :

ptvcursor_push_subtree(cursor, it, ett_sub);