From 2c9fb391c11490ef782bb689981a0921018240c9 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Thu, 11 May 2023 14:21:29 +0200 Subject: [PATCH] libocispec: write _present tags together by grouping the _present tags together in the generated C header, the resulting C structure can be packed more efficiently, reducing memory usage. Signed-off-by: Giuseppe Scrivano --- src/ocispec/headers.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ocispec/headers.py b/src/ocispec/headers.py index 022705cf..606903d9 100755 --- a/src/ocispec/headers.py +++ b/src/ocispec/headers.py @@ -157,15 +157,20 @@ def append_type_c_header(obj, header, prefix): header.write("typedef struct {\n") if obj.children is None: header.write(" char unuseful; // unuseful definition to avoid empty struct\n") + present_tags = [] for i in obj.children or []: + if helpers.judge_data_type(i.typ) or i.typ == 'boolean': + present_tags.append(" unsigned int %s_present : 1;\n" % (i.fixname)) if i.typ == 'array': append_header_child_arr(i, header, prefix) else: append_header_child_others(i, header, prefix) - if helpers.judge_data_type(i.typ) or i.typ == 'boolean': - header.write(" unsigned int %s_present : 1;\n" % (i.fixname)) if obj.children is not None: header.write(" yajl_val _residual;\n") + if len(present_tags) > 0: + header.write("\n") + for tag in present_tags: + header.write(tag) typename = helpers.get_prefixed_name(obj.name, prefix) header.write("}\n%s;\n\n" % typename) header.write("void free_%s (%s *ptr);\n\n" % (typename, typename))