From e18cf9d616d36a7fef0eab2da34e81bc6dd928e1 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 3 Sep 2020 11:19:36 -0400 Subject: [PATCH] Make generated C code as `const` as possible This patch changes `gen.py` and many C-generating templates to make barectf generate C code with as many `const` variables as possible. The Jinja 2 `ft_c_type` filter now accepts a parameter to make the returned C type `const`. All the `*_params_str` filters also do; the reason is that I don't want the public header to show those useless `const` parameters (for variables), but I want the same parameters to be `const` in the C source file. Signed-off-by: Philippe Proulx --- barectf/gen.py | 45 +++++----- barectf/templates/c/barectf.c-macros.j2 | 2 +- barectf/templates/c/barectf.c.j2 | 84 ++++++++++--------- barectf/templates/c/barectf.h.j2 | 21 ++--- barectf/templates/c/close-func-proto.j2 | 3 +- barectf/templates/c/common.j2 | 26 ++++++ barectf/templates/c/ctx-init-func-proto.j2 | 5 +- barectf/templates/c/func-proto-params.j2 | 2 +- barectf/templates/c/open-func-proto.j2 | 3 +- .../c/serialize-write-bit-array-statements.j2 | 5 +- .../c/serialize-write-uuid-statements.j2 | 2 +- barectf/templates/c/trace-func-proto.j2 | 3 +- 12 files changed, 121 insertions(+), 80 deletions(-) diff --git a/barectf/gen.py b/barectf/gen.py index ae36036..073f328 100644 --- a/barectf/gen.py +++ b/barectf/gen.py @@ -453,8 +453,11 @@ class _CCodeGenerator: def _trace_type(self): return self._cfg.trace.type - # Returns the C type for the field type `ft`. - def _ft_c_type(self, ft): + # Returns the C type for the field type `ft`, returning a `const` C + # type if `is_const` is `True`. + def _ft_c_type(self, ft, is_const=False): + const_beg_str = 'const ' + if isinstance(ft, barectf_config._IntegerFieldType): sign_prefix = 'u' if isinstance(ft, barectf_config.UnsignedIntegerFieldType) else '' @@ -468,17 +471,19 @@ class _CCodeGenerator: assert ft.size <= 64 sz = 64 - return f'{sign_prefix}int{sz}_t' + return f'{const_beg_str if is_const else ""}{sign_prefix}int{sz}_t' elif type(ft) is barectf_config.RealFieldType: if ft.size == 32 and ft.alignment == 32: - return 'float' + c_type = 'float' elif ft.size == 64 and ft.alignment == 64: - return 'double' + c_type = 'double' else: - return 'uint64_t' + c_type = 'uint64_t' + + return f'{const_beg_str if is_const else ""}{c_type}' else: assert type(ft) is barectf_config.StringFieldType - return 'const char *' + return f'const char *{" const" if is_const else ""}' # Returns the function prototype parameters for the members of the # root structure field type `root_ft`. @@ -486,7 +491,7 @@ class _CCodeGenerator: # Each parameter has the prefix `name_prefix` followed with `_`. # # Members of which the name is in `exclude_set` are excluded. - def _proto_params_str(self, root_ft, name_prefix, exclude_set=None): + def _proto_params_str(self, root_ft, name_prefix, const_params, exclude_set=None): if root_ft is None: return @@ -501,14 +506,15 @@ class _CCodeGenerator: params.append(_FtParam(member.field_type, member_name)) - return self._func_proto_params_templ.render(params=params, prefix=name_prefix) + return self._func_proto_params_templ.render(params=params, prefix=name_prefix, + const_params=const_params) # Returns the packet opening function prototype parameters for the # stream type `stream_type`. - def _open_func_params_str(self, stream_type): + def _open_func_params_str(self, stream_type, const_params): parts = [] parts.append(self._proto_params_str(self._trace_type._pkt_header_ft, _RootFtPrefixes.PH, - {'magic', 'stream_id', 'uuid'})) + const_params, {'magic', 'stream_id', 'uuid'})) exclude_set = { 'timestamp_begin', @@ -518,38 +524,39 @@ class _CCodeGenerator: 'events_discarded', } parts.append(self._proto_params_str(stream_type._pkt_ctx_ft, _RootFtPrefixes.PC, - exclude_set)) + const_params, exclude_set)) return ''.join(parts) # Returns the tracing function prototype parameters for the stream # and event types `stream_ev_types`. - def _trace_func_params_str(self, stream_ev_types): + def _trace_func_params_str(self, stream_ev_types, const_params): stream_type = stream_ev_types[0] ev_type = stream_ev_types[1] parts = [] if stream_type._ev_header_ft is not None: parts.append(self._proto_params_str(stream_type._ev_header_ft, _RootFtPrefixes.EH, - {'id', 'timestamp'})) + const_params, {'id', 'timestamp'})) if stream_type.event_common_context_field_type is not None: parts.append(self._proto_params_str(stream_type.event_common_context_field_type, - _RootFtPrefixes.ECC)) + _RootFtPrefixes.ECC, const_params)) if ev_type.specific_context_field_type is not None: parts.append(self._proto_params_str(ev_type.specific_context_field_type, - _RootFtPrefixes.SC)) + _RootFtPrefixes.SC, const_params)) if ev_type.payload_field_type is not None: - parts.append(self._proto_params_str(ev_type.payload_field_type, _RootFtPrefixes.P)) + parts.append(self._proto_params_str(ev_type.payload_field_type, _RootFtPrefixes.P, + const_params)) return ''.join(parts) # Returns the event header serialization function prototype # parameters for the stream type `stream_type`. - def _serialize_ev_common_ctx_func_params_str(self, stream_type): + def _serialize_ev_common_ctx_func_params_str(self, stream_type, const_params): return self._proto_params_str(stream_type.event_common_context_field_type, - _RootFtPrefixes.ECC); + _RootFtPrefixes.ECC, const_params); # Generates the bitfield header file contents. def generate_bitfield_header(self): diff --git a/barectf/templates/c/barectf.c-macros.j2 b/barectf/templates/c/barectf.c-macros.j2 index b890faa..4ed0add 100644 --- a/barectf/templates/c/barectf.c-macros.j2 +++ b/barectf/templates/c/barectf.c-macros.j2 @@ -34,7 +34,7 @@ # the stream type `stream_type`. #} {% macro open_close_func_preamble(stream_type) %} -struct {{ prefix }}ctx *ctx = &sctx->parent; +struct {{ prefix }}ctx * const ctx = &sctx->parent; {% if stream_type.default_clock_type %} const {{ cg_opts.clock_type_c_types[stream_type.default_clock_type] }} ts = ctx->use_cur_last_event_ts ? sctx->cur_last_event_ts : diff --git a/barectf/templates/c/barectf.c.j2 b/barectf/templates/c/barectf.c.j2 index 88573e1..6e7d547 100644 --- a/barectf/templates/c/barectf.c.j2 +++ b/barectf/templates/c/barectf.c.j2 @@ -29,6 +29,7 @@ {% set ucprefix = common.ucprefix %} {% set ctx_struct_name = c_common.ctx_struct_name %} {% set cg_opts = cfg.options.code_generation_options %} +{% set const_params = true %} {% include 'licence-header.j2' %} @@ -65,88 +66,89 @@ union _d2u { uint64_t u; }; -uint32_t {{ prefix }}packet_size(void *ctx) +uint32_t {{ prefix }}packet_size(const void * const ctx) { - return _FROM_VOID_PTR(struct {{ ctx_struct_name }}, ctx)->packet_size; + return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, ctx)->packet_size; } -int {{ prefix }}packet_is_full(void *vctx) +int {{ prefix }}packet_is_full(const void * const vctx) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx); return ctx->at == ctx->packet_size; } -int {{ prefix }}packet_is_empty(void *vctx) +int {{ prefix }}packet_is_empty(const void * const vctx) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx); return ctx->at <= ctx->off_content; } -uint32_t {{ prefix }}packet_events_discarded(void *vctx) +uint32_t {{ prefix }}packet_events_discarded(const void * const vctx) { - return _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->events_discarded; + return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->events_discarded; } -uint8_t *{{ prefix }}packet_buf(void *vctx) +uint8_t *{{ prefix }}packet_buf(const void * const vctx) { - return _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->buf; + return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->buf; } -uint32_t {{ prefix }}packet_buf_size(void *vctx) +uint32_t {{ prefix }}packet_buf_size(const void * const vctx) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + const struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx); return _BITS_TO_BYTES(ctx->packet_size); } -void {{ prefix }}packet_set_buf(void *vctx, uint8_t *buf, uint32_t buf_size) +void {{ prefix }}packet_set_buf(void * const vctx, uint8_t * const buf, + const uint32_t buf_size) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); ctx->buf = buf; ctx->packet_size = _BYTES_TO_BITS(buf_size); } -int {{ prefix }}packet_is_open(void *vctx) +int {{ prefix }}packet_is_open(const void * const vctx) { - return _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->packet_is_open; + return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->packet_is_open; } -int {{ prefix }}is_in_tracing_section(void *vctx) +int {{ prefix }}is_in_tracing_section(const void * const vctx) { - return _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->in_tracing_section; + return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section; } -volatile const int *{{ prefix }}is_in_tracing_section_ptr(void *vctx) +volatile const int *{{ prefix }}is_in_tracing_section_ptr(const void * const vctx) { - return &_FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->in_tracing_section; + return &_FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->in_tracing_section; } -int {{ prefix }}is_tracing_enabled(void *vctx) +int {{ prefix }}is_tracing_enabled(const void * const vctx) { - return _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled; + return _FROM_VOID_PTR(const struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled; } -void {{ prefix }}enable_tracing(void *vctx, int enable) +void {{ prefix }}enable_tracing(void * const vctx, const int enable) { _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx)->is_tracing_enabled = enable; } static -void _write_c_str(struct {{ ctx_struct_name }} *ctx, const char *src) +void _write_c_str(struct {{ ctx_struct_name }} * const ctx, const char * const src) { - uint32_t sz = strlen(src) + 1; + const uint32_t sz = strlen(src) + 1; memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], src, sz); ctx->at += _BYTES_TO_BITS(sz); } static -int _reserve_ev_space(void *vctx, uint32_t ev_size) +int _reserve_ev_space(void * const vctx, const uint32_t ev_size) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); /* event _cannot_ fit? */ if (ev_size > (ctx->packet_size - ctx->off_content)) { @@ -194,9 +196,9 @@ int _reserve_ev_space(void *vctx, uint32_t ev_size) } static -void _commit_ev(void *vctx) +void _commit_ev(void * const vctx) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); /* is packet full? */ if ({{ prefix }}packet_is_full(ctx)) { @@ -208,7 +210,7 @@ void _commit_ev(void *vctx) {% include 'c/ctx-init-func-proto.j2' %} { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); ctx->cbs = cbs; ctx->data = data; ctx->buf = buf; @@ -377,12 +379,12 @@ void _commit_ev(void *vctx) } {% if stream_type._ev_header_ft %} -static void _serialize_ev_header_{{ stream_type.name }}(void *vctx, - uint32_t ev_type_id) +static void _serialize_ev_header_{{ stream_type.name }}(void * const vctx, + const uint32_t ev_type_id) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); {% if def_clk_type %} - struct {{ sctx_name }}_ctx *sctx = _FROM_VOID_PTR(struct {{ sctx_name }}_ctx, vctx); + struct {{ sctx_name }}_ctx * const sctx = _FROM_VOID_PTR(struct {{ sctx_name }}_ctx, vctx); const {{ cg_opts.clock_type_c_types[def_clk_type] }} ts = sctx->cur_last_event_ts; {% endif %} @@ -397,9 +399,9 @@ static void _serialize_ev_header_{{ stream_type.name }}(void *vctx, {% endif %} {% if stream_type.event_common_context_field_type %} -static void _serialize_ev_common_ctx_{{ stream_type.name }}(void *vctx{{ stream_type | serialize_ev_common_ctx_func_params_str }}) +static void _serialize_ev_common_ctx_{{ stream_type.name }}(void * const vctx{{ stream_type | serialize_ev_common_ctx_func_params_str(const_params) }}) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); /* serialize event common context */ { @@ -413,9 +415,9 @@ static void _serialize_ev_common_ctx_{{ stream_type.name }}(void *vctx{{ stream {# internal serialization functions #} {% for ev_type in stream_type.event_types | sort %} -static void _serialize_ev_{{ stream_type.name }}_{{ ev_type.name }}(void *vctx{{ (stream_type, ev_type) | trace_func_params_str }}) +static void _serialize_ev_{{ stream_type.name }}_{{ ev_type.name }}(void * const vctx{{ (stream_type, ev_type) | trace_func_params_str(const_params) }}) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); {% if stream_type._ev_header_ft %} /* serialize header */ @@ -454,9 +456,9 @@ static void _serialize_ev_{{ stream_type.name }}_{{ ev_type.name }}(void *vctx{{ {% for ev_type in stream_type.event_types | sort %} {% set this_ev_ops = this_stream_ops.ev_ops[ev_type] %} -static uint32_t _ev_size_{{ stream_type.name }}_{{ ev_type.name }}(void *vctx{{ (stream_type, ev_type) | trace_func_params_str }}) +static uint32_t _ev_size_{{ stream_type.name }}_{{ ev_type.name }}(void * const vctx{{ (stream_type, ev_type) | trace_func_params_str(const_params) }}) { - struct {{ ctx_struct_name }} *ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); + struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx); uint32_t at = ctx->at; {% if this_stream_ops.ev_header_ops %} @@ -508,7 +510,7 @@ static uint32_t _ev_size_{{ stream_type.name }}_{{ ev_type.name }}(void *vctx{{ {% include 'c/trace-func-proto.j2' %} { - struct {{ ctx_struct_name }} *ctx = &sctx->parent; + struct {{ ctx_struct_name }} * const ctx = &sctx->parent; uint32_t ev_size; {% if def_clk_type %} diff --git a/barectf/templates/c/barectf.h.j2 b/barectf/templates/c/barectf.h.j2 index 6c75a13..b6f18e2 100644 --- a/barectf/templates/c/barectf.h.j2 +++ b/barectf/templates/c/barectf.h.j2 @@ -30,6 +30,7 @@ {% set cg_opts = cfg.options.code_generation_options %} {% set def_stream_type = cg_opts.default_stream_type %} {% set header_opts = cg_opts.header_options %} +{% set const_params = false %} #ifndef _{{ ucprefix }}H #define _{{ ucprefix }}H @@ -57,17 +58,17 @@ extern "C" { struct {{ prefix }}ctx; -uint32_t {{ prefix }}packet_size(void *ctx); -int {{ prefix }}packet_is_full(void *ctx); -int {{ prefix }}packet_is_empty(void *ctx); -uint32_t {{ prefix }}packet_events_discarded(void *ctx); -uint8_t *{{ prefix }}packet_buf(void *ctx); +uint32_t {{ prefix }}packet_size(const void *ctx); +int {{ prefix }}packet_is_full(const void *ctx); +int {{ prefix }}packet_is_empty(const void *ctx); +uint32_t {{ prefix }}packet_events_discarded(const void *ctx); +uint8_t *{{ prefix }}packet_buf(const void *ctx); void {{ prefix }}packet_set_buf(void *ctx, uint8_t *buf, uint32_t buf_size); -uint32_t {{ prefix }}packet_buf_size(void *ctx); -int {{ prefix }}packet_is_open(void *ctx); -int {{ prefix }}is_in_tracing_section(void *ctx); -volatile const int *{{ prefix }}is_in_tracing_section_ptr(void *ctx); -int {{ prefix }}is_tracing_enabled(void *ctx); +uint32_t {{ prefix }}packet_buf_size(const void *ctx); +int {{ prefix }}packet_is_open(const void *ctx); +int {{ prefix }}is_in_tracing_section(const void *ctx); +volatile const int *{{ prefix }}is_in_tracing_section_ptr(const void *ctx); +int {{ prefix }}is_tracing_enabled(const void *ctx); void {{ prefix }}enable_tracing(void *ctx, int enable); /* barectf platform callbacks */ diff --git a/barectf/templates/c/close-func-proto.j2 b/barectf/templates/c/close-func-proto.j2 index a7b9f98..51ba42d 100644 --- a/barectf/templates/c/close-func-proto.j2 +++ b/barectf/templates/c/close-func-proto.j2 @@ -23,5 +23,6 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #} {% import 'common.j2' as common %} +{% import 'c/common.j2' as c_common %} /* close packet for stream type `{{ stream_type.name }}` */ -void {{ common.prefix }}{{ stream_type.name }}_close_packet(struct {{ common.prefix }}{{ stream_type.name }}_ctx *sctx) +void {{ common.prefix }}{{ stream_type.name }}_close_packet(struct {{ common.prefix }}{{ stream_type.name }}_ctx *{{ c_common.const_ptr_str(const_params) }}sctx) diff --git a/barectf/templates/c/common.j2 b/barectf/templates/c/common.j2 index 6715a78..96e8dfb 100644 --- a/barectf/templates/c/common.j2 +++ b/barectf/templates/c/common.j2 @@ -55,3 +55,29 @@ {% macro op_src(op) %} {{ op.names | join('_') }} {%- endmacro %} + +{# + # Generates: + # + # If `is_const` is `true`: + # The string ` const `. + # + # Otherwise: + # An empty string. + #} +{% macro const_ptr_str(is_const) %} +{{ ' const ' if is_const else '' }} +{%- endmacro %} + +{# + # Generates: + # + # If `is_const` is `true`: + # The string `const `. + # + # Otherwise: + # An empty string. + #} +{% macro const_str(is_const) %} +{{ 'const ' if is_const else '' }} +{%- endmacro %} diff --git a/barectf/templates/c/ctx-init-func-proto.j2 b/barectf/templates/c/ctx-init-func-proto.j2 index a99732f..c7a1405 100644 --- a/barectf/templates/c/ctx-init-func-proto.j2 +++ b/barectf/templates/c/ctx-init-func-proto.j2 @@ -23,7 +23,8 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #} {% import 'common.j2' as common %} +{% import 'c/common.j2' as c_common %} /* initialize context */ void {{ common.prefix }}init(void *vctx, - uint8_t *buf, uint32_t buf_size, - struct {{ common.prefix }}platform_callbacks cbs, void *data) + uint8_t *{{ c_common.const_ptr_str(const_params) }}buf, {{ c_common.const_str(const_params) }}uint32_t buf_size, + const struct {{ common.prefix }}platform_callbacks cbs, void *{{ c_common.const_ptr_str(const_params) }}data) diff --git a/barectf/templates/c/func-proto-params.j2 b/barectf/templates/c/func-proto-params.j2 index 5d30cc5..10af150 100644 --- a/barectf/templates/c/func-proto-params.j2 +++ b/barectf/templates/c/func-proto-params.j2 @@ -23,6 +23,6 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #} {% for param in params %} - {% set c_type = param.ft | ft_c_type %}, + {% set c_type = param.ft | ft_c_type(const_params) %}, {{ c_type }}{{ ' ' if not c_type.endswith('*') }}{{ prefix }}_{{ param.name }} {%- endfor %} diff --git a/barectf/templates/c/open-func-proto.j2 b/barectf/templates/c/open-func-proto.j2 index 919543b..60cd3a4 100644 --- a/barectf/templates/c/open-func-proto.j2 +++ b/barectf/templates/c/open-func-proto.j2 @@ -23,6 +23,7 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #} {% import 'common.j2' as common %} +{% import 'c/common.j2' as c_common %} /* open packet for stream type `{{ stream_type.name }}` */ void {{ common.prefix }}{{ stream_type.name }}_open_packet( - struct {{ common.prefix }}{{ stream_type.name }}_ctx *sctx{{ stream_type | open_func_params_str }}) + struct {{ common.prefix }}{{ stream_type.name }}_ctx *{{ c_common.const_ptr_str(const_params) }}sctx{{ stream_type | open_func_params_str(const_params) }}) diff --git a/barectf/templates/c/serialize-write-bit-array-statements.j2 b/barectf/templates/c/serialize-write-bit-array-statements.j2 index 285cd46..006ab76 100644 --- a/barectf/templates/c/serialize-write-bit-array-statements.j2 +++ b/barectf/templates/c/serialize-write-bit-array-statements.j2 @@ -24,7 +24,8 @@ #} {% import 'common.j2' as common %} {% set bo = 'le' if op.ft.byte_order == barectf_config.ByteOrder.LITTLE_ENDIAN else 'be' %} +{% set c_type_non_const = c_type | replace('const ', '') %} bt_bitfield_write_{{ bo }}(&ctx->buf[_BITS_TO_BYTES(ctx->at)], - uint8_t, {{ op.offset_in_byte }}, {{ op.ft.size }}, {{ c_type }}, - ({{ c_type }}) {{ src }}); + uint8_t, {{ op.offset_in_byte }}, {{ op.ft.size }}, {{ c_type_non_const }}, + ({{ c_type_non_const }}) {{ src }}); ctx->at += {{ op.ft.size }}; diff --git a/barectf/templates/c/serialize-write-uuid-statements.j2 b/barectf/templates/c/serialize-write-uuid-statements.j2 index 825b5a8..e4ade0c 100644 --- a/barectf/templates/c/serialize-write-uuid-statements.j2 +++ b/barectf/templates/c/serialize-write-uuid-statements.j2 @@ -24,7 +24,7 @@ #} /* write UUID */ { - static uint8_t uuid[] = { + static const uint8_t uuid[] = { {% for row in cfg.trace.type.uuid.bytes | batch(4) %} {{ row | join(', ') }}, {% endfor %} diff --git a/barectf/templates/c/trace-func-proto.j2 b/barectf/templates/c/trace-func-proto.j2 index 9b221ef..a25cd67 100644 --- a/barectf/templates/c/trace-func-proto.j2 +++ b/barectf/templates/c/trace-func-proto.j2 @@ -23,5 +23,6 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #} {% import 'common.j2' as common %} +{% import 'c/common.j2' as c_common %} /* trace (stream type `{{ stream_type.name }}`, event type `{{ ev_type.name }}`) */ -void {{ common.prefix }}{{ stream_type.name }}_trace_{{ ev_type.name }}(struct {{ common.prefix }}{{ stream_type.name }}_ctx *sctx{{ (stream_type, ev_type) | trace_func_params_str }}) +void {{ common.prefix }}{{ stream_type.name }}_trace_{{ ev_type.name }}(struct {{ common.prefix }}{{ stream_type.name }}_ctx *{{ c_common.const_ptr_str(const_params) }}sctx{{ (stream_type, ev_type) | trace_func_params_str(const_params) }}) -- 2.34.1