From 2b270e6697e3651f4266c7a39beaafe624bc19c4 Mon Sep 17 00:00:00 2001 From: Nick Briggs Date: Wed, 31 Aug 2022 12:42:39 -0700 Subject: [PATCH] Use mmap() in place of posix_memalign() to allocate Lisp memory posix_memalign() does not guarantee initializing the allocated memory to zero, which Lisp expects, so the code must memset() the entire allocated region to zero. The effect of this is (generally) to force the allocation of RAM to the process, which is wasteful, since we normally start with only 4% of 256 MB in use for a full.sysout. Allocating memory with mmap, using MAP_ANON, guarantees that the memory is already zeroed (effectively mapping /dev/zero with copy-on-write) so it is not necessary to touch it before use. This keeps the pre-allocated RAM to a minimum. --- src/ldsout.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ldsout.c b/src/ldsout.c index 57c44bd..c7adba0 100644 --- a/src/ldsout.c +++ b/src/ldsout.c @@ -13,8 +13,9 @@ #include // for open, O_RDONLY #include // for perror, fprintf, printf, stderr, sprintf -#include // for exit, free, malloc, posix_memalign +#include // for exit, free, malloc #include // for memset +#include // for mmap #include // for stat, fstat #include // for lseek, read, close, getpagesize #include "adr68k.h" // for Addr68k_from_LADDR @@ -172,12 +173,11 @@ int sysout_loader(const char *sysout_file_name, int sys_size) { /* allocate Virtual Memory Space */ - if (posix_memalign((void *)&lispworld_scratch, getpagesize(), sys_size * MBYTE) != 0) { + lispworld_scratch = mmap(0, sys_size * MBYTE, PROT_READ|PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + if (lispworld_scratch == MAP_FAILED) { fprintf(stderr, "sysout_loader: can't allocate Lisp %dMBytes VM \n", sys_size); exit(-1); } - /* Sadly, parts of the system depend on memory being initialized to zero */ - memset(lispworld_scratch, 0, sys_size * MBYTE); /* now you can access lispworld */ Lisp_world = (DLword *)lispworld_scratch;