Skip to content

Commit c023534

Browse files
committed
target: use list for target events
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
1 parent 04124c7 commit c023534

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/target/target.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#endif
3232

3333
#include <helper/align.h>
34+
#include <helper/list.h>
3435
#include <helper/nvp.h>
3536
#include <helper/time_support.h>
3637
#include <jtag/jtag.h>
@@ -52,6 +53,13 @@
5253
/* default halt wait timeout (ms) */
5354
#define DEFAULT_HALT_TIMEOUT 5000
5455

56+
struct target_event_action {
57+
enum target_event event;
58+
Jim_Interp *interp;
59+
Jim_Obj *body;
60+
struct list_head list;
61+
};
62+
5563
static int target_read_buffer_default(struct target *target, target_addr_t address,
5664
uint32_t count, uint8_t *buffer);
5765
static int target_write_buffer_default(struct target *target, target_addr_t address,
@@ -2194,12 +2202,11 @@ static void target_destroy(struct target *target)
21942202

21952203
jtag_unregister_event_callback(jtag_enable_callback, target);
21962204

2197-
struct target_event_action *teap = target->event_action;
2198-
while (teap) {
2199-
struct target_event_action *next = teap->next;
2205+
struct target_event_action *teap, *temp;
2206+
list_for_each_entry_safe(teap, temp, &target->events_action, list) {
2207+
list_del(&teap->list);
22002208
Jim_DecrRefCount(teap->interp, teap->body);
22012209
free(teap);
2202-
teap = next;
22032210
}
22042211

22052212
target_free_all_working_areas(target);
@@ -4663,7 +4670,7 @@ void target_handle_event(struct target *target, enum target_event e)
46634670
struct target_event_action *teap;
46644671
int retval;
46654672

4666-
for (teap = target->event_action; teap; teap = teap->next) {
4673+
list_for_each_entry(teap, &target->events_action, list) {
46674674
if (teap->event == e) {
46684675
LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
46694676
target_name(target),
@@ -4826,7 +4833,7 @@ bool target_has_event_action(const struct target *target, enum target_event even
48264833
{
48274834
struct target_event_action *teap;
48284835

4829-
for (teap = target->event_action; teap; teap = teap->next) {
4836+
list_for_each_entry(teap, &target->events_action, list) {
48304837
if (teap->event == event)
48314838
return true;
48324839
}
@@ -4946,13 +4953,14 @@ static int target_configure(struct jim_getopt_info *goi, struct target *target)
49464953
{
49474954
struct target_event_action *teap;
49484955

4949-
teap = target->event_action;
49504956
/* replace existing? */
4951-
while (teap) {
4957+
list_for_each_entry(teap, &target->events_action, list)
49524958
if (teap->event == (enum target_event)n->value)
49534959
break;
4954-
teap = teap->next;
4955-
}
4960+
4961+
/* not found! */
4962+
if (&teap->list == &target->events_action)
4963+
teap = NULL;
49564964

49574965
if (goi->is_configure) {
49584966
/* START_DEPRECATED_TPIU */
@@ -4986,8 +4994,7 @@ static int target_configure(struct jim_getopt_info *goi, struct target *target)
49864994

49874995
if (!replace) {
49884996
/* add to head of event list */
4989-
teap->next = target->event_action;
4990-
target->event_action = teap;
4997+
list_add(&teap->list, &target->events_action);
49914998
}
49924999
Jim_SetEmptyResult(goi->interp);
49935000
} else {
@@ -5402,19 +5409,19 @@ COMMAND_HANDLER(handle_target_wait_state)
54025409
COMMAND_HANDLER(handle_target_event_list)
54035410
{
54045411
struct target *target = get_current_target(CMD_CTX);
5405-
struct target_event_action *teap = target->event_action;
5412+
struct target_event_action *teap;
54065413

54075414
command_print(CMD, "Event actions for target %s\n",
54085415
target_name(target));
54095416
command_print(CMD, "%-25s | Body", "Event");
54105417
command_print(CMD, "------------------------- | "
54115418
"----------------------------------------");
5412-
while (teap) {
5419+
5420+
list_for_each_entry(teap, &target->events_action, list)
54135421
command_print(CMD, "%-25s | %s",
54145422
target_event_name(teap->event),
54155423
Jim_GetString(teap->body, NULL));
5416-
teap = teap->next;
5417-
}
5424+
54185425
command_print(CMD, "***END***");
54195426
return ERROR_OK;
54205427
}
@@ -5767,6 +5774,8 @@ static int target_create(struct jim_getopt_info *goi)
57675774

57685775
target->halt_issued = false;
57695776

5777+
INIT_LIST_HEAD(&target->events_action);
5778+
57705779
/* initialize trace information */
57715780
target->trace_info = calloc(1, sizeof(struct trace));
57725781
if (!target->trace_info) {

src/target/target.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct target {
139139
*/
140140
bool running_alg;
141141

142-
struct target_event_action *event_action;
142+
struct list_head events_action;
143143

144144
bool reset_halt; /* attempt resetting the CPU into the halted mode? */
145145
target_addr_t working_area; /* working area (initialised RAM). Evaluated
@@ -295,13 +295,6 @@ enum target_event {
295295
TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107 = 0x107,
296296
};
297297

298-
struct target_event_action {
299-
enum target_event event;
300-
Jim_Interp *interp;
301-
Jim_Obj *body;
302-
struct target_event_action *next;
303-
};
304-
305298
bool target_has_event_action(const struct target *target, enum target_event event);
306299

307300
struct target_event_callback {

0 commit comments

Comments
 (0)