[Spice-devel] [PATCH v2 13/43] Add new_ett function to be able to create new trees

Frediano Ziglio fziglio at redhat.com
Wed Jul 8 06:53:46 PDT 2015


Use an array to declare tree items instead of allocating it statically.
This save a bit of memory and it also generate smaller code to read.
A tree in wireshark represent an item which could be expanded.
Possibly wireshark is using these registrations to save expansion
state in the user interface.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 codegen/dissector_test.c    | 13 +++++++++++++
 python_modules/dissector.py | 28 ++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/codegen/dissector_test.c b/codegen/dissector_test.c
index 6189da0..3212655 100644
--- a/codegen/dissector_test.c
+++ b/codegen/dissector_test.c
@@ -13,9 +13,11 @@
 enum {
 	first_hf_registered = 0x10000,
 	first_ei_registered = 0x20000,
+	first_tree_registered = 0x30000,
 };
 static int last_hf_registered = first_hf_registered - 1;
 static int last_ei_registered = first_ei_registered - 1;
+static int last_tree_registered = first_tree_registered - 1;
 
 WS_DLL_PUBLIC void
 proto_register_field_array(const int parent, hf_register_info *hf, const int num_records)
@@ -31,6 +33,17 @@ proto_register_field_array(const int parent, hf_register_info *hf, const int num
 }
 
 WS_DLL_PUBLIC void
+proto_register_subtree_array(gint *const *indices, const int num_indices)
+{
+	int i;
+	assert(num_indices >= 0);
+	for (i = 0; i < num_indices; ++i) {
+		assert(indices && *indices[i] == -1);
+		*indices[i] = ++last_tree_registered;
+	}
+}
+
+WS_DLL_PUBLIC void
 expert_register_field_array(expert_module_t* module, ei_register_info *ei, const int num_records)
 {
 	int i;
diff --git a/python_modules/dissector.py b/python_modules/dissector.py
index 45f8737..52234fc 100644
--- a/python_modules/dissector.py
+++ b/python_modules/dissector.py
@@ -2,6 +2,22 @@
 from . import codegen
 import re
 
+
+# generate a new tree identifier
+ett_writer = None
+ett_num = 0
+def new_ett(writer):
+    global ett_writer
+    global ett_num
+
+    if ett_num and ett_num % 16 == 0:
+        ett_writer.newline()
+    name = 'etts[%u]' % ett_num
+    ett_num = ett_num + 1
+    ett_writer.write('-1, ')
+    return name
+
+
 hf_writer = None
 hf_defs = None
 
@@ -54,6 +70,10 @@ def write_protocol_definitions(writer):
     writer.newline()
     writer.function('spice_register_fields', 'void', 'int proto, expert_module_t* expert_proto')
 
+    writer.variable_def('guint', 'n');
+    writer.variable_def('gint *', 'ett[array_length(etts)]')
+    writer.newline()
+
     writer.write("static hf_register_info hf[] = ")
     writer.begin_block()
     hf_defs = writer.get_subwriter()
@@ -66,17 +86,25 @@ def write_protocol_definitions(writer):
     writer.end_block(semicolon = True)
     writer.newline()
 
+    with writer.for_loop('n', 'array_length(etts)'):
+        writer.assign('ett[n]', '&etts[n]')
+
     writer.statement('proto_register_field_array(proto, hf, array_length(hf))')
+    writer.statement('proto_register_subtree_array(ett, array_length(etts))')
     writer.statement('expert_register_field_array(expert_proto, ei, array_length(ei))')
     writer.end_block()
 
 
 def write_protocol_parser(writer, proto):
     global hf_writer
+    global ett_writer
 
     write_parser_helpers(writer)
 
     # put fields declaration first
+    with writer.block('static gint etts[] =', semicolon=True) as scope:
+        ett_writer = scope
+        writer.newline()
     hf_writer = writer.get_subwriter()
 
     # put fields definition at last
-- 
2.1.0



More information about the Spice-devel mailing list