From abcc74facc86124042abdb9fd4391b6eadda2b9e Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Thu, 28 Jun 2018 09:47:51 -0400 Subject: [PATCH] Add lttng_event copy constructor MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Since the lttng_event structure now has an extended_ptr to store additional data, such as a userspace probe location, we can't simply memcpy the struct to duplicate it. This commit introduces a lttng_event_copy function that allocates a new lttng_event struct and copies the content of both the struct and its extended_ptr. Signed-off-by: Francis Deslauriers Signed-off-by: Jérémie Galarneau --- include/lttng/event.h | 2 ++ src/lib/lttng-ctl/event.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/lttng/event.h b/include/lttng/event.h index 0131defcc..548ec2a46 100644 --- a/include/lttng/event.h +++ b/include/lttng/event.h @@ -304,6 +304,8 @@ extern int lttng_list_events(struct lttng_handle *handle, extern struct lttng_event *lttng_event_create(void); +extern struct lttng_event *lttng_event_copy(const struct lttng_event *event); + extern void lttng_event_destroy(struct lttng_event *event); /* diff --git a/src/lib/lttng-ctl/event.c b/src/lib/lttng-ctl/event.c index 9413b7052..046028cd2 100644 --- a/src/lib/lttng-ctl/event.c +++ b/src/lib/lttng-ctl/event.c @@ -54,6 +54,38 @@ error: goto end; } +struct lttng_event *lttng_event_copy(const struct lttng_event *event) +{ + struct lttng_event *new_event; + struct lttng_event_extended *new_event_extended; + + new_event = zmalloc(sizeof(*event)); + if (!event) { + PERROR("Error allocating event structure"); + goto end; + } + + /* Copy the content of the old event. */ + memcpy(new_event, event, sizeof(*event)); + + /* + * We need to create a new extended since the previous pointer is now + * invalid. + */ + new_event_extended = zmalloc(sizeof(*new_event_extended)); + if (!new_event_extended) { + PERROR("Error allocating event extended structure"); + goto error; + } + + new_event->extended.ptr = new_event_extended; +end: + return new_event; +error: + free(event); + goto end; +} + void lttng_event_destroy(struct lttng_event *event) { struct lttng_event_extended *event_extended; -- 2.34.1