Skip to contentSkip to conversation

timeout: add l_timeout_remaining

PATCH Applied Denis Kenzior 1 participant 4 messages Oct 31, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 timeout: add l_timeout_remaining

From: James Prestwood <[email protected]>

Gets the remaining microseconds left on a timer. Microseconds were chosen in order to be easily compatible with l_time APIs.

---
 ell/ell.sym   |  1 +
 ell/timeout.c | 29 +++++++++++++++++++++++++++++
 ell/timeout.h |  3 ++-
 3 files changed, 32 insertions(+), 1 deletion(-)
ell/ell.sym +1 -0
index 00123136a527..af7395f3f1ae 100644
@@ -529,6 +529,7 @@ global:
 	l_timeout_create_ms;
 	l_timeout_modify;
 	l_timeout_modify_ms;
+	l_timeout_remaining;
 	l_timeout_remove;
 	l_timeout_set_callback;
 	/* tls */
ell/timeout.c +29 -0
index 4fc21a0854c1..541692ecff93 100644
@@ -23,6 +23,7 @@
 #include "timeout.h"
 #include "main-private.h"
 #include "private.h"
+#include "time-private.h"
 
 /**
  * SECTION:timeout
@@ -298,3 +299,31 @@ LIB_EXPORT void l_timeout_set_callback(struct l_timeout *timeout,
 	timeout->user_data = user_data;
 	timeout->destroy = destroy;
 }
+
+/**
+ * l_timeout_get_remaining:
+ *
+ * Get the remaining time for a timeout in microseconds
+ *
+ * @timeout: timeout object
+ * @remaining: microseconds remaining on timer
+ *
+ * Returns: True if successfully got remaining time
+ *          False if failure to get remaining time
+ **/
+LIB_EXPORT bool l_timeout_remaining(struct l_timeout *timeout,
+						uint64_t *remaining)
+{
+	struct itimerspec current;
+
+	if (unlikely(!timeout))
+		return false;
+
+	if (timerfd_gettime(timeout->fd, &current) < 0)
+		return false;
+
+	if (remaining)
+		*remaining = _time_from_timespec(&current.it_value);
+
+	return true;
+}
ell/timeout.h +2 -1
index 2db78d8fcd41..c0d463c1d021 100644
@@ -34,7 +34,8 @@ void l_timeout_remove(struct l_timeout *timeout);
 void l_timeout_set_callback(struct l_timeout *timeout,
 				l_timeout_notify_cb_t callback, void *user_data,
 				l_timeout_destroy_cb_t destroy);
-
+bool l_timeout_remaining(struct l_timeout *timeout,
+				uint64_t *remaining);
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 2/3 util: Add l_memcpy

Some callers use memcpy without checking whether the size 'n' is 0 and src is NULL. 'src' is declared as non-null and can thus cause sanitizers to complain. Having 'src' as NULL is fine as long as 'n' is
0. Add a new l_memcpy function to take care of this case.

---
 ell/util.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
ell/util.h +10 -0
index c56f182292fa..2a0ee0e50b93 100644
@@ -243,6 +243,16 @@ static inline void l_put_be64(uint64_t val, void *ptr)
 void *l_malloc(size_t size) __attribute__ ((warn_unused_result, malloc));
 void *l_memdup(const void *mem, size_t size)
 			__attribute__ ((warn_unused_result, malloc));
+
+static inline void * __attribute__((nonnull(1))) l_memcpy(void *dest,
+						const void *src, size_t n)
+{
+	if (!n)
+		return dest;
+
+	return __builtin_memcpy(dest, src, n);
+}
+
 void l_free(void *ptr);
 DEFINE_CLEANUP_FUNC(l_free);
 
Signature · 1 line
2.45.2
author Applied
#2 lore raw

Re: [PATCH v3 2/3] util: Add l_memcpy

Quoted 8 lines · Denis Kenzior

Some callers use memcpy without checking whether the size 'n' is 0 and src is NULL. 'src' is declared as non-null and can thus cause sanitizers to complain. Having 'src' as NULL is fine as long as 'n' is
0. Add a new l_memcpy function to take care of this case.

---
  ell/util.h | 10 ++++++++++
  1 file changed, 10 insertions(+)

Applied

#3 lore raw

PATCH v3 3/3 unit: Add l_memcpy test

In reply to Denis Kenzior
---
 unit/test-util.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
unit/test-util.c +12 -0
index 341c790c3731..27b0422200d0 100644
@@ -288,6 +288,16 @@ static void test_roundup_pow_of_two(const void *test_data)
 	}
 }
 
+static void test_l_memcpy(const void *test_data)
+{
+	static const uint8_t src[] = { 0x00, 0xff, 0xff, 0xff };
+	uint8_t dst[10];
+
+	assert(l_memcpy(dst, src, sizeof(src)) == dst);
+	assert(l_memcpy(dst, src, 0) == dst);
+	assert(l_memcpy(dst, NULL, 0) == dst);
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -313,5 +323,7 @@ int main(int argc, char *argv[])
 
 	l_test_add("roundup_pow_of_two", test_roundup_pow_of_two, NULL);
 
+	l_test_add("l_memcpy", test_l_memcpy, NULL);
+
 	return l_test_run();
 }
Signature · 1 line
2.45.2