From 6583e303561f7ddf045c617ee04a9a7519a0fc89 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Fri, 27 Dec 2024 18:22:02 -0800 Subject: [PATCH 1/4] First pass at full names for reactors --- core/reactor_common.c | 28 ++++++++++++++++++++++++++++ include/api/reaction_macros.h | 6 ++++++ include/api/reaction_macros_undef.h | 2 ++ include/core/lf_types.h | 3 +++ include/core/reactor.h | 10 ++++++++++ 5 files changed, 49 insertions(+) diff --git a/core/reactor_common.c b/core/reactor_common.c index 358480eb8..6063d9d27 100644 --- a/core/reactor_common.c +++ b/core/reactor_common.c @@ -149,6 +149,34 @@ void lf_set_stop_tag(environment_t* env, tag_t tag) { } } +const char* lf_reactor_full_name(self_base_t* self) { + if (self->full_name != NULL) { + return self->full_name; + } + // First find the length of the name. + size_t len = 0; + len += strlen(self->name); + self_base_t* parent = self->parent; + while (parent != NULL) { + len++; + len += strlen(parent->name); + parent = parent->parent; + } + self->full_name = (char*)lf_allocate(len + 1, sizeof(char), &self->allocations); + + size_t location = len - strlen(self->name); + strncpy(&self->full_name[location], self->name, strlen(self->name) + 1); + parent = self->parent; + while(parent != NULL) { + location--; + self->full_name[location] = '.'; + location -= strlen(parent->name); + strncpy(&self->full_name[location], parent->name, strlen(parent->name)); + parent = parent->parent; + } + return self->full_name; +} + #ifdef FEDERATED_DECENTRALIZED interval_t lf_get_stp_offset() { return lf_fed_STA_offset; } diff --git a/include/api/reaction_macros.h b/include/api/reaction_macros.h index 52c39e125..c019a5cff 100644 --- a/include/api/reaction_macros.h +++ b/include/api/reaction_macros.h @@ -199,4 +199,10 @@ */ #define lf_time_logical_elapsed() lf_time_logical_elapsed(self->base.environment) +/** + * @brief Return the fully qualified name of the reactor. + * @param reactor The reactor to get the name of. + */ +#define lf_reactor_full_name(reactor) lf_reactor_full_name(&reactor->base) + #endif // REACTION_MACROS_H diff --git a/include/api/reaction_macros_undef.h b/include/api/reaction_macros_undef.h index 601ea34d3..8055c0ca6 100644 --- a/include/api/reaction_macros_undef.h +++ b/include/api/reaction_macros_undef.h @@ -26,4 +26,6 @@ #undef lf_time_logical #undef lf_time_logical_elapsed +#undef lf_reactor_full_name + #endif // REACTION_MACROS_H diff --git a/include/core/lf_types.h b/include/core/lf_types.h index b6d754ca2..c7f8150ea 100644 --- a/include/core/lf_types.h +++ b/include/core/lf_types.h @@ -277,6 +277,9 @@ typedef struct self_base_t { struct allocation_record_t* allocations; struct reaction_t* executing_reaction; // The currently executing reaction of the reactor. environment_t* environment; + char* name; // The name of the reactor. If a bank, appended with [index]. + char* full_name; // The full name of the reactor or NULL if lf_reactor_full_name() is not called. + self_base_t* parent; // The parent of this reactor. #if !defined(LF_SINGLE_THREADED) void* reactor_mutex; // If not null, this is expected to point to an lf_mutex_t. // It is not declared as such to avoid a dependence on platform.h. diff --git a/include/core/reactor.h b/include/core/reactor.h index fffa9ba19..519ef3f61 100644 --- a/include/core/reactor.h +++ b/include/core/reactor.h @@ -128,5 +128,15 @@ void lf_free_all_reactors(void); */ void lf_free_reactor(self_base_t* self); +/** + * @brief Return the full name of the reactor. + * + * The full name includes the names of all the parent reactors, separated by dots. + * If any reactor is a bank, the name will have a suffix of the form `[bank_index]`. + * + * @param self The self struct of the reactor. + */ +const char* lf_reactor_full_name(self_base_t* self); + #endif /* REACTOR_H */ /** @} */ From 89bfac97e97b8709a90a9872464f31083da21677 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Fri, 27 Dec 2024 21:00:34 -0800 Subject: [PATCH 2/4] Format --- core/reactor_common.c | 2 +- include/core/lf_types.h | 4 ++-- include/core/reactor.h | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/reactor_common.c b/core/reactor_common.c index 6063d9d27..9fd04e356 100644 --- a/core/reactor_common.c +++ b/core/reactor_common.c @@ -167,7 +167,7 @@ const char* lf_reactor_full_name(self_base_t* self) { size_t location = len - strlen(self->name); strncpy(&self->full_name[location], self->name, strlen(self->name) + 1); parent = self->parent; - while(parent != NULL) { + while (parent != NULL) { location--; self->full_name[location] = '.'; location -= strlen(parent->name); diff --git a/include/core/lf_types.h b/include/core/lf_types.h index c7f8150ea..b0bf58b70 100644 --- a/include/core/lf_types.h +++ b/include/core/lf_types.h @@ -277,8 +277,8 @@ typedef struct self_base_t { struct allocation_record_t* allocations; struct reaction_t* executing_reaction; // The currently executing reaction of the reactor. environment_t* environment; - char* name; // The name of the reactor. If a bank, appended with [index]. - char* full_name; // The full name of the reactor or NULL if lf_reactor_full_name() is not called. + char* name; // The name of the reactor. If a bank, appended with [index]. + char* full_name; // The full name of the reactor or NULL if lf_reactor_full_name() is not called. self_base_t* parent; // The parent of this reactor. #if !defined(LF_SINGLE_THREADED) void* reactor_mutex; // If not null, this is expected to point to an lf_mutex_t. diff --git a/include/core/reactor.h b/include/core/reactor.h index 519ef3f61..256fa5c8c 100644 --- a/include/core/reactor.h +++ b/include/core/reactor.h @@ -130,10 +130,10 @@ void lf_free_reactor(self_base_t* self); /** * @brief Return the full name of the reactor. - * + * * The full name includes the names of all the parent reactors, separated by dots. * If any reactor is a bank, the name will have a suffix of the form `[bank_index]`. - * + * * @param self The self struct of the reactor. */ const char* lf_reactor_full_name(self_base_t* self); From 9162ff634ab416f2429ce3b20525c24f7b12b25f Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sat, 28 Dec 2024 08:29:17 -0800 Subject: [PATCH 3/4] Use memcpy rather than strncpy --- core/reactor_common.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/reactor_common.c b/core/reactor_common.c index 9fd04e356..d97b0a84d 100644 --- a/core/reactor_common.c +++ b/core/reactor_common.c @@ -153,25 +153,27 @@ const char* lf_reactor_full_name(self_base_t* self) { if (self->full_name != NULL) { return self->full_name; } - // First find the length of the name. - size_t len = 0; - len += strlen(self->name); + // First find the length of the full name. + size_t name_len = strlen(self->name); + size_t len = name_len; self_base_t* parent = self->parent; while (parent != NULL) { - len++; + len++; // For the dot. len += strlen(parent->name); parent = parent->parent; } self->full_name = (char*)lf_allocate(len + 1, sizeof(char), &self->allocations); + self->full_name[len] = '\0'; // Null terminate the string. - size_t location = len - strlen(self->name); - strncpy(&self->full_name[location], self->name, strlen(self->name) + 1); + size_t location = len - name_len; + memcpy(&self->full_name[location], self->name, name_len); parent = self->parent; while (parent != NULL) { location--; self->full_name[location] = '.'; - location -= strlen(parent->name); - strncpy(&self->full_name[location], parent->name, strlen(parent->name)); + size_t parent_len = strlen(parent->name); + location -= parent_len; + memcpy(&self->full_name[location], parent->name, parent_len); parent = parent->parent; } return self->full_name; From 6306c1196bb5e165dac8dd807c8d1cc01ba85754 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sat, 28 Dec 2024 17:23:47 -0800 Subject: [PATCH 4/4] Added lf_reactor_name function --- core/reactor_common.c | 2 ++ include/api/reaction_macros.h | 15 +++++++++++++++ include/api/reaction_macros_undef.h | 1 + include/core/lf_types.h | 4 ++-- include/core/reactor.h | 15 +++++++++++++-- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/reactor_common.c b/core/reactor_common.c index d97b0a84d..7257e5132 100644 --- a/core/reactor_common.c +++ b/core/reactor_common.c @@ -149,6 +149,8 @@ void lf_set_stop_tag(environment_t* env, tag_t tag) { } } +const char* lf_reactor_name(self_base_t* self) { return self->name; } + const char* lf_reactor_full_name(self_base_t* self) { if (self->full_name != NULL) { return self->full_name; diff --git a/include/api/reaction_macros.h b/include/api/reaction_macros.h index c019a5cff..c73cde530 100644 --- a/include/api/reaction_macros.h +++ b/include/api/reaction_macros.h @@ -199,8 +199,23 @@ */ #define lf_time_logical_elapsed() lf_time_logical_elapsed(self->base.environment) +/** + * @brief Return the instance name of the reactor. + * + * The instance name is the name of given to the instance created by the `new` operator in LF. + * If the instance is in a bank, then the name will have a suffix of the form `[bank_index]`. + * + * @param reactor The reactor to get the name of. + */ +#define lf_reactor_name(reactor) lf_reactor_name(&reactor->base) + /** * @brief Return the fully qualified name of the reactor. + * + * The fully qualified name of a reactor is the instance name of the reactor concatenated with the names of all + * of its parents, separated by dots. If the reactor or any of its parents is a bank, then the name + * will have a suffix of the form `[bank_index]`. + * * @param reactor The reactor to get the name of. */ #define lf_reactor_full_name(reactor) lf_reactor_full_name(&reactor->base) diff --git a/include/api/reaction_macros_undef.h b/include/api/reaction_macros_undef.h index 8055c0ca6..26ab86b2e 100644 --- a/include/api/reaction_macros_undef.h +++ b/include/api/reaction_macros_undef.h @@ -26,6 +26,7 @@ #undef lf_time_logical #undef lf_time_logical_elapsed +#undef lf_reactor_name #undef lf_reactor_full_name #endif // REACTION_MACROS_H diff --git a/include/core/lf_types.h b/include/core/lf_types.h index b0bf58b70..385fc654e 100644 --- a/include/core/lf_types.h +++ b/include/core/lf_types.h @@ -251,8 +251,8 @@ struct trigger_t { * An allocation record that is used by a destructor for a reactor * to free memory that has been dynamically allocated for the particular * instance of the reactor. This will be an element of linked list. - * If the indirect field is true, then the allocated pointer points to - * pointer to allocated memory, rather than directly to the allocated memory. + * The `allocated` pointer points to the allocated memory, and the `next` + * pointer points to the next allocation record (or NULL if there are no more). */ typedef struct allocation_record_t { void* allocated; diff --git a/include/core/reactor.h b/include/core/reactor.h index 256fa5c8c..c98f2e93b 100644 --- a/include/core/reactor.h +++ b/include/core/reactor.h @@ -128,11 +128,22 @@ void lf_free_all_reactors(void); */ void lf_free_reactor(self_base_t* self); +/** + * @brief Return the instance name of the reactor. + * + * The instance name is the name of given to the instance created by the `new` operator in LF. + * If the instance is in a bank, then the name will have a suffix of the form `[bank_index]`. + * + * @param self The self struct of the reactor. + */ +const char* lf_reactor_name(self_base_t* self); + /** * @brief Return the full name of the reactor. * - * The full name includes the names of all the parent reactors, separated by dots. - * If any reactor is a bank, the name will have a suffix of the form `[bank_index]`. + * The fully qualified name of a reactor is the instance name of the reactor concatenated with the names of all + * of its parents, separated by dots. If the reactor or any of its parents is a bank, then the name + * will have a suffix of the form `[bank_index]`. * * @param self The self struct of the reactor. */