From 577418403d68e663fb33c7b0c8a90d862d9c00cf Mon Sep 17 00:00:00 2001 From: Greg Hackmann Date: Tue, 12 Feb 2013 16:39:31 -0800 Subject: [PATCH] bionic: add __system_property_foreach find_nth() will be inefficient on a trie. Since find_nth() is only used internally and only for enumerating properties, we can add a foreach() function to do this directly. Change-Id: I66bde9926c193073d74b244cce9fffd52108fff8 Signed-off-by: Greg Hackmann --- libc/bionic/system_properties.c | 16 ++++++++++++++++ libc/include/sys/system_properties.h | 19 ++++++++++++++++--- tests/system_properties_test.cpp | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/libc/bionic/system_properties.c b/libc/bionic/system_properties.c index d96950709..5197ef3ff 100644 --- a/libc/bionic/system_properties.c +++ b/libc/bionic/system_properties.c @@ -159,6 +159,22 @@ cleanup: return result; } +int __system_property_foreach( + void (*propfn)(const prop_info *pi, void *cookie), + void *cookie) +{ + prop_area *pa = __system_property_area__; + unsigned i; + + for (i = 0; i < pa->count; i++) { + unsigned entry = pa->toc[i]; + prop_info *pi = TOC_TO_INFO(pa, entry); + propfn(pi, cookie); + } + + return 0; +} + const prop_info *__system_property_find_nth(unsigned n) { prop_area *pa = __system_property_area__; diff --git a/libc/include/sys/system_properties.h b/libc/include/sys/system_properties.h index 85915b2de..01c3db332 100644 --- a/libc/include/sys/system_properties.h +++ b/libc/include/sys/system_properties.h @@ -76,13 +76,26 @@ int __system_property_read(const prop_info *pi, char *name, char *value); ** there is no nth property. Use __system_property_read() to ** read the value of this property. ** -** This method is for inspecting and debugging the property +** Please do not call this method. It only exists to provide +** backwards compatibility to NDK apps. Its implementation +** is inefficient and order of results may change from call +** to call. +*/ +const prop_info *__system_property_find_nth(unsigned n); + +/* Pass a prop_info for each system property to the provided +** callback. Use __system_property_read() to read the value +** of this property. +** +** This method is for inspecting and debugging the property ** system. Please use __system_property_find() instead. ** ** Order of results may change from call to call. This is ** not a bug. -*/ -const prop_info *__system_property_find_nth(unsigned n); +*/ +int __system_property_foreach( + void (*propfn)(const prop_info *pi, void *cookie), + void *cookie); __END_DECLS diff --git a/tests/system_properties_test.cpp b/tests/system_properties_test.cpp index 60188f41d..70ff1d662 100644 --- a/tests/system_properties_test.cpp +++ b/tests/system_properties_test.cpp @@ -125,6 +125,25 @@ TEST(properties, fill_247) { } } +static void foreach_test_callback(const prop_info *pi, void* cookie) { + size_t *count = static_cast(cookie); + + ASSERT_NE((prop_info *)NULL, pi); + (*count)++; +} + +TEST(properties, foreach) { + LocalPropertyTestState pa; + size_t count = 0; + + ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6)); + ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6)); + ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6)); + + ASSERT_EQ(0, __system_property_foreach(foreach_test_callback, &count)); + ASSERT_EQ(3U, count); +} + TEST(properties, find_nth) { LocalPropertyTestState pa; -- 2.11.0