Skip to content

Commit fed5115

Browse files
author
Steven Cartmell
committed
Return pointer to sleep tracker array element instead of an index
1 parent 076548a commit fed5115

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

hal/mbed_sleep_manager.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static const char* strip_path(const char* const filename)
6262
return filename;
6363
}
6464

65-
static size_t sleep_tracker_find_index(const char *const filename)
65+
static sleep_statistic_t* sleep_tracker_find(const char *const filename)
6666
{
6767
char temp[IDENTIFIER_WIDTH];
6868
strncpy(temp, filename, IDENTIFIER_WIDTH);
@@ -71,14 +71,14 @@ static size_t sleep_tracker_find_index(const char *const filename)
7171
// Search for the a driver matching the current name and return it's index
7272
for (int i = 0; i < STATISTIC_COUNT; ++i) {
7373
if (strcmp(sleep_stats[i].identifier, temp) == 0) {
74-
return i;
74+
return &sleep_stats[i];
7575
}
7676
}
7777

78-
return -1;
78+
return NULL;
7979
}
8080

81-
static size_t sleep_tracker_add(const char* const filename)
81+
static sleep_statistic_t* sleep_tracker_add(const char* const filename)
8282
{
8383
char temp[IDENTIFIER_WIDTH];
8484
strncpy(temp, filename, IDENTIFIER_WIDTH);
@@ -90,14 +90,14 @@ static size_t sleep_tracker_add(const char* const filename)
9090
strncpy(sleep_stats[i].identifier, temp, sizeof(temp));
9191
core_util_critical_section_exit();
9292

93-
return i;
93+
return &sleep_stats[i];
9494
}
9595
}
9696

9797
// Panic if there are no free indexes left to track with
9898
error("No free indexes left to use in mbed stats tracker");
9999

100-
return -1;
100+
return NULL;
101101
}
102102

103103
static void sleep_tracker_print_stats(void)
@@ -120,29 +120,29 @@ void sleep_tracker_lock(const char* const filename, int line)
120120
{
121121
const char* const stripped_path = strip_path(filename);
122122

123-
size_t index = sleep_tracker_find_index(stripped_path);
123+
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
124124

125125
// Entry for this driver does not exist, create one.
126-
if (index == -1) {
127-
index = sleep_tracker_add(filename);
126+
if (stat == NULL) {
127+
stat = sleep_tracker_add(filename);
128128
}
129129

130-
core_util_atomic_incr_u8(&sleep_stats[index].count, 1);
130+
core_util_atomic_incr_u8(&stat->count, 1);
131131

132132
printf("LOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
133133
}
134134

135135
void sleep_tracker_unlock(const char* const filename, int line)
136136
{
137137
const char* const stripped_path = strip_path(filename);
138-
size_t index = sleep_tracker_find_index(stripped_path);
138+
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
139139

140140
// Entry for this driver does not exist, something went wrong.
141-
if (index == -1) {
141+
if (stat == NULL) {
142142
error("Unlocking sleep for driver that was not previously locked.");
143143
}
144144

145-
core_util_atomic_decr_u8(&sleep_stats[index].count, 1);
145+
core_util_atomic_decr_u8(&stat->count, 1);
146146

147147
printf("UNLOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
148148
sleep_tracker_print_stats();

0 commit comments

Comments
 (0)