From 6faf43791b274928485d49ad12fb48106c4ccac8 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sun, 5 Dec 2021 19:00:11 +0100 Subject: [PATCH] Disable vmalloc by default; fix build on Cygwin Vmalloc is incompatible with Cygwin, but the code to disable it on Cygwin did not work properly, somehow causing the build to freeze at a seemingly unrelated point (i.e., when iffe feature tests attempt to write to sfstdout). Vmalloc has wasted my time for the last time, so now it's getting disabled by default even on development builds and you'll have to pass -D_AST_vmalloc in CCFLAGS to enable it for testing purposes. This commit has a few other build tweaks as well. src/lib/libast/features/vmalloc: - tst map_malloc: Remove no-op #if __CYGWIN__ block which was in the #else clause of another #if __CYGWIN__ block. - Output block ('cat{'): - Instead of disabling vmalloc for certain systems, disable it unless _AST_vmalloc is defined. - To disable it, set _AST_std_malloc as well as _std_malloc, just to be sure. src/lib/libast/vmalloc/malloc.c: - Remove ineffective Cygwin special-casing. src/lib/libcmd/vmstate.c: - This is only useful for vmalloc, so do not pointlessly compile it if vmalloc is disabled. src/lib/libast/man/vmalloc.3: - Add deprecation notice. Resolves: https://github.com/ksh93/ksh/issues/360 --- src/cmd/ksh93/tests/leaks.sh | 1 + src/lib/libast/features/vmalloc | 9 +++++---- src/lib/libast/man/vmalloc.3 | 7 +++++++ src/lib/libast/vmalloc/malloc.c | 4 ---- src/lib/libcmd/vmstate.c | 12 ++++++++---- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/cmd/ksh93/tests/leaks.sh b/src/cmd/ksh93/tests/leaks.sh index 5af1f5b3f..256cb4644 100755 --- a/src/cmd/ksh93/tests/leaks.sh +++ b/src/cmd/ksh93/tests/leaks.sh @@ -23,6 +23,7 @@ # Determine method for running tests. # The 'vmstate' builtin can be used if ksh was compiled with vmalloc. +# (Pass -D_AST_vmalloc in CCFLAGS; for testing only as it's deprecated) if builtin vmstate 2>/dev/null && n=$(vmstate --format='%(busy_size)u') && let "($n) == ($n) && n > 0" # non-zero number? diff --git a/src/lib/libast/features/vmalloc b/src/lib/libast/features/vmalloc index 177d3e6fb..ba68abd5a 100644 --- a/src/lib/libast/features/vmalloc +++ b/src/lib/libast/features/vmalloc @@ -57,9 +57,6 @@ tst map_malloc note{ map malloc to _ast_malloc }end noexecute{ extern void* calloc _ARG_((unsigned int, unsigned int)); #define LOCAL() calloc(1,1) #endif - #if __CYGWIN__ - #define extern __declspec(dllexport) - #endif #define HT double static HT heap[1024 * 4]; static HT* hp = &heap[1]; @@ -207,9 +204,13 @@ tst malloc_hook note{ gnu malloc hooks work }end execute{ cat{ #include "FEATURE/mmap" - #if _AST_ksh_release || _BLD_INSTRUMENT || cray || _UWIN && _BLD_ast + /* AST vmalloc is deprecated for being crashy and wasteful; ksh 93u+m uses the OS's malloc by default */ + #undef _std_malloc + #undef _AST_std_malloc + #if !_AST_vmalloc #undef _map_malloc #define _std_malloc 1 /* defer to standard malloc */ + #define _AST_std_malloc 1 #endif #if _mmap_anon #define _mem_mmap_anon 1 diff --git a/src/lib/libast/man/vmalloc.3 b/src/lib/libast/man/vmalloc.3 index 75d4f585f..e9c60b99e 100644 --- a/src/lib/libast/man/vmalloc.3 +++ b/src/lib/libast/man/vmalloc.3 @@ -66,6 +66,13 @@ vmalloc \- virtual memory allocation .MW "Void_t* valloc(size_t size);" .MW "int setregmax(int regmax);" .fi +.SH DEPRECATION NOTICE +In the 2021 ksh 93u+m distribution, \fIvmalloc\fP(3) was deprecated for +being unstable and wasteful in comparison to \fImalloc\fP(3) implementations +that come with modern operating systems. +By default, the interfaces described here map to the OS's standard +\fImalloc\fP(3) implementation. \fIvmalloc\fP can be enabled at compile time +by passing \fB-D_AST_vmalloc\fP in \fBCCFLAGS\fP. .SH DESCRIPTION These functions for dynamic storage allocation work in \fIregions\fP of memory. diff --git a/src/lib/libast/vmalloc/malloc.c b/src/lib/libast/vmalloc/malloc.c index 8abbc0c37..090d10b83 100644 --- a/src/lib/libast/vmalloc/malloc.c +++ b/src/lib/libast/vmalloc/malloc.c @@ -85,10 +85,6 @@ typedef struct ______mstats Mstats_t; * will simply call malloc etc. */ -#if !defined(_AST_std_malloc) && __CYGWIN__ -#define _AST_std_malloc 1 -#endif - /* malloc compatibility functions ** ** These are aware of debugging/profiling and are driven by the diff --git a/src/lib/libcmd/vmstate.c b/src/lib/libcmd/vmstate.c index d0b16f281..92381c9b6 100644 --- a/src/lib/libcmd/vmstate.c +++ b/src/lib/libcmd/vmstate.c @@ -21,6 +21,12 @@ ***********************************************************************/ #pragma prototyped +#include +#include +#include + +#if !_std_malloc /* do not pointlessly compile this if vmalloc is disabled */ + #define FORMAT "region=%(region)p method=%(method)s flags=%(flags)s size=%(size)d segments=%(segments)d busy=(%(busy_size)d,%(busy_blocks)d,%(busy_max)d) free=(%(free_size)d,%(free_blocks)d,%(free_max)d)" static const char usage[] = @@ -49,10 +55,6 @@ static const char usage[] = "[+SEE ALSO?\bvmalloc\b(3)]" ; -#include -#include -#include - typedef struct State_s { char* format; @@ -200,3 +202,5 @@ b_vmstate(int argc, char** argv, Shbltin_t* context) } return 0; } + +#endif /* !_std_malloc */