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 ''
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`.
# 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
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',
'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):
# 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 :
{% 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' %}
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)) {
}
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)) {
{% 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;
}
{% 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 %}
{% 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 */
{
{# 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 */
{% 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 %}
{% 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 %}
{% 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
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 */
# 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)
{% 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 %}
# 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)
# 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 %}
# 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) }})
#}
{% 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 }};
#}
/* write UUID */
{
- static uint8_t uuid[] = {
+ static const uint8_t uuid[] = {
{% for row in cfg.trace.type.uuid.bytes | batch(4) %}
{{ row | join(', ') }},
{% endfor %}
# 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) }})