From de3dd40e6fcad56e227f5fc8a8290fbaa88b4e07 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 16 Jul 2015 13:04:50 -0400 Subject: [PATCH] ir: consolidate reference counting functions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch adds the bt_ctf_get() and bt_ctf_put() functions which resp. get and put references of any CTF IR object. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/clock.c | 26 ++-- formats/ctf/ir/event-fields.c | 20 +-- formats/ctf/ir/event-types.c | 124 ++++++++---------- formats/ctf/ir/event.c | 51 +++---- formats/ctf/ir/stream-class.c | 26 ++-- formats/ctf/ir/stream.c | 26 ++-- formats/ctf/ir/trace.c | 25 ++-- formats/ctf/writer/writer.c | 26 ++-- include/Makefile.am | 4 +- include/babeltrace/ctf-ir/clock-internal.h | 4 +- include/babeltrace/ctf-ir/clock.h | 2 + include/babeltrace/ctf-ir/common-internal.h | 47 +++++++ .../babeltrace/ctf-ir/event-fields-internal.h | 4 +- include/babeltrace/ctf-ir/event-fields.h | 2 + include/babeltrace/ctf-ir/event-internal.h | 6 +- .../babeltrace/ctf-ir/event-types-internal.h | 4 +- include/babeltrace/ctf-ir/event-types.h | 2 + include/babeltrace/ctf-ir/event.h | 4 + include/babeltrace/ctf-ir/ref.h | 98 ++++++++++++++ .../babeltrace/ctf-ir/stream-class-internal.h | 4 +- include/babeltrace/ctf-ir/stream-class.h | 2 + include/babeltrace/ctf-ir/stream-internal.h | 4 +- include/babeltrace/ctf-ir/stream.h | 2 + include/babeltrace/ctf-ir/trace-internal.h | 4 +- include/babeltrace/ctf-ir/trace.h | 2 + .../babeltrace/ctf-writer/writer-internal.h | 4 +- include/babeltrace/ctf-writer/writer.h | 2 + .../{ctf-writer => }/ref-internal.h | 28 ++-- lib/objects.c | 29 ++-- 29 files changed, 349 insertions(+), 233 deletions(-) create mode 100644 include/babeltrace/ctf-ir/common-internal.h create mode 100644 include/babeltrace/ctf-ir/ref.h rename include/babeltrace/{ctf-writer => }/ref-internal.h (74%) diff --git a/formats/ctf/ir/clock.c b/formats/ctf/ir/clock.c index d1f435d5..22a5d747 100644 --- a/formats/ctf/ir/clock.c +++ b/formats/ctf/ir/clock.c @@ -28,12 +28,14 @@ #include #include +#include +#include #include #include #include static -void bt_ctf_clock_destroy(struct bt_ctf_ref *ref); +void bt_ctf_clock_destroy(struct bt_ref *ref); BT_HIDDEN struct bt_ctf_clock *_bt_ctf_clock_create(void) @@ -47,7 +49,7 @@ struct bt_ctf_clock *_bt_ctf_clock_create(void) clock->precision = 1; clock->frequency = 1000000000; - bt_ctf_ref_init(&clock->ref_count); + bt_ctf_base_init(clock, bt_ctf_clock_destroy); end: return clock; } @@ -100,7 +102,7 @@ struct bt_ctf_clock *bt_ctf_clock_create(const char *name) clock->uuid_set = 1; return clock; error_destroy: - bt_ctf_clock_destroy(&clock->ref_count); + bt_ctf_clock_destroy(&clock->base.ref_count); error: return NULL; } @@ -345,20 +347,12 @@ end: void bt_ctf_clock_get(struct bt_ctf_clock *clock) { - if (!clock) { - return; - } - - bt_ctf_ref_get(&clock->ref_count); + bt_ctf_get(clock); } void bt_ctf_clock_put(struct bt_ctf_clock *clock) { - if (!clock) { - return; - } - - bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy); + bt_ctf_put(clock); } BT_HIDDEN @@ -410,15 +404,17 @@ void bt_ctf_clock_serialize(struct bt_ctf_clock *clock, } static -void bt_ctf_clock_destroy(struct bt_ctf_ref *ref) +void bt_ctf_clock_destroy(struct bt_ref *ref) { struct bt_ctf_clock *clock; + struct bt_ctf_base *base; if (!ref) { return; } - clock = container_of(ref, struct bt_ctf_clock, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + clock = container_of(base, struct bt_ctf_clock, base); if (clock->name) { g_string_free(clock->name, TRUE); } diff --git a/formats/ctf/ir/event-fields.c b/formats/ctf/ir/event-fields.c index 2ca21f10..2fb783c3 100644 --- a/formats/ctf/ir/event-fields.c +++ b/formats/ctf/ir/event-fields.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) @@ -57,7 +59,7 @@ static struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); static -void bt_ctf_field_destroy(struct bt_ctf_ref *); +void bt_ctf_field_destroy(struct bt_ref *); static void bt_ctf_field_integer_destroy(struct bt_ctf_field *); static @@ -250,7 +252,7 @@ struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) /* The type's declaration can't change after this point */ bt_ctf_field_type_freeze(type); bt_ctf_field_type_get(type); - bt_ctf_ref_init(&field->ref_count); + bt_ctf_base_init(field, bt_ctf_field_destroy); field->type = type; error: return field; @@ -258,16 +260,12 @@ error: void bt_ctf_field_get(struct bt_ctf_field *field) { - if (field) { - bt_ctf_ref_get(&field->ref_count); - } + bt_ctf_get(field); } void bt_ctf_field_put(struct bt_ctf_field *field) { - if (field) { - bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy); - } + bt_ctf_put(field); } struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) @@ -1275,9 +1273,10 @@ struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) } static -void bt_ctf_field_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_destroy(struct bt_ref *ref) { struct bt_ctf_field *field; + struct bt_ctf_base *base; struct bt_ctf_field_type *type; enum ctf_type_id type_id; @@ -1285,7 +1284,8 @@ void bt_ctf_field_destroy(struct bt_ctf_ref *ref) return; } - field = container_of(ref, struct bt_ctf_field, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + field = container_of(base, struct bt_ctf_field, base); type = field->type; type_id = bt_ctf_field_type_get_type_id(type); if (type_id <= CTF_TYPE_UNKNOWN || diff --git a/formats/ctf/ir/event-types.c b/formats/ctf/ir/event-types.c index 5079d698..450f8d58 100644 --- a/formats/ctf/ir/event-types.c +++ b/formats/ctf/ir/event-types.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -52,26 +54,26 @@ struct range_overlap_query { }; static -void bt_ctf_field_type_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_destroy(struct bt_ref *); static -void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *); static -void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *); +void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *); static -void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = { +void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = { [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy, [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_destroy, @@ -298,7 +300,7 @@ void bt_ctf_field_type_init(struct bt_ctf_field_type *type, int init_bo) assert(type && (type_id > CTF_TYPE_UNKNOWN) && (type_id < NR_CTF_TYPES)); - bt_ctf_ref_init(&type->ref_count); + bt_ctf_base_init(type, bt_ctf_field_type_destroy); type->freeze = type_freeze_funcs[type_id]; type->serialize = type_serialize_funcs[type_id]; @@ -346,23 +348,25 @@ end: } static -void bt_ctf_field_type_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_destroy(struct bt_ref *ref) { struct bt_ctf_field_type *type; + struct bt_ctf_base *base; enum ctf_type_id type_id; if (!ref) { return; } - type = container_of(ref, struct bt_ctf_field_type, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + type = container_of(base, struct bt_ctf_field_type, base); type_id = type->declaration->id; if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { return; } - type_destroy_funcs[type_id](ref); + type_destroy_funcs[type_id](type); } BT_HIDDEN @@ -1920,20 +1924,12 @@ enum ctf_type_id bt_ctf_field_type_get_type_id( void bt_ctf_field_type_get(struct bt_ctf_field_type *type) { - if (!type) { - return; - } - - bt_ctf_ref_get(&type->ref_count); + bt_ctf_get(type); } void bt_ctf_field_type_put(struct bt_ctf_field_type *type) { - if (!type) { - return; - } - - bt_ctf_ref_put(&type->ref_count, bt_ctf_field_type_destroy); + bt_ctf_put(type); } BT_HIDDEN @@ -2302,17 +2298,15 @@ end: } static -void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_integer *integer; + struct bt_ctf_field_type_integer *integer = + (struct bt_ctf_field_type_integer *) type; - if (!ref) { + if (!type) { return; } - integer = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_integer, parent); if (integer->mapped_clock) { bt_ctf_clock_put(integer->mapped_clock); } @@ -2320,66 +2314,58 @@ void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref) } static -void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_enumeration *enumeration; + struct bt_ctf_field_type_enumeration *enumeration = + (struct bt_ctf_field_type_enumeration *) type; - if (!ref) { + if (!type) { return; } - enumeration = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_enumeration, parent); g_ptr_array_free(enumeration->entries, TRUE); bt_ctf_field_type_put(enumeration->container); g_free(enumeration); } static -void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_floating_point *floating_point; + struct bt_ctf_field_type_floating_point *floating_point = + (struct bt_ctf_field_type_floating_point *) type; - if (!ref) { + if (!type) { return; } - floating_point = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_floating_point, parent); g_free(floating_point); } static -void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_structure *structure; + struct bt_ctf_field_type_structure *structure = + (struct bt_ctf_field_type_structure *) type; - if (!ref) { + if (!type) { return; } - structure = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_structure, parent); g_ptr_array_free(structure->fields, TRUE); g_hash_table_destroy(structure->field_name_to_index); g_free(structure); } static -void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_variant *variant; + struct bt_ctf_field_type_variant *variant = + (struct bt_ctf_field_type_variant *) type; - if (!ref) { + if (!type) { return; } - variant = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_variant, parent); g_ptr_array_free(variant->fields, TRUE); g_hash_table_destroy(variant->field_name_to_index); g_string_free(variant->tag_name, TRUE); @@ -2389,33 +2375,29 @@ void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref) } static -void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_array *array; + struct bt_ctf_field_type_array *array = + (struct bt_ctf_field_type_array *) type; - if (!ref) { + if (!type) { return; } - array = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_array, parent); bt_ctf_field_type_put(array->element_type); g_free(array); } static -void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_sequence *sequence; + struct bt_ctf_field_type_sequence *sequence = + (struct bt_ctf_field_type_sequence *) type; - if (!ref) { + if (!type) { return; } - sequence = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_sequence, parent); bt_ctf_field_type_put(sequence->element_type); g_string_free(sequence->length_field_name, TRUE); bt_ctf_field_path_destroy(sequence->length_field_path); @@ -2423,17 +2405,15 @@ void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref) } static -void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref) +void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type) { - struct bt_ctf_field_type_string *string; + struct bt_ctf_field_type_string *string = + (struct bt_ctf_field_type_string *) type; - if (!ref) { + if (!type) { return; } - string = container_of( - container_of(ref, struct bt_ctf_field_type, ref_count), - struct bt_ctf_field_type_string, parent); g_free(string); } diff --git a/formats/ctf/ir/event.c b/formats/ctf/ir/event.c index 586b69b6..6cb9f2d5 100644 --- a/formats/ctf/ir/event.c +++ b/formats/ctf/ir/event.c @@ -36,13 +36,15 @@ #include #include #include +#include +#include #include #include static -void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref); +void bt_ctf_event_class_destroy(struct bt_ref *ref); static -void bt_ctf_event_destroy(struct bt_ctf_ref *ref); +void bt_ctf_event_destroy(struct bt_ref *ref); static int set_integer_field_value(struct bt_ctf_field *field, uint64_t value); @@ -61,7 +63,7 @@ struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) goto error; } - bt_ctf_ref_init(&event_class->ref_count); + bt_ctf_base_init(event_class, bt_ctf_event_class_destroy); event_class->fields = bt_ctf_field_type_structure_create(); if (!event_class->fields) { goto error; @@ -510,20 +512,12 @@ end: void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) { - if (!event_class) { - return; - } - - bt_ctf_ref_get(&event_class->ref_count); + bt_ctf_get(event_class); } void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) { - if (!event_class) { - return; - } - - bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); + bt_ctf_put(event_class); } BT_HIDDEN @@ -572,7 +566,7 @@ struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) goto end; } - bt_ctf_ref_init(&event->ref_count); + bt_ctf_base_init(event, bt_ctf_event_destroy); bt_ctf_event_class_get(event_class); bt_ctf_event_class_freeze(event_class); event->event_class = event_class; @@ -603,7 +597,7 @@ end: return event; error_destroy: if (event) { - bt_ctf_event_destroy(&event->ref_count); + bt_ctf_event_destroy(&event->base.ref_count); } return NULL; @@ -884,26 +878,19 @@ end: void bt_ctf_event_get(struct bt_ctf_event *event) { - if (!event) { - return; - } - - bt_ctf_ref_get(&event->ref_count); + bt_ctf_get(event); } void bt_ctf_event_put(struct bt_ctf_event *event) { - if (!event) { - return; - } - - bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy); + bt_ctf_put(event); } static -void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) +void bt_ctf_event_class_destroy(struct bt_ref *ref) { struct bt_ctf_event_class *event_class; + struct bt_ctf_base *base; if (!ref) { return; @@ -913,7 +900,8 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) * Don't call put() on the stream class. See comment in * bt_ctf_event_class_set_stream_class for explanation. */ - event_class = container_of(ref, struct bt_ctf_event_class, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + event_class = container_of(base, struct bt_ctf_event_class, base); if (event_class->attributes) { bt_ctf_attributes_destroy(event_class->attributes); } @@ -927,16 +915,17 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) } static -void bt_ctf_event_destroy(struct bt_ctf_ref *ref) +void bt_ctf_event_destroy(struct bt_ref *ref) { struct bt_ctf_event *event; + struct bt_ctf_base *base; if (!ref) { return; } - event = container_of(ref, struct bt_ctf_event, - ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + event = container_of(base, struct bt_ctf_event, base); if (event->event_class) { bt_ctf_event_class_put(event->event_class); } @@ -1292,7 +1281,7 @@ struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event) goto error; } - bt_ctf_ref_init(©->ref_count); + bt_ctf_base_init(copy, bt_ctf_event_destroy); copy->event_class = event->event_class; bt_ctf_event_class_get(copy->event_class); copy->stream = event->stream; diff --git a/formats/ctf/ir/stream-class.c b/formats/ctf/ir/stream-class.c index 72b07b84..1b4e9eb5 100644 --- a/formats/ctf/ir/stream-class.c +++ b/formats/ctf/ir/stream-class.c @@ -37,11 +37,13 @@ #include #include #include +#include +#include #include #include static -void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref); +void bt_ctf_stream_class_destroy(struct bt_ref *ref); static int init_event_header(struct bt_ctf_stream_class *stream_class); static @@ -78,11 +80,11 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) goto error_destroy; } - bt_ctf_ref_init(&stream_class->ref_count); + bt_ctf_base_init(stream_class, bt_ctf_stream_class_destroy); return stream_class; error_destroy: - bt_ctf_stream_class_destroy(&stream_class->ref_count); + bt_ctf_stream_class_destroy(&stream_class->base.ref_count); stream_class = NULL; error: return stream_class; @@ -608,20 +610,12 @@ end: void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) { - if (!stream_class) { - return; - } - - bt_ctf_ref_get(&stream_class->ref_count); + bt_ctf_get(stream_class); } void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) { - if (!stream_class) { - return; - } - - bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy); + bt_ctf_put(stream_class); } BT_HIDDEN @@ -764,15 +758,17 @@ end: } static -void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref) +void bt_ctf_stream_class_destroy(struct bt_ref *ref) { struct bt_ctf_stream_class *stream_class; + struct bt_ctf_base *base; if (!ref) { return; } - stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + stream_class = container_of(base, struct bt_ctf_stream_class, base); bt_ctf_clock_put(stream_class->clock); if (stream_class->event_classes) { diff --git a/formats/ctf/ir/stream.c b/formats/ctf/ir/stream.c index 047fc292..29874924 100644 --- a/formats/ctf/ir/stream.c +++ b/formats/ctf/ir/stream.c @@ -35,13 +35,15 @@ #include #include #include +#include +#include #include #include #include #include static -void bt_ctf_stream_destroy(struct bt_ctf_ref *ref); +void bt_ctf_stream_destroy(struct bt_ref *ref); static int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t); @@ -272,7 +274,7 @@ struct bt_ctf_stream *bt_ctf_stream_create( /* A stream has no ownership of its trace (weak ptr) */ stream->trace = trace; - bt_ctf_ref_init(&stream->ref_count); + bt_ctf_base_init(stream, bt_ctf_stream_destroy); stream->packet_context = bt_ctf_field_create( stream_class->packet_context_type); if (!stream->packet_context) { @@ -338,7 +340,7 @@ struct bt_ctf_stream *bt_ctf_stream_create( end: return stream; error_destroy: - bt_ctf_stream_destroy(&stream->ref_count); + bt_ctf_stream_destroy(&stream->base.ref_count); return NULL; } @@ -897,32 +899,26 @@ end: void bt_ctf_stream_get(struct bt_ctf_stream *stream) { - if (!stream) { - return; - } - - bt_ctf_ref_get(&stream->ref_count); + bt_ctf_get(stream); } void bt_ctf_stream_put(struct bt_ctf_stream *stream) { - if (!stream) { - return; - } - - bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy); + bt_ctf_put(stream); } static -void bt_ctf_stream_destroy(struct bt_ctf_ref *ref) +void bt_ctf_stream_destroy(struct bt_ref *ref) { struct bt_ctf_stream *stream; + struct bt_ctf_base *base; if (!ref) { return; } - stream = container_of(ref, struct bt_ctf_stream, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + stream = container_of(base, struct bt_ctf_stream, base); ctf_fini_pos(&stream->pos); if (stream->pos.fd >= 0 && close(stream->pos.fd)) { perror("close"); diff --git a/formats/ctf/ir/trace.c b/formats/ctf/ir/trace.c index d911dfd6..d5786d33 100644 --- a/formats/ctf/ir/trace.c +++ b/formats/ctf/ir/trace.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include @@ -42,7 +44,7 @@ #define DEFAULT_METADATA_STRING_SIZE 4096 static -void bt_ctf_trace_destroy(struct bt_ctf_ref *ref); +void bt_ctf_trace_destroy(struct bt_ref *ref); static int init_trace_packet_header(struct bt_ctf_trace *trace); static @@ -83,7 +85,7 @@ struct bt_ctf_trace *bt_ctf_trace_create(void) } bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); - bt_ctf_ref_init(&trace->ref_count); + bt_ctf_base_init(trace, bt_ctf_trace_destroy); trace->clocks = g_ptr_array_new_with_free_func( (GDestroyNotify) bt_ctf_clock_put); trace->streams = g_ptr_array_new_with_free_func( @@ -109,21 +111,23 @@ struct bt_ctf_trace *bt_ctf_trace_create(void) return trace; error_destroy: - bt_ctf_trace_destroy(&trace->ref_count); + bt_ctf_trace_destroy(&trace->base.ref_count); trace = NULL; error: return trace; } -void bt_ctf_trace_destroy(struct bt_ctf_ref *ref) +void bt_ctf_trace_destroy(struct bt_ref *ref) { struct bt_ctf_trace *trace; + struct bt_ctf_base *base; if (!ref) { return; } - trace = container_of(ref, struct bt_ctf_trace, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + trace = container_of(base, struct bt_ctf_trace, base); if (trace->environment) { bt_ctf_attributes_destroy(trace->environment); } @@ -859,20 +863,13 @@ end: void bt_ctf_trace_get(struct bt_ctf_trace *trace) { - if (!trace) { - return; - } - - bt_ctf_ref_get(&trace->ref_count); + bt_ctf_get(trace); } void bt_ctf_trace_put(struct bt_ctf_trace *trace) { - if (!trace) { - return; - } + bt_ctf_put(trace); - bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy); } BT_HIDDEN diff --git a/formats/ctf/writer/writer.c b/formats/ctf/writer/writer.c index 51e545f6..9adfcd38 100644 --- a/formats/ctf/writer/writer.c +++ b/formats/ctf/writer/writer.c @@ -33,6 +33,8 @@ #include #include #include +#include +#include #include #include #include @@ -43,7 +45,7 @@ #include static -void bt_ctf_writer_destroy(struct bt_ctf_ref *ref); +void bt_ctf_writer_destroy(struct bt_ref *ref); static int create_stream_file(struct bt_ctf_writer *writer, struct bt_ctf_stream *stream); @@ -61,7 +63,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) goto error; } - bt_ctf_ref_init(&writer->ref_count); + bt_ctf_base_init(writer, bt_ctf_writer_destroy); writer->path = g_string_new(path); if (!writer->path) { goto error_destroy; @@ -92,21 +94,23 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path) error_destroy: unlinkat(writer->trace_dir_fd, "metadata", 0); - bt_ctf_writer_destroy(&writer->ref_count); + bt_ctf_writer_destroy(&writer->base.ref_count); writer = NULL; error: return writer; } -void bt_ctf_writer_destroy(struct bt_ctf_ref *ref) +void bt_ctf_writer_destroy(struct bt_ref *ref) { struct bt_ctf_writer *writer; + struct bt_ctf_base *base; if (!ref) { return; } - writer = container_of(ref, struct bt_ctf_writer, ref_count); + base = container_of(ref, struct bt_ctf_base, ref_count); + writer = container_of(base, struct bt_ctf_writer, base); bt_ctf_writer_flush_metadata(writer); if (writer->path) { g_string_free(writer->path, TRUE); @@ -267,20 +271,12 @@ end: void bt_ctf_writer_get(struct bt_ctf_writer *writer) { - if (!writer) { - return; - } - - bt_ctf_ref_get(&writer->ref_count); + bt_ctf_get(writer); } void bt_ctf_writer_put(struct bt_ctf_writer *writer) { - if (!writer) { - return; - } - - bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy); + bt_ctf_put(writer); } static diff --git a/include/Makefile.am b/include/Makefile.am index 35a52f96..b70ee7e6 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -27,6 +27,7 @@ babeltracectfirinclude_HEADERS = \ babeltrace/ctf-ir/event-fields.h \ babeltrace/ctf-ir/event-types.h \ babeltrace/ctf-ir/event.h \ + babeltrace/ctf-ir/ref.h \ babeltrace/ctf-ir/stream.h \ babeltrace/ctf-ir/stream-class.h \ babeltrace/ctf-ir/trace.h \ @@ -43,6 +44,7 @@ noinst_HEADERS = \ babeltrace/iterator-internal.h \ babeltrace/trace-collection.h \ babeltrace/prio_heap.h \ + babeltrace/ref-internal.h \ babeltrace/types.h \ babeltrace/ctf-ir/metadata.h \ babeltrace/ctf/events-internal.h \ @@ -51,9 +53,9 @@ noinst_HEADERS = \ babeltrace/ctf/types.h \ babeltrace/ctf/callbacks-internal.h \ babeltrace/ctf/ctf-index.h \ - babeltrace/ctf-writer/ref-internal.h \ babeltrace/ctf-writer/writer-internal.h \ babeltrace/ctf-ir/attributes-internal.h \ + babeltrace/ctf-ir/common-internal.h \ babeltrace/ctf-ir/event-types-internal.h \ babeltrace/ctf-ir/event-fields-internal.h \ babeltrace/ctf-ir/event-internal.h \ diff --git a/include/babeltrace/ctf-ir/clock-internal.h b/include/babeltrace/ctf-ir/clock-internal.h index f876cd30..7428126e 100644 --- a/include/babeltrace/ctf-ir/clock-internal.h +++ b/include/babeltrace/ctf-ir/clock-internal.h @@ -27,15 +27,15 @@ * SOFTWARE. */ -#include #include #include +#include #include #include #include struct bt_ctf_clock { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; GString *name; GString *description; uint64_t frequency; diff --git a/include/babeltrace/ctf-ir/clock.h b/include/babeltrace/ctf-ir/clock.h index cad2eec9..1ac8ca9e 100644 --- a/include/babeltrace/ctf-ir/clock.h +++ b/include/babeltrace/ctf-ir/clock.h @@ -258,6 +258,8 @@ extern int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, * bt_ctf_clock_get and bt_ctf_clock_put: increment and decrement the * refcount of the clock * + * You may also use bt_ctf_get() and bt_ctf_put() with clock objects. + * * These functions ensure that the clock won't be destroyed when it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) has to be done to diff --git a/include/babeltrace/ctf-ir/common-internal.h b/include/babeltrace/ctf-ir/common-internal.h new file mode 100644 index 00000000..5922780b --- /dev/null +++ b/include/babeltrace/ctf-ir/common-internal.h @@ -0,0 +1,47 @@ +#ifndef BABELTRACE_CTF_IR_COMMON_INTERNAL_H +#define BABELTRACE_CTF_IR_COMMON_INTERNAL_H + +/* + * Babeltrace - CTF IR: common data structures + * + * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +/* + * bt_ctf_trace must be the base class of _all_ CTF IR classes and is assumed + * to be the first member of all bt_ctf_* structures. + */ +struct bt_ctf_base { + struct bt_ref ref_count; +}; + +static inline +void bt_ctf_base_init(void *obj, bt_ref_release_func_t release_func) +{ + struct bt_ctf_base *base = obj; + + bt_ref_init(&base->ref_count, release_func); +} + +#endif /* BABELTRACE_CTF_IR_COMMON_INTERNAL_H */ diff --git a/include/babeltrace/ctf-ir/event-fields-internal.h b/include/babeltrace/ctf-ir/event-fields-internal.h index 34f8fad4..e0214e82 100644 --- a/include/babeltrace/ctf-ir/event-fields-internal.h +++ b/include/babeltrace/ctf-ir/event-fields-internal.h @@ -27,14 +27,14 @@ * SOFTWARE. */ -#include #include +#include #include #include #include struct bt_ctf_field { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; struct bt_ctf_field_type *type; int payload_set; }; diff --git a/include/babeltrace/ctf-ir/event-fields.h b/include/babeltrace/ctf-ir/event-fields.h index 4a55b449..2b682bb6 100644 --- a/include/babeltrace/ctf-ir/event-fields.h +++ b/include/babeltrace/ctf-ir/event-fields.h @@ -370,6 +370,8 @@ extern struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field); * bt_ctf_field_get and bt_ctf_field_put: increment and decrement the * field's reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with field objects. + * * These functions ensure that the field won't be destroyed when it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-ir/event-internal.h b/include/babeltrace/ctf-ir/event-internal.h index 5201ee53..9b2e2075 100644 --- a/include/babeltrace/ctf-ir/event-internal.h +++ b/include/babeltrace/ctf-ir/event-internal.h @@ -27,7 +27,6 @@ * SOFTWARE. */ -#include #include #include #include @@ -35,13 +34,14 @@ #include #include #include +#include #include #define BT_CTF_EVENT_CLASS_ATTR_ID_INDEX 0 #define BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX 1 struct bt_ctf_event_class { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; struct bt_object *attributes; /* * Weak reference; an event class does not have ownership of a @@ -56,7 +56,7 @@ struct bt_ctf_event_class { }; struct bt_ctf_event { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; struct bt_ctf_event_class *event_class; /* Weak reference; an event does not have ownership of a stream */ struct bt_ctf_stream *stream; diff --git a/include/babeltrace/ctf-ir/event-types-internal.h b/include/babeltrace/ctf-ir/event-types-internal.h index 8475198b..d5290f57 100644 --- a/include/babeltrace/ctf-ir/event-types-internal.h +++ b/include/babeltrace/ctf-ir/event-types-internal.h @@ -28,10 +28,10 @@ */ #include -#include #include #include #include +#include #include #include #include @@ -62,7 +62,7 @@ struct bt_ctf_field_path { }; struct bt_ctf_field_type { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; struct bt_declaration *declaration; type_freeze_func freeze; type_serialize_func serialize; diff --git a/include/babeltrace/ctf-ir/event-types.h b/include/babeltrace/ctf-ir/event-types.h index fdec2e07..d777ce6b 100644 --- a/include/babeltrace/ctf-ir/event-types.h +++ b/include/babeltrace/ctf-ir/event-types.h @@ -755,6 +755,8 @@ extern enum ctf_type_id bt_ctf_field_type_get_type_id( * bt_ctf_field_type_get and bt_ctf_field_type_put: increment and decrement * the field type's reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with field type objects. + * * These functions ensure that the field type won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-ir/event.h b/include/babeltrace/ctf-ir/event.h index 09ada907..4c90a2eb 100644 --- a/include/babeltrace/ctf-ir/event.h +++ b/include/babeltrace/ctf-ir/event.h @@ -307,6 +307,8 @@ extern int bt_ctf_event_class_set_context_type( * bt_ctf_event_class_get and bt_ctf_event_class_put: increment and decrement * the event class' reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with event class objects. + * * These functions ensure that the event class won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to @@ -512,6 +514,8 @@ extern struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event); * bt_ctf_event_get and bt_ctf_event_put: increment and decrement * the event's reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with event objects. + * * These functions ensure that the event won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-ir/ref.h b/include/babeltrace/ctf-ir/ref.h new file mode 100644 index 00000000..fa612ca0 --- /dev/null +++ b/include/babeltrace/ctf-ir/ref.h @@ -0,0 +1,98 @@ +#ifndef BABELTRACE_CTF_IR_REF_H +#define BABELTRACE_CTF_IR_REF_H + +/* + * BabelTrace - CTF IR: common reference counting + * + * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation + * Copyright (c) 2015 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +/* + * BT_CTF_PUT: calls bt_ctf_put() with a variable, then sets this + * variable to NULL. + * + * A common action with CTF IR objects is to create or get one, do + * something with it, and then put it. To avoid putting it a second time + * later (if an error occurs, for example), the variable is often reset + * to NULL after putting the object it points to. Since this is so + * common, you can use the BT_CTF_PUT() macro, which does just that. + * + * It is safe to call this function with a NULL object. + * + * @param obj CTF IR object. + */ +#define BT_CTF_PUT(_obj) \ + do { \ + bt_ctf_put(_obj); \ + (_obj) = NULL; \ + } while (0) + +/* + * bt_ctf_get: increments the reference count of a CTF IR object. + * + * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra + * bt_ctf_put() to release the initial reference done at creation) have + * to be done to destroy a CTF IR object. + * + * It is safe to call this function with a NULL object. + * + * @param obj CTF IR object. + */ +static inline +void bt_ctf_get(void *obj) +{ + if (obj) { + struct bt_ctf_base *base = obj; + + bt_ref_get(&base->ref_count); + } +} + +/* + * bt_ctf_put: decrements the reference count of a CTF IR object. + * + * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra + * bt_ctf_put() to release the initial reference done at creation) have + * to be done to destroy a CTF IR object. + * + * When the object's reference count is decremented to 0 by a call to + * bt_ctf_put(), the object is freed. + * + * It is safe to call this function with a NULL object. + * + * @param obj CTF IR object. + */ +static inline +void bt_ctf_put(void *obj) +{ + if (obj) { + struct bt_ctf_base *base = obj; + + bt_ref_put(&base->ref_count); + } +} + +#endif /* BABELTRACE_CTF_IR_REF_H */ diff --git a/include/babeltrace/ctf-ir/stream-class-internal.h b/include/babeltrace/ctf-ir/stream-class-internal.h index c88be91c..4338742e 100644 --- a/include/babeltrace/ctf-ir/stream-class-internal.h +++ b/include/babeltrace/ctf-ir/stream-class-internal.h @@ -27,17 +27,17 @@ * SOFTWARE. */ -#include #include #include #include #include +#include #include #include #include struct bt_ctf_stream_class { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; GString *name; struct bt_ctf_clock *clock; GPtrArray *event_classes; /* Array of pointers to bt_ctf_event_class */ diff --git a/include/babeltrace/ctf-ir/stream-class.h b/include/babeltrace/ctf-ir/stream-class.h index 200a1fd1..75be6b67 100644 --- a/include/babeltrace/ctf-ir/stream-class.h +++ b/include/babeltrace/ctf-ir/stream-class.h @@ -290,6 +290,8 @@ extern int bt_ctf_stream_class_set_event_context_type( * bt_ctf_stream_class_get and bt_ctf_stream_class_put: increment and * decrement the stream class' reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with stream class objects. + * * These functions ensure that the stream class won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-ir/stream-internal.h b/include/babeltrace/ctf-ir/stream-internal.h index 44ff774a..b0011ddf 100644 --- a/include/babeltrace/ctf-ir/stream-internal.h +++ b/include/babeltrace/ctf-ir/stream-internal.h @@ -28,7 +28,7 @@ */ #include -#include +#include #include #include #include @@ -37,7 +37,7 @@ #include struct bt_ctf_stream { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; /* Trace owning this stream. A stream does not own a trace. */ struct bt_ctf_trace *trace; uint32_t id; diff --git a/include/babeltrace/ctf-ir/stream.h b/include/babeltrace/ctf-ir/stream.h index 9e61d763..18f86a7c 100644 --- a/include/babeltrace/ctf-ir/stream.h +++ b/include/babeltrace/ctf-ir/stream.h @@ -219,6 +219,8 @@ extern int bt_ctf_stream_flush(struct bt_ctf_stream *stream); * bt_ctf_stream_get and bt_ctf_stream_put: increment and decrement the * stream's reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with stream objects. + * * These functions ensure that the stream won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-ir/trace-internal.h b/include/babeltrace/ctf-ir/trace-internal.h index 03433a4b..4a76c1a1 100644 --- a/include/babeltrace/ctf-ir/trace-internal.h +++ b/include/babeltrace/ctf-ir/trace-internal.h @@ -27,10 +27,10 @@ * SOFTWARE. */ -#include #include #include #include +#include #include #include #include @@ -48,7 +48,7 @@ enum field_type_alias { }; struct bt_ctf_trace { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; int frozen; uuid_t uuid; int byte_order; /* A value defined in Babeltrace's "endian.h" */ diff --git a/include/babeltrace/ctf-ir/trace.h b/include/babeltrace/ctf-ir/trace.h index 95381034..cc628a5b 100644 --- a/include/babeltrace/ctf-ir/trace.h +++ b/include/babeltrace/ctf-ir/trace.h @@ -346,6 +346,8 @@ extern int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, * bt_ctf_trace_get and bt_ctf_trace_put: increment and decrement the * trace's reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with trace objects. + * * These functions ensure that the trace won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-writer/writer-internal.h b/include/babeltrace/ctf-writer/writer-internal.h index 388ff0d1..6b1677ae 100644 --- a/include/babeltrace/ctf-writer/writer-internal.h +++ b/include/babeltrace/ctf-writer/writer-internal.h @@ -27,16 +27,16 @@ * SOFTWARE. */ -#include #include #include #include #include #include #include +#include struct bt_ctf_writer { - struct bt_ctf_ref ref_count; + struct bt_ctf_base base; int frozen; /* Protects attributes that can't be changed mid-trace */ struct bt_ctf_trace *trace; GString *path; diff --git a/include/babeltrace/ctf-writer/writer.h b/include/babeltrace/ctf-writer/writer.h index f189ab5c..1c94bcca 100644 --- a/include/babeltrace/ctf-writer/writer.h +++ b/include/babeltrace/ctf-writer/writer.h @@ -153,6 +153,8 @@ extern int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, * bt_ctf_writer_get and bt_ctf_writer_put: increment and decrement the * writer's reference count. * + * You may also use bt_ctf_get() and bt_ctf_put() with writer objects. + * * These functions ensure that the writer won't be destroyed while it * is in use. The same number of get and put (plus one extra put to * release the initial reference done at creation) have to be done to diff --git a/include/babeltrace/ctf-writer/ref-internal.h b/include/babeltrace/ref-internal.h similarity index 74% rename from include/babeltrace/ctf-writer/ref-internal.h rename to include/babeltrace/ref-internal.h index 1db4974b..a8eec330 100644 --- a/include/babeltrace/ctf-writer/ref-internal.h +++ b/include/babeltrace/ref-internal.h @@ -1,8 +1,8 @@ -#ifndef BABELTRACE_CTF_WRITER_REF_INTERNAL_H -#define BABELTRACE_CTF_WRITER_REF_INTERNAL_H +#ifndef BABELTRACE_REF_INTERNAL_H +#define BABELTRACE_REF_INTERNAL_H /* - * BabelTrace - CTF Writer: Reference count + * Babeltrace - reference counting * * Copyright 2013, 2014 Jérémie Galarneau * @@ -29,33 +29,39 @@ #include -struct bt_ctf_ref { +struct bt_ref; + +typedef void (*bt_ref_release_func_t)(struct bt_ref *); + +struct bt_ref { long refcount; + bt_ref_release_func_t release_func; }; static inline -void bt_ctf_ref_init(struct bt_ctf_ref *ref) +void bt_ref_init(struct bt_ref *ref, + bt_ref_release_func_t release_func) { assert(ref); ref->refcount = 1; + ref->release_func = release_func; } static inline -void bt_ctf_ref_get(struct bt_ctf_ref *ref) +void bt_ref_get(struct bt_ref *ref) { assert(ref); ref->refcount++; } static inline -void bt_ctf_ref_put(struct bt_ctf_ref *ref, - void (*release)(struct bt_ctf_ref *)) +void bt_ref_put(struct bt_ref *ref) { assert(ref); - assert(release); + assert(ref->release_func); if ((--ref->refcount) == 0) { - release(ref); + ref->release_func(ref); } } -#endif /* BABELTRACE_CTF_WRITER_REF_INTERNAL_H */ +#endif /* BABELTRACE_REF_INTERNAL_H */ diff --git a/lib/objects.c b/lib/objects.c index 4d31d2ee..8a0bc650 100644 --- a/lib/objects.c +++ b/lib/objects.c @@ -29,10 +29,10 @@ #include #include #include -#include +#include #include -#include #include +#include #define BT_OBJECT_FROM_CONCRETE(_concrete) ((struct bt_object *) (_concrete)) #define BT_OBJECT_TO_BOOL(_base) ((struct bt_object_bool *) (_base)) @@ -44,7 +44,7 @@ struct bt_object { enum bt_object_type type; - struct bt_ctf_ref ref_count; + struct bt_ref ref_count; bool is_frozen; }; @@ -86,6 +86,9 @@ struct bt_object_map { GHashTable *ght; }; +static +void bt_object_destroy(struct bt_ref *ref_count); + static void bt_object_string_destroy(struct bt_object *object) { @@ -435,7 +438,7 @@ void (* const freeze_funcs[])(struct bt_object *) = { }; static -void bt_object_destroy(struct bt_ctf_ref *ref_count) +void bt_object_destroy(struct bt_ref *ref_count) { struct bt_object *object; @@ -455,26 +458,18 @@ void bt_object_destroy(struct bt_ctf_ref *ref_count) void bt_object_get(struct bt_object *object) { - if (!object) { - goto skip; + if (object && !bt_object_is_null(object)) { + bt_ref_get(&object->ref_count); } - bt_ctf_ref_get(&object->ref_count); - -skip: return; } void bt_object_put(struct bt_object *object) { - if (!object) { - goto skip; + if (object && !bt_object_is_null(object)) { + bt_ref_put(&object->ref_count); } - - bt_ctf_ref_put(&object->ref_count, bt_object_destroy); - -skip: - return; } enum bt_object_status bt_object_freeze(struct bt_object *object) @@ -513,7 +508,7 @@ struct bt_object bt_object_create_base(enum bt_object_type type) base.type = type; base.is_frozen = false; - bt_ctf_ref_init(&base.ref_count); + bt_ref_init(&base.ref_count, bt_object_destroy); return base; } -- 2.34.1