]> www.infradead.org Git - users/borneoa/openocd-next.git/commitdiff
target: use list for target events
authorAntonio Borneo <borneo.antonio@gmail.com>
Sun, 23 Mar 2025 11:09:28 +0000 (12:09 +0100)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 5 Apr 2025 06:23:37 +0000 (06:23 +0000)
To simplify removing an event when it's set to an empty string,
switch event list from hardcoded simply linked list to helper's
double linked list.

While there, move the declaration of struct target_event_action
in 'target.c' as it is not anymore visible outside.

Change-Id: I799754c80055dc6d22db55aca483757e833714ff
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8813
Tested-by: jenkins
src/target/target.c
src/target/target.h

index 3b62e0db09a6cb8e290f90929f0bdd80289e5288..53850bf28827057efe3605b0bebf2ae1bfe0b590 100644 (file)
@@ -31,6 +31,7 @@
 #endif
 
 #include <helper/align.h>
+#include <helper/list.h>
 #include <helper/nvp.h>
 #include <helper/time_support.h>
 #include <jtag/jtag.h>
 /* default halt wait timeout (ms) */
 #define DEFAULT_HALT_TIMEOUT 5000
 
+struct target_event_action {
+       enum target_event event;
+       Jim_Interp *interp;
+       Jim_Obj *body;
+       struct list_head list;
+};
+
 static int target_read_buffer_default(struct target *target, target_addr_t address,
                uint32_t count, uint8_t *buffer);
 static int target_write_buffer_default(struct target *target, target_addr_t address,
@@ -2194,12 +2202,11 @@ static void target_destroy(struct target *target)
 
        jtag_unregister_event_callback(jtag_enable_callback, target);
 
-       struct target_event_action *teap = target->event_action;
-       while (teap) {
-               struct target_event_action *next = teap->next;
+       struct target_event_action *teap, *temp;
+       list_for_each_entry_safe(teap, temp, &target->events_action, list) {
+               list_del(&teap->list);
                Jim_DecrRefCount(teap->interp, teap->body);
                free(teap);
-               teap = next;
        }
 
        target_free_all_working_areas(target);
@@ -4663,7 +4670,7 @@ void target_handle_event(struct target *target, enum target_event e)
        struct target_event_action *teap;
        int retval;
 
-       for (teap = target->event_action; teap; teap = teap->next) {
+       list_for_each_entry(teap, &target->events_action, list) {
                if (teap->event == e) {
                        LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
                                           target_name(target),
@@ -4826,7 +4833,7 @@ bool target_has_event_action(const struct target *target, enum target_event even
 {
        struct target_event_action *teap;
 
-       for (teap = target->event_action; teap; teap = teap->next) {
+       list_for_each_entry(teap, &target->events_action, list) {
                if (teap->event == event)
                        return true;
        }
@@ -4946,13 +4953,14 @@ no_params:
                        {
                                struct target_event_action *teap;
 
-                               teap = target->event_action;
                                /* replace existing? */
-                               while (teap) {
+                               list_for_each_entry(teap, &target->events_action, list)
                                        if (teap->event == (enum target_event)n->value)
                                                break;
-                                       teap = teap->next;
-                               }
+
+                               /* not found! */
+                               if (&teap->list == &target->events_action)
+                                       teap = NULL;
 
                                if (goi->is_configure) {
                                        /* START_DEPRECATED_TPIU */
@@ -4986,8 +4994,7 @@ no_params:
 
                                        if (!replace) {
                                                /* add to head of event list */
-                                               teap->next = target->event_action;
-                                               target->event_action = teap;
+                                               list_add(&teap->list, &target->events_action);
                                        }
                                        Jim_SetEmptyResult(goi->interp);
                                } else {
@@ -5402,19 +5409,19 @@ COMMAND_HANDLER(handle_target_wait_state)
 COMMAND_HANDLER(handle_target_event_list)
 {
        struct target *target = get_current_target(CMD_CTX);
-       struct target_event_action *teap = target->event_action;
+       struct target_event_action *teap;
 
        command_print(CMD, "Event actions for target %s\n",
                                   target_name(target));
        command_print(CMD, "%-25s | Body", "Event");
        command_print(CMD, "------------------------- | "
                        "----------------------------------------");
-       while (teap) {
+
+       list_for_each_entry(teap, &target->events_action, list)
                command_print(CMD, "%-25s | %s",
                                target_event_name(teap->event),
                                Jim_GetString(teap->body, NULL));
-               teap = teap->next;
-       }
+
        command_print(CMD, "***END***");
        return ERROR_OK;
 }
@@ -5767,6 +5774,8 @@ static int target_create(struct jim_getopt_info *goi)
 
        target->halt_issued                     = false;
 
+       INIT_LIST_HEAD(&target->events_action);
+
        /* initialize trace information */
        target->trace_info = calloc(1, sizeof(struct trace));
        if (!target->trace_info) {
index 47f02ec266625008ded4f3f06f865e1530e95c8a..b698f250ced2f48309dc5647c2694e765ae70347 100644 (file)
@@ -139,7 +139,7 @@ struct target {
         */
        bool running_alg;
 
-       struct target_event_action *event_action;
+       struct list_head events_action;
 
        bool reset_halt;                                                /* attempt resetting the CPU into the halted mode? */
        target_addr_t working_area;                             /* working area (initialised RAM). Evaluated
@@ -295,13 +295,6 @@ enum target_event {
        TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107 = 0x107,
 };
 
-struct target_event_action {
-       enum target_event event;
-       Jim_Interp *interp;
-       Jim_Obj *body;
-       struct target_event_action *next;
-};
-
 bool target_has_event_action(const struct target *target, enum target_event event);
 
 struct target_event_callback {