Skip to content

Commit

Permalink
libc: minimal: Fix gmtime() userspace support
Browse files Browse the repository at this point in the history
The gmtime() function returns a global result variable, and this
variable must be placed in the `z_libc_partition` when userspace is
enabled.

Since gmtime() makes use of a global variable and this results in a
footprint increase, this commit makes the time functions optional by
introducing `CONFIG_MINIMAL_LIBC_TIME` Kconfig and making them only
available when this option is enabled.

Signed-off-by: Stephanos Ioannidis <[email protected]>
  • Loading branch information
stephanosio authored and carlescufi committed Jun 1, 2022
1 parent 2bcb713 commit eb71003
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
12 changes: 12 additions & 0 deletions lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ config MINIMAL_LIBC_RAND
In order to make use of the non-reentrant rand() and srand(), it is
necessary to set CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS=y.

config MINIMAL_LIBC_TIME
bool "Time functions"
default y
help
Enable time() and gmtime_r() for the minimal libc.

time() requires CONFIG_POSIX_CLOCK=y because it relies on the POSIX
clock_gettime() function.

In order to make use of the non-reentrant gmtime(), it is necessary
to set CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS=y.

endif # MINIMAL_LIBC

config STDOUT_CONSOLE
Expand Down
7 changes: 5 additions & 2 deletions lib/libc/minimal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ zephyr_library_sources(
source/stdout/stdout_console.c
source/stdout/sprintf.c
source/stdout/fprintf.c
source/time/gmtime.c
source/math/sqrtf.c
source/math/sqrt.c
)

zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK source/time/time.c)
if(CONFIG_MINIMAL_LIBC_TIME)
zephyr_library_sources(source/time/gmtime.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK source/time/time.c)
endif()

zephyr_library_sources_ifdef(CONFIG_MINIMAL_LIBC_RAND source/stdlib/rand.c)
7 changes: 4 additions & 3 deletions lib/libc/minimal/source/time/gmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

#include <time.h>
#include <zephyr/sys/libc-hooks.h>

/* A signed type with the representation of time_t without its
* implications.
Expand Down Expand Up @@ -97,10 +98,10 @@ struct tm *gmtime_r(const time_t *ZRESTRICT timep,
}

#ifdef CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS
static Z_LIBC_DATA struct tm gmtime_result;

struct tm *gmtime(const time_t *timep)
{
static struct tm shared;

return gmtime_r(timep, &shared);
return gmtime_r(timep, &gmtime_result);
}
#endif /* CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS */

0 comments on commit eb71003

Please sign in to comment.