From aae3831e6334eed571e8c2eb04295e53cf24f024 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 16 Sep 2022 16:04:21 -0400 Subject: [PATCH] Implement sysconf for Arm - add support for using sysconf to get page size in _mallocr.c via HAVE_SYSCONF_PAGESIZE flag set in configure.host - set flag in configure.host for arm and add a default sysconf implementation in libc/sys/arm that returns the page size - the default implementation can be overridden outside newlib to allow a different page size to improve malloc on devices with a small footprint without needing to rebuild newlib - this patch is based on a contribution from Torbjorn Svensson and Niklas Dahlquist (https://ecos.sourceware.org/ml/newlib/current/017616.html) --- newlib/configure.host | 2 ++ newlib/libc/stdlib/_mallocr.c | 2 ++ newlib/libc/sys/arm/Makefile.inc | 2 +- newlib/libc/sys/arm/sysconf.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 newlib/libc/sys/arm/sysconf.c diff --git a/newlib/configure.host b/newlib/configure.host index 98ce07d..32d1436 100644 --- a/newlib/configure.host +++ b/newlib/configure.host @@ -628,6 +628,7 @@ newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED -DEXIT_PROVID ;; arm*-*-pe) syscall_dir=syscalls + newlib_cflags="${newlib_cflags} -DHAVE_SYSCONF_PAGESIZE" ;; arm*-*-*) syscall_dir=syscalls @@ -642,6 +643,7 @@ newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED -DEXIT_PROVID # newlib_cflags="${newlib_cflags} -DARM_RDP_MONITOR" newlib_cflags="${newlib_cflags} -DARM_RDI_MONITOR" fi + newlib_cflags="${newlib_cflags} -DHAVE_SYSCONF_PAGESIZE" ;; avr*) newlib_cflags="${newlib_cflags} -DNO_EXEC -DSMALL_MEMORY -DMISSING_SYSCALL_NAMES" diff --git a/newlib/libc/stdlib/_mallocr.c b/newlib/libc/stdlib/_mallocr.c index 4b53997..1997b6d 100644 --- a/newlib/libc/stdlib/_mallocr.c +++ b/newlib/libc/stdlib/_mallocr.c @@ -320,12 +320,14 @@ extern "C" { #endif #ifndef _WIN32 +#ifndef HAVE_SYSCONF_PAGESIZE #ifdef SMALL_MEMORY #define malloc_getpagesize (128) #else #define malloc_getpagesize (4096) #endif #endif +#endif #if __STD_C extern void __malloc_lock(struct _reent *); diff --git a/newlib/libc/sys/arm/Makefile.inc b/newlib/libc/sys/arm/Makefile.inc index 490a963..0122956 100644 --- a/newlib/libc/sys/arm/Makefile.inc +++ b/newlib/libc/sys/arm/Makefile.inc @@ -1,6 +1,6 @@ AM_CPPFLAGS_%C% = -I$(srcdir)/libc/machine/arm -libc_a_SOURCES += %D%/access.c %D%/aeabi_atexit.c +libc_a_SOURCES += %D%/access.c %D%/aeabi_atexit.c %D%/sysconf.c if MAY_SUPPLY_SYSCALLS libc_a_SOURCES += %D%/libcfunc.c %D%/trap.S %D%/syscalls.c endif diff --git a/newlib/libc/sys/arm/sysconf.c b/newlib/libc/sys/arm/sysconf.c new file mode 100644 index 0000000..dbed7d7 --- /dev/null +++ b/newlib/libc/sys/arm/sysconf.c @@ -0,0 +1,30 @@ +/* libc/sys/arm/sysconf.c - The sysconf function */ + +/* Copyright 2020, STMicroelectronics + * + * All rights reserved. + * + * Redistribution, modification, and use in source and binary forms is permitted + * provided that the above copyright notice and following paragraph are + * duplicated in all such forms. + * + * This file is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include +#include + +long sysconf(int name) +{ + switch (name) + { + case _SC_PAGESIZE: + return 4096; + + default: + errno = EINVAL; + return -1; + } + return -1; /* Can't get here */ +} -- 1.8.3.1