diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2015-01-11 21:18:24 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2015-01-11 21:25:26 +0100 |
commit | 010286ba2b513a0d279cf2e08aa68987eca7ed23 (patch) | |
tree | 59045dfb60d73e9a9e4019902dff14c502f89531 /build-aux | |
parent | f96f36ed5013ecf4d60fb4ed626102ab153fbf1f (diff) | |
download | external_libqmi-010286ba2b513a0d279cf2e08aa68987eca7ed23.zip external_libqmi-010286ba2b513a0d279cf2e08aa68987eca7ed23.tar.gz external_libqmi-010286ba2b513a0d279cf2e08aa68987eca7ed23.tar.bz2 |
qmi-codegen: avoid breaking API when defining strings in public structs
Commit b9c3701e337198 introduced an API break, where we would change a pointer
to a heap allocated string and instead use a fixed size char array. This commit
will instead recover the pointer to the string when used in a public struct, so
that API isn't broken w.r.t. previous stable libqmi versions.
The string is now properly allocated before reading and deallocated as part of
the struct deallocation.
Diffstat (limited to 'build-aux')
-rw-r--r-- | build-aux/qmi-codegen/Variable.py | 12 | ||||
-rw-r--r-- | build-aux/qmi-codegen/VariableString.py | 44 | ||||
-rw-r--r-- | build-aux/qmi-codegen/VariableStruct.py | 3 |
3 files changed, 53 insertions, 6 deletions
diff --git a/build-aux/qmi-codegen/Variable.py b/build-aux/qmi-codegen/Variable.py index d3691a3..6b534d7 100644 --- a/build-aux/qmi-codegen/Variable.py +++ b/build-aux/qmi-codegen/Variable.py @@ -16,6 +16,7 @@ # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # Copyright (C) 2012 Lanedo GmbH +# Copyright (C) 2012-2015 Aleksander Morgado <aleksander@aleksander.es> # import string @@ -54,6 +55,11 @@ class Variable: else: raise ValueError("Invalid endian value %s" % endian) + """ + Initially all variables are flagged as not being public + """ + self.public = False + """ Emits the code to declare specific new types required by the variable. """ @@ -150,3 +156,9 @@ class Variable: """ def add_sections(self, sections): pass + + """ + Flag as being public + """ + def flag_public(self): + self.public = True diff --git a/build-aux/qmi-codegen/VariableString.py b/build-aux/qmi-codegen/VariableString.py index b58a7d2..01452f5 100644 --- a/build-aux/qmi-codegen/VariableString.py +++ b/build-aux/qmi-codegen/VariableString.py @@ -16,6 +16,7 @@ # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # Copyright (C) 2012 Lanedo GmbH +# Copyright (C) 2012-2015 Aleksander Morgado <aleksander@aleksander.es> # import string @@ -83,10 +84,24 @@ class VariableString(Variable): if self.is_fixed_size: translations['fixed_size'] = self.fixed_size - template = ( - '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error}))\n' - '${lp} goto ${tlv_out};\n' - '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n') + + # Fixed sized strings exposed in public fields will need to be + # explicitly allocated in heap + if self.public: + translations['fixed_size_plus_one'] = int(self.fixed_size) + 1 + template = ( + '${lp}${variable_name} = g_malloc (${fixed_size_plus_one});\n' + '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error})) {\n' + '${lp} g_free (${variable_name});\n' + '${lp} ${variable_name} = NULL;\n' + '${lp} goto ${tlv_out};\n' + '${lp}}\n' + '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n') + else: + template = ( + '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error}))\n' + '${lp} goto ${tlv_out};\n' + '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n') else: translations['n_size_prefix_bytes'] = self.n_size_prefix_bytes translations['max_size'] = self.max_size if self.max_size != '' else '0' @@ -158,7 +173,13 @@ class VariableString(Variable): translations = { 'lp' : line_prefix, 'name' : variable_name } - if self.is_fixed_size: + if public: + # Fixed sized strings given in public structs are given as pointers, + # instead of as fixed-sized arrays directly in the struct. + template = ( + '${lp}gchar *${name};\n') + self.is_public = True + elif self.is_fixed_size: translations['fixed_size_plus_one'] = int(self.fixed_size) + 1 template = ( '${lp}gchar ${name}[${fixed_size_plus_one}];\n') @@ -309,7 +330,7 @@ class VariableString(Variable): """ def build_dispose(self, line_prefix, variable_name): # Fixed-size strings don't need dispose - if self.is_fixed_size: + if self.is_fixed_size and not self.public: return '' translations = { 'lp' : line_prefix, @@ -318,3 +339,14 @@ class VariableString(Variable): template = ( '${lp}g_free (${variable_name});\n') return string.Template(template).substitute(translations) + + + """ + Flag as being public + """ + def flag_public(self): + # Call the parent method + Variable.flag_public(self) + # Fixed-sized strings will need dispose if they are in the public header + if self.is_fixed_size: + self.needs_dispose = True diff --git a/build-aux/qmi-codegen/VariableStruct.py b/build-aux/qmi-codegen/VariableStruct.py index 445af71..78e6752 100644 --- a/build-aux/qmi-codegen/VariableStruct.py +++ b/build-aux/qmi-codegen/VariableStruct.py @@ -16,6 +16,7 @@ # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # Copyright (C) 2012 Lanedo GmbH +# Copyright (C) 2012-2015 Aleksander Morgado <aleksander@aleksander.es> # import string @@ -48,6 +49,8 @@ class VariableStruct(Variable): member = {} member['name'] = utils.build_underscore_name(member_dictionary['name']) member['object'] = VariableFactory.create_variable(member_dictionary, struct_type_name + ' ' + member['name'], self.container_type) + # Specify that the variable will be defined in the public header + member['object'].flag_public() self.members.append(member) # We'll need to dispose if at least one of the members needs it |