// Cached system property lookup. For code that needs to read the same property multiple times, // this class helps optimize those lookups. class CachedProperty { public: // The lifetime of `property_name` must be greater than that of this CachedProperty. explicit CachedProperty(constchar* property_name)
: property_name_(property_name),
prop_info_(nullptr),
cached_area_serial_(0),
cached_property_serial_(0),
is_read_only_(strncmp(property_name, "ro.", 3) == 0),
read_only_property_(nullptr) {
cached_value_[0] = '\0';
}
// Returns true if the property has been updated (based on the serial rather than the value) // since the last call to Get. bool DidChange() {
uint32_t initial_property_serial_ = cached_property_serial_;
Get(); return (cached_property_serial_ != initial_property_serial_);
}
// Returns the current value of the underlying system property as cheaply as possible. // The returned pointer is valid until the next call to Get. It is the caller's responsibility // to provide a lock for thread-safety. constchar* Get() { // Do we have a `struct prop_info` yet? if (prop_info_ == nullptr) { // `__system_property_find` is expensive, so only retry if a property // has been created since last time we checked.
uint32_t property_area_serial = __system_property_area_serial(); if (property_area_serial != cached_area_serial_) {
prop_info_ = __system_property_find(property_name_);
cached_area_serial_ = property_area_serial;
}
}
if (prop_info_ != nullptr) { // Only bother re-reading the property if it's actually changed since last time.
uint32_t property_serial = __system_property_serial(prop_info_); if (property_serial != cached_property_serial_) {
__system_property_read_callback(prop_info_, &CachedProperty::Callback, this);
}
} if (is_read_only_ && read_only_property_ != nullptr) { return read_only_property_;
} return cached_value_;
}
staticvoid Callback(void* data, constchar*, constchar* value, uint32_t serial) {
CachedProperty* instance = reinterpret_cast<CachedProperty*>(data);
instance->cached_property_serial_ = serial; // Read only properties can be larger than PROP_VALUE_MAX, but also never change value or // location, thus we return the pointer from the shared memory directly. if (instance->is_read_only_) {
instance->read_only_property_ = value;
} else {
strlcpy(instance->cached_value_, value, PROP_VALUE_MAX);
}
}
};
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.