Wireshark-dev: Re: [Wireshark-dev] RFD: The Future of Memory Management in Wireshark
From: Dirk Jagdmann <doj@xxxxxxxxx>
Date: Sat, 27 Oct 2012 11:17:18 -0700
> General thoughts from the list on whether or not this would be a good idea?

some general comments on the whole wmem idea:
memory allocation is done almost everywhere in Wireshark, also in somewhat
hidden places. For example all the proto_tree_add_* functions will need to
allocate memory. It will be a real pain to pass down the current allocator
object into each of those function invocations, you're looking at changing a
considerable amount of code. Also when thinking about writing dissector code,
it's probably not useful to mix different allocators while dissecting a protocol.

An alternative idea (although a little more ugly) is to use a thread local
global variable which contains pointers to the current ep and se allocators. The
se_* and ep_* functions retrieve the allocator object from that thread local
global variable and do their allocations. If a function wants to change the
allocators for its own function calls, it can make a copy of the global variable
pointers to local variables, set it's desired new allocators and restore the
global pointers at the end, like:

__thread void* se_allocator;
__thread void* ep_allocator;

void my_special_func()
{
  void* old_se = se_allocator;
  void* old_ep = ep_allocator;

  se_allocator = create_special_se();
  ep_allocator = create_special_ep();
  dissect_packet();
  garbage_collect(se_allocator);
  garbage_collect(ep_allocator);

  se_allocator = old_se;
  pe_allocator = old_ep;
}

Second point:

as you've noticed especially in dissector we pass large number of arguments into
almost every function (tvb, pinfo, tree). Bundling all of them into a struct
like dissect_context would make adding things like custom allocators way easier
to support, since we could add the required pointers to that context struct and
only pass that struct into all of the functions. However now you would need to
modify every line of code which was using the pointers for tvb, pinfo, tree and
in the dissectors that is a very large number of lines. Also it will be a
difficult decision how the API on the new allocation functions should look like?
Would we want to hand them the dissect_context struct or the allocator pointer?

struct dissect_context {
  tvbuff_t* tvb;
  packet_info* pinfo;
  void* se_allocator;
  void* ep_allocator;
};

void* se_alloc(dissect_context* ctx); // like this?
void* se_alloc(void* se_allocator); // or like this?

-- 
---> Dirk Jagdmann
----> http://cubic.org/~doj
-----> http://llg.cubic.org