Index: uspace/lib/c/Makefile
===================================================================
--- uspace/lib/c/Makefile	(revision 7acd787ef6db58d28e222cd14600149ac8c9ff72)
+++ uspace/lib/c/Makefile	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
@@ -189,4 +189,5 @@
 TEST_SOURCES = \
 	test/adt/circ_buf.c \
+	test/casting.c \
 	test/fibril/timer.c \
 	test/main.c \
@@ -196,4 +197,5 @@
 	test/stdio/scanf.c \
 	test/odict.c \
+	test/perf.c \
 	test/perm.c \
 	test/qsort.c \
Index: uspace/lib/c/include/perf.h
===================================================================
--- uspace/lib/c/include/perf.h	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
+++ uspace/lib/c/include/perf.h	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2018 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ * System performance measurement utilities.
+ */
+
+#ifndef LIBC_PERF_H_
+#define LIBC_PERF_H_
+
+#include <time.h>
+
+/** Stopwatch is THE way to measure elapsed time on HelenOS. */
+typedef struct {
+	struct timespec start;
+	struct timespec end;
+} stopwatch_t;
+
+#define STOPWATCH_INITIALIZE_STATIC { { 0, 0 }, { 0, 0 } }
+
+/** Initialize system stopwatch.
+ *
+ * @param stopwatch Stopwatch to initialize.
+ */
+static inline void stopwatch_init(stopwatch_t *stopwatch)
+{
+	stopwatch->start.tv_sec = 0;
+	stopwatch->start.tv_nsec = 0;
+	stopwatch->end.tv_sec = 0;
+	stopwatch->end.tv_nsec = 0;
+}
+
+/** Emulate elapsed time for use in tests.
+ *
+ * @param stopwatch Stopwatch to use.
+ * @param nanos Elapsed time in nanoseconds to set.
+ */
+static inline void stopwatch_set_elapsed(stopwatch_t *stopwatch, nsec_t nanos)
+{
+	stopwatch->start.tv_sec = 0;
+	stopwatch->start.tv_nsec = 0;
+	stopwatch->end.tv_sec = NSEC2SEC(nanos);
+	stopwatch->end.tv_nsec = nanos - SEC2NSEC(stopwatch->end.tv_sec);
+}
+
+/** Start the stopwatch.
+ *
+ * @param stopwatch Stopwatch to start.
+ */
+static inline void stopwatch_start(stopwatch_t *stopwatch)
+{
+	getuptime(&stopwatch->start);
+}
+
+/** Stop the stopwatch.
+ *
+ * @param stopwatch Stopwatch to stop.
+ */
+static inline void stopwatch_stop(stopwatch_t *stopwatch)
+{
+	getuptime(&stopwatch->end);
+}
+
+/** Get elapsed time in nanoseconds.
+ *
+ * @param stopwatch Stopwatch to read from.
+ * @return Elapsed time in nanoseconds.
+ */
+static inline nsec_t stopwatch_get_nanos(stopwatch_t *stopwatch)
+{
+	return ts_sub_diff(&stopwatch->end, &stopwatch->start);
+}
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/types/casting.h
===================================================================
--- uspace/lib/c/include/types/casting.h	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
+++ uspace/lib/c/include/types/casting.h	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2019 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_TYPES_CASTING_H_
+#define LIBC_TYPES_CASTING_H_
+
+#include <stdbool.h>
+#include <stddef.h>
+
+/**
+ * Checks that the size_t value can be casted to int without loss of data.
+ *
+ * @param val Value to test.
+ * @return Whether it is safe to typecast value to int.
+ */
+static inline bool can_cast_size_t_to_int(size_t val)
+{
+	unsigned int as_uint = (unsigned int) val;
+	return !((as_uint != val) || (((int) as_uint) < 0));
+}
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/test/casting.c
===================================================================
--- uspace/lib/c/test/casting.c	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
+++ uspace/lib/c/test/casting.c	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2019 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <pcut/pcut.h>
+#include <limits.h>
+#include <types/casting.h>
+
+PCUT_INIT;
+
+PCUT_TEST_SUITE(casting);
+
+/*
+ * Following tests checks functionality of can_cast_size_t_to_int.
+ */
+
+PCUT_TEST(size_t_to_int_with_small)
+{
+	PCUT_ASSERT_TRUE(can_cast_size_t_to_int(0));
+	PCUT_ASSERT_TRUE(can_cast_size_t_to_int(128));
+}
+
+PCUT_TEST(size_t_to_int_with_biggest_int)
+{
+	PCUT_ASSERT_TRUE(can_cast_size_t_to_int(INT_MAX));
+}
+
+PCUT_TEST(size_t_to_int_with_biggest_size_t)
+{
+	PCUT_ASSERT_FALSE(can_cast_size_t_to_int(SIZE_MAX));
+}
+
+PCUT_EXPORT(casting);
Index: uspace/lib/c/test/main.c
===================================================================
--- uspace/lib/c/test/main.c	(revision 7acd787ef6db58d28e222cd14600149ac8c9ff72)
+++ uspace/lib/c/test/main.c	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
@@ -32,4 +32,5 @@
 PCUT_INIT;
 
+PCUT_IMPORT(casting);
 PCUT_IMPORT(circ_buf);
 PCUT_IMPORT(fibril_timer);
@@ -37,4 +38,5 @@
 PCUT_IMPORT(mem);
 PCUT_IMPORT(odict);
+PCUT_IMPORT(perf);
 PCUT_IMPORT(perm);
 PCUT_IMPORT(qsort);
Index: uspace/lib/c/test/perf.c
===================================================================
--- uspace/lib/c/test/perf.c	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
+++ uspace/lib/c/test/perf.c	(revision a362c166d0d57f9e8ef9b820030c62e68272a36e)
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2018 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <pcut/pcut.h>
+#include <fibril.h>
+#include <perf.h>
+
+PCUT_INIT;
+
+PCUT_TEST_SUITE(perf);
+
+/* Checks that initialization zeroes out all entries. */
+PCUT_TEST(zero_diff)
+{
+	stopwatch_t sw;
+	stopwatch_init(&sw);
+	PCUT_ASSERT_INT_EQUALS(0, (int) stopwatch_get_nanos(&sw));
+}
+
+/* Checks that initialization zeroes out all entries. */
+PCUT_TEST(zero_diff_static)
+{
+	stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
+	PCUT_ASSERT_INT_EQUALS(0, (int) stopwatch_get_nanos(&sw));
+}
+
+/* Checks that measuring 1s sleep does not give completely invalid results. */
+PCUT_TEST(stopwatch_smokes)
+{
+	stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
+	stopwatch_start(&sw);
+	fibril_sleep(1);
+	stopwatch_stop(&sw);
+	nsec_t diff_nanos = stopwatch_get_nanos(&sw);
+	PCUT_ASSERT_TRUE(diff_nanos > MSEC2NSEC(500));
+	PCUT_ASSERT_TRUE(diff_nanos < SEC2NSEC(5));
+}
+
+/* Checks that setting time works for small values. */
+PCUT_TEST(stopwatch_emulation_works_small)
+{
+	stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
+	stopwatch_set_elapsed(&sw, 42);
+	PCUT_ASSERT_INT_EQUALS(42, (int) stopwatch_get_nanos(&sw));
+}
+
+/* Checks that setting time works for big values too. */
+PCUT_TEST(stopwatch_emulation_works_big)
+{
+	stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
+	stopwatch_set_elapsed(&sw, 4200000000021);
+	PCUT_ASSERT_EQUALS(4200000000021, (long long) stopwatch_get_nanos(&sw));
+}
+
+PCUT_EXPORT(perf);
