Skip to content

Commit 9a0e879

Browse files
author
Steven Cartmell
committed
Refactor sleep tracing driver identifier to be pointer to the driver filepath.
The use of __FILE__ macro to get a usable identifier from the driver path causes the path of the file to be stored in the .text region of the binary. Given that this remains for the entire duration of the program, storing a pointer to this string as an identifier is more efficient than copying the contents of the string during lookup/insertion.
1 parent bd63f93 commit 9a0e879

File tree

1 file changed

+6
-19
lines changed

1 file changed

+6
-19
lines changed

hal/mbed_sleep_manager.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,20 @@ static uint16_t deep_sleep_lock = 0U;
3030

3131
#ifdef MBED_SLEEP_TRACING_ENABLED
3232

33-
// Length of the identifier extracted from the driver name to store for logging.
34-
#define IDENTIFIER_WIDTH 15
3533
// Number of drivers that can be stored in the structure
3634
#define STATISTIC_COUNT 10
3735

3836
typedef struct sleep_statistic {
39-
char identifier[IDENTIFIER_WIDTH];
37+
const char* identifier;
4038
uint8_t count;
4139
} sleep_statistic_t;
4240

4341
static sleep_statistic_t sleep_stats[STATISTIC_COUNT];
4442

4543
static sleep_statistic_t* sleep_tracker_find(const char *const filename)
4644
{
47-
char temp[IDENTIFIER_WIDTH];
48-
strncpy(temp, filename, IDENTIFIER_WIDTH);
49-
temp[IDENTIFIER_WIDTH - 1] = '\0';
50-
51-
// Search for the a driver matching the current name and return it's index
5245
for (int i = 0; i < STATISTIC_COUNT; ++i) {
53-
if (strcmp(sleep_stats[i].identifier, temp) == 0) {
46+
if (sleep_stats[i].identifier == filename) {
5447
return &sleep_stats[i];
5548
}
5649
}
@@ -60,15 +53,9 @@ static sleep_statistic_t* sleep_tracker_find(const char *const filename)
6053

6154
static sleep_statistic_t* sleep_tracker_add(const char* const filename)
6255
{
63-
char temp[IDENTIFIER_WIDTH];
64-
strncpy(temp, filename, IDENTIFIER_WIDTH);
65-
temp[IDENTIFIER_WIDTH - 1] = '\0';
66-
6756
for (int i = 0; i < STATISTIC_COUNT; ++i) {
68-
if (sleep_stats[i].identifier[0] == '\0') {
69-
core_util_critical_section_enter();
70-
strncpy(sleep_stats[i].identifier, temp, sizeof(temp));
71-
core_util_critical_section_exit();
57+
if (sleep_stats[i].identifier == NULL) {
58+
sleep_stats[i].identifier = filename;
7259

7360
return &sleep_stats[i];
7461
}
@@ -87,12 +74,12 @@ static void sleep_tracker_print_stats(void)
8774
continue;
8875
}
8976

90-
if (sleep_stats[i].identifier[0] == '\0') {
77+
if (sleep_stats[i].identifier == NULL) {
9178
return;
9279
}
9380

9481
debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier,
95-
sleep_stats[i].count);
82+
sleep_stats[i].count);
9683
}
9784
}
9885

0 commit comments

Comments
 (0)