Skip to content

Commit 1965c11

Browse files
Steven Cartmelladbridge
authored andcommitted
Replace runtime strip_path function with compiler intrinsic equivalents
Sleep manager tracing strips the path from filenames and uses the result as an identifier to track drivers that unlock/lock sleep tracing. Replace the function that strips the path from the string, replace this function with a new macro, __FILENAME__ which performs the same action in a compiler specific manner. - GCC_ARM, use __builtin_strrchr which is optimized out at compile time. - ARM, use __MODULE__ which returns the filename without path. - IAR, specifiy the --no_path_in_file_macros compiler flag.
1 parent 0b866b7 commit 1965c11

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

hal/mbed_sleep_manager.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ typedef struct sleep_statistic {
4242

4343
static sleep_statistic_t sleep_stats[STATISTIC_COUNT];
4444

45-
static const char* strip_path(const char* const filename)
46-
{
47-
char *output = strrchr(filename, '/');
48-
49-
if (output != NULL) {
50-
return output + 1;
51-
}
52-
53-
output = strrchr(filename, '\\');
54-
55-
if (output != NULL) {
56-
return output + 1;
57-
}
58-
59-
return filename;
60-
}
61-
6245
static sleep_statistic_t* sleep_tracker_find(const char *const filename)
6346
{
6447
char temp[IDENTIFIER_WIDTH];
@@ -115,34 +98,31 @@ static void sleep_tracker_print_stats(void)
11598

11699
void sleep_tracker_lock(const char* const filename, int line)
117100
{
118-
const char* const stripped_path = strip_path(filename);
119-
120-
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
101+
sleep_statistic_t *stat = sleep_tracker_find(filename);
121102

122103
// Entry for this driver does not exist, create one.
123104
if (stat == NULL) {
124-
stat = sleep_tracker_add(stripped_path);
105+
stat = sleep_tracker_add(filename);
125106
}
126107

127108
core_util_atomic_incr_u8(&stat->count, 1);
128109

129-
debug("LOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
110+
debug("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
130111
}
131112

132113
void sleep_tracker_unlock(const char* const filename, int line)
133114
{
134-
const char* const stripped_path = strip_path(filename);
135-
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
115+
sleep_statistic_t *stat = sleep_tracker_find(filename);
136116

137117
// Entry for this driver does not exist, something went wrong.
138118
if (stat == NULL) {
139-
debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", stripped_path, line);
119+
debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", filename, line);
140120
return;
141121
}
142122

143123
core_util_atomic_decr_u8(&stat->count, 1);
144124

145-
debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
125+
debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
146126
}
147127

148128
#endif // MBED_SLEEP_TRACING_ENABLED

platform/mbed_power_mgmt.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,26 @@ extern "C" {
6868
void sleep_tracker_lock(const char *const filename, int line);
6969
void sleep_tracker_unlock(const char *const filename, int line);
7070

71-
#define sleep_manager_lock_deep_sleep() \
72-
do \
73-
{ \
74-
sleep_manager_lock_deep_sleep_internal(); \
75-
sleep_tracker_lock(__FILE__, __LINE__); \
76-
} while (0);
71+
#if defined(__CC_ARM) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
72+
#define __FILENAME__ __MODULE__
73+
#elif defined(__GNUC__)
74+
#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __builtin_strrchr(__FILE__, '\\') ? __builtin_strrchr(__FILE__, '\\') + 1 : __FILE__)
75+
#else
76+
#define __FILENAME__ __FILE__
77+
#endif
7778

78-
#define sleep_manager_unlock_deep_sleep() \
79+
#define sleep_manager_lock_deep_sleep() \
7980
do \
8081
{ \
81-
sleep_manager_unlock_deep_sleep_internal(); \
82-
sleep_tracker_unlock(__FILE__, __LINE__); \
82+
sleep_manager_lock_deep_sleep_internal(); \
83+
sleep_tracker_lock(__FILENAME__, __LINE__); \
84+
} while (0);
85+
86+
#define sleep_manager_unlock_deep_sleep() \
87+
do \
88+
{ \
89+
sleep_manager_unlock_deep_sleep_internal(); \
90+
sleep_tracker_unlock(__FILENAME__, __LINE__); \
8391
} while (0);
8492

8593
#else
@@ -121,7 +129,6 @@ void sleep_manager_unlock_deep_sleep_internal(void);
121129
*/
122130
bool sleep_manager_can_deep_sleep(void);
123131

124-
125132
/** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
126133
* on the deepsleep locking counter
127134
*

tools/profiles/debug.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"common": [
5252
"--no_wrap_diagnostics", "-e",
5353
"--diag_suppress=Pa050,Pa084,Pa093,Pa082", "-On", "-r", "-DMBED_DEBUG",
54-
"-DMBED_TRAP_ERRORS_ENABLED=1", "--enable_restrict"],
54+
"-DMBED_TRAP_ERRORS_ENABLED=1", "--enable_restrict",
55+
"--no_path_in_file_macros"],
5556
"asm": [],
5657
"c": ["--vla"],
5758
"cxx": ["--guard_calls", "--no_static_destruction"],

tools/profiles/develop.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"IAR": {
4747
"common": [
4848
"--no_wrap_diagnostics", "-e",
49-
"--diag_suppress=Pa050,Pa084,Pa093,Pa082", "-Oh", "--enable_restrict"],
49+
"--diag_suppress=Pa050,Pa084,Pa093,Pa082", "-Oh", "--enable_restrict",
50+
"--no_path_in_file_macros"],
5051
"asm": [],
5152
"c": ["--vla"],
5253
"cxx": ["--guard_calls", "--no_static_destruction"],

0 commit comments

Comments
 (0)