mirror of
https://github.com/esphome/esphome.git
synced 2026-09-14 00:28:39 +00:00
[scheduler] Address review feedback (const void *, %p cast, buffer size)
This commit is contained in:
@@ -35,7 +35,9 @@ static constexpr uint32_t MAX_INTERVAL_DELAY = 5000;
|
||||
// Uses a stack buffer to avoid heap allocation
|
||||
// Uses ESPHOME_snprintf_P/ESPHOME_PSTR for ESP8266 to keep format strings in flash
|
||||
struct SchedulerNameLog {
|
||||
char buffer[20]; // Enough for "id:4294967295" or "hash:0xFFFFFFFF" or "(null)"
|
||||
// Sized for the widest formatted output: "self:0x" + 16 hex digits (64-bit pointer) + nul.
|
||||
// Also covers "id:4294967295", "hash:0xFFFFFFFF", "iid:4294967295", "(null)".
|
||||
char buffer[28];
|
||||
|
||||
// Format a scheduler item name for logging
|
||||
// Returns pointer to formatted string (either static_name or internal buffer)
|
||||
@@ -58,7 +60,8 @@ struct SchedulerNameLog {
|
||||
return buffer;
|
||||
} else { // SELF_POINTER
|
||||
// static_name carries the void* key for SELF_POINTER (pointer-width union slot).
|
||||
ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("self:%p"), static_name);
|
||||
// Cast to const void* — %p requires a void* argument.
|
||||
ESPHOME_snprintf_P(buffer, sizeof(buffer), ESPHOME_PSTR("self:%p"), static_cast<const void *>(static_name));
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
@@ -301,19 +304,19 @@ bool HOT Scheduler::cancel_interval(Component *component, uint32_t id) {
|
||||
// passed through the existing static_name pointer slot. Matching is by raw pointer equality
|
||||
// (see matches_item_locked_'s SELF_POINTER branch). No Component pointer is stored, so
|
||||
// is_failed() skip and component-based log attribution don't apply.
|
||||
void HOT Scheduler::set_timeout(void *self, uint32_t timeout, std::function<void()> &&func) {
|
||||
void HOT Scheduler::set_timeout(const void *self, uint32_t timeout, std::function<void()> &&func) {
|
||||
this->set_timer_common_(nullptr, SchedulerItem::TIMEOUT, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
|
||||
timeout, std::move(func));
|
||||
}
|
||||
void HOT Scheduler::set_interval(void *self, uint32_t interval, std::function<void()> &&func) {
|
||||
void HOT Scheduler::set_interval(const void *self, uint32_t interval, std::function<void()> &&func) {
|
||||
this->set_timer_common_(nullptr, SchedulerItem::INTERVAL, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
|
||||
interval, std::move(func));
|
||||
}
|
||||
bool HOT Scheduler::cancel_timeout(void *self) {
|
||||
bool HOT Scheduler::cancel_timeout(const void *self) {
|
||||
return this->cancel_item_(nullptr, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
|
||||
SchedulerItem::TIMEOUT);
|
||||
}
|
||||
bool HOT Scheduler::cancel_interval(void *self) {
|
||||
bool HOT Scheduler::cancel_interval(const void *self) {
|
||||
return this->cancel_item_(nullptr, NameType::SELF_POINTER, static_cast<const char *>(self), 0,
|
||||
SchedulerItem::INTERVAL);
|
||||
}
|
||||
|
||||
@@ -168,11 +168,11 @@ class Scheduler {
|
||||
*
|
||||
* If you need either of those, use the existing `(Component *, id)` overloads.
|
||||
*/
|
||||
void set_timeout(void *self, uint32_t timeout, std::function<void()> &&func);
|
||||
/// Self-keyed interval. See set_timeout(void *, ...) for semantics.
|
||||
void set_interval(void *self, uint32_t interval, std::function<void()> &&func);
|
||||
bool cancel_timeout(void *self);
|
||||
bool cancel_interval(void *self);
|
||||
void set_timeout(const void *self, uint32_t timeout, std::function<void()> &&func);
|
||||
/// Self-keyed interval. See set_timeout(const void *, ...) for semantics.
|
||||
void set_interval(const void *self, uint32_t interval, std::function<void()> &&func);
|
||||
bool cancel_timeout(const void *self);
|
||||
bool cancel_interval(const void *self);
|
||||
|
||||
protected:
|
||||
struct SchedulerItem {
|
||||
|
||||
@@ -21,8 +21,10 @@ script:
|
||||
then:
|
||||
- logger.log: "Testing self-keyed scheduler API"
|
||||
- lambda: |-
|
||||
// Two distinct heap-allocated keys - they must not collide
|
||||
// even though both are self-keyed and share no Component pointer.
|
||||
// Two distinct keys backed by addresses of static markers — they
|
||||
// must not collide even though both are self-keyed and share no
|
||||
// Component pointer. Static storage gives them stable, unique
|
||||
// addresses for the lifetime of the program.
|
||||
static int key_a_marker = 0;
|
||||
static int key_b_marker = 0;
|
||||
void *key_a = &key_a_marker;
|
||||
|
||||
Reference in New Issue
Block a user