diff options
| author | Thomas Ulmer <thomasmulmer02@gmail.com> | 2026-06-23 21:49:31 -0700 |
|---|---|---|
| committer | Thomas Ulmer <thomasmulmer02@gmail.com> | 2026-06-23 21:49:31 -0700 |
| commit | 23e0502dcc958cea4d195eb8c8a28a5c362ab69f (patch) | |
| tree | a758ea1c4a6e524b45fb108afa4ebaab7c1db36e | |
| parent | 874d317334fae1ddce316f9b6f4d0ce4e434bf52 (diff) | |
| parent | 9855bed0869ace4665e2776ec4d489847a0781b4 (diff) | |
combine sbrk spoof with heirloom fix for line numbers
| -rw-r--r-- | .gitignore | 7 | ||||
| -rw-r--r-- | Makefile.in | 15 | ||||
| -rw-r--r-- | README | 16 | ||||
| -rw-r--r-- | brk_shim.c | 44 | ||||
| -rw-r--r-- | brk_shim.h | 4 | ||||
| -rw-r--r-- | ex.c | 3 | ||||
| -rw-r--r-- | ex_re.c | 2 | ||||
| -rw-r--r-- | ex_subr.c | 4 | ||||
| -rw-r--r-- | exrecover.c | 5 | ||||
| -rw-r--r-- | libuxre/re.h | 2 | ||||
| -rw-r--r-- | libuxre/regex.h | 1 |
11 files changed, 88 insertions, 15 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7e5cc2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o +*~ +ex +exrecover +expreserve +libterm/libtermlib.a +libuxre/libuxre.a diff --git a/Makefile.in b/Makefile.in index 20af20c..ba35952 100644 --- a/Makefile.in +++ b/Makefile.in @@ -79,7 +79,7 @@ # Destinations for installation. $(PRESERVEDIR) is used for recovery files. # It will get mode 1777. # -PREFIX = /usr/local +PREFIX ?= /usr/local BINDIR = $(PREFIX)/bin LIBEXECDIR = $(PREFIX)/libexec MANDIR = $(PREFIX)/share/man @@ -190,7 +190,7 @@ OSTYPE = -DVMUNIX # raises this limit to around 1 GB, but ex will consume much more of core # and temp file space then. # -#LARGEF = -DLARGEF +LARGEF = -DLARGEF # # The next setting is a crucial one since it determines the way ex @@ -249,18 +249,18 @@ OBJS = ex.o ex_addr.o ex_cmds.o ex_cmds2.o ex_cmdsub.o \ ex_set.o ex_subr.o ex_tagio.o ex_temp.o ex_tty.o ex_unix.o \ ex_v.o ex_vadj.o ex_vget.o ex_vmain.o ex_voper.o \ ex_vops.o ex_vops2.o ex_vops3.o ex_vput.o ex_vwind.o \ - printf.o ex_version.o $(MALLOC) -EXRECOBJS=exrecover.o $(MALLOC) compat.o + printf.o ex_version.o $(MALLOC) brk_shim.o +EXRECOBJS=exrecover.o $(MALLOC) compat.o brk_shim.o EXPREOBJS=expreserve.o compat.o HDRS = ex.h ex_argv.h ex_re.h ex_temp.h ex_tty.h ex_tune.h ex_vars.h \ - ex_vis.h libterm/libterm.h + ex_vis.h libterm/libterm.h brk_shim.h SRC1 = ex.c ex_addr.c ex_cmds.c ex_cmds2.c ex_cmdsub.c SRC2 = ex_data.c ex_get.c ex_io.c ex_put.c ex_re.c SRC3 = ex_set.c ex_subr.c ex_tagio.c ex_temp.c ex_tty.c ex_unix.c SRC4 = ex_v.c ex_vadj.c ex_vget.c ex_vmain.c ex_voper.c SRC5 = ex_vops.c ex_vops2.c ex_vops3.c ex_vput.c ex_vwind.c SRC6 = printf.c expreserve.c exrecover.c ex_version.c -SRC7 = mapmalloc.c malloc.c +SRC7 = mapmalloc.c brk_shim.c #malloc.c .SUFFIXES: .o .c .c.o: ; $(CC) $(CCFLAGS) -c $< @@ -413,6 +413,7 @@ ex_vwind.o: libterm/libterm.h expreserve.o: config.h exrecover.o: config.h ex.h ex_proto.h ex_temp.h ex_tty.h ex_tune.h ex_vars.h exrecover.o: libterm/libterm.h -malloc.o: config.h +# malloc.o: config.h mapmalloc.o: config.h printf.o: config.h +brk_shim.o: brk_shim.h @@ -1,3 +1,19 @@ +TCCQ's fork of vi +========================= + +This is a small fork of a port of vi to modern posix systems. The changes +are primarily aimed at making compile and work with musl. Specifically +instead of allocating scratch space with sbrk it just preallocates a +large (64MB) buffer in .bss and relies on OS level paging to not waste +more physical memory than is necessary. Note that this means that the +memory usage is unlikely to decrease meaningfully after going up. +For vi which often doesn't have super long running sessions I deem this +acceptable. + +This version compiles and works nicely with tinycc and/or musl. Any C99 +compiler should work fine, though getting gcc+glibc to include the right +set of things takes some convincing. tinycc is suggested. + Welcome to the ex/vi port! ========================== diff --git a/brk_shim.c b/brk_shim.c new file mode 100644 index 0000000..1680a18 --- /dev/null +++ b/brk_shim.c @@ -0,0 +1,44 @@ +#include "brk_shim.h" + +#include <errno.h> + +/* + * The idea is to just allocate a large buffer and rely on lazy page + * loading to not use more physical memory than necessary. + */ + +#ifdef LARGEF +#define SHIMSIZE 0x4000000 +/* 64MB */ +#else +#define SHIMSIZE 0x200000 +/* 2MB */ +#endif + +static char shim_space[SHIMSIZE]; +static intptr_t shim_size = 0; + +void *shim_sbrk(intptr_t increment) { + void* current = (void*)(shim_space + shim_size); + shim_size += increment; + if (shim_size < 0) { + shim_size = 0; + return (void*)-1; + } else if (shim_size > SHIMSIZE) { + shim_size = SHIMSIZE; + return (void*)-1; + } + return current; +} + +int shim_brk(void* addr) { + if (addr < (void*)shim_space || + addr > (void*)(shim_space + SHIMSIZE)) { + errno = EINVAL; + return -1; + } + shim_size = ((void*)shim_space) - addr; + return 0; +} + + diff --git a/brk_shim.h b/brk_shim.h new file mode 100644 index 0000000..3e81758 --- /dev/null +++ b/brk_shim.h @@ -0,0 +1,4 @@ +#include <stdint.h> + +int shim_brk(void *addr); +void *shim_sbrk(intptr_t increment); @@ -87,6 +87,7 @@ static char sccsid[] = "@(#)ex.c 1.37 (gritter) 8/4/05"; #include "ex_argv.h" #include "ex_temp.h" #include "ex_tty.h" +#include "brk_shim.h" #ifdef TRACE char tttrace[] = { '/','d','e','v','/','t','t','y','x','x',0 }; @@ -527,7 +528,7 @@ argend: * this as ed does, saving a little core, but it will probably * not often make much difference. */ - fendcore = (line *) sbrk(0); + fendcore = (line *) shim_sbrk(0); endcore = fendcore - 2; #ifdef SIGTSTP @@ -84,7 +84,7 @@ static char sccsid[] = "@(#)ex_re.c 1.60 (gritter) 8/6/05"; #ifdef UXRE -#include <regex.h> +#include "uxre_regex.h" char *braslist[NBRA]; char *braelist[NBRA]; @@ -537,12 +537,12 @@ morelines(void) pg = 4096; pg /= sizeof (line); } - if ((char *)sbrk(pg * sizeof (line)) == (char *)-1) + if ((char *)shim_sbrk(pg * sizeof (line)) == (char *)-1) return (-1); endcore += pg; return (0); #else /* !_SC_PAGESIZE */ - if (sbrk(1024 * sizeof (line)) == (char *)-1) + if (shim_sbrk(1024 * sizeof (line)) == (char *)-1) return (-1); endcore += 1024; return (0); diff --git a/exrecover.c b/exrecover.c index c9a45a8..ef2410b 100644 --- a/exrecover.c +++ b/exrecover.c @@ -107,6 +107,7 @@ extern int vsnprintf(char *, size_t, const char *, va_list); #include "ex.h" #include "ex_temp.h" #include "ex_tty.h" +#include "brk_shim.h" #include <dirent.h> #include <time.h> #if !defined(HAVE_STRLCPY) || !defined(HAVE_STRLCAT) @@ -224,7 +225,7 @@ main(int argc, char *argv[]) /* * Initialize as though the editor had just started. */ - fendcore = (line *) sbrk(0); + fendcore = (line *) shim_sbrk(0); dot = zero = dol = fendcore; one = zero + 1; endcore = fendcore - 2; @@ -263,7 +264,7 @@ main(int argc, char *argv[]) /* * Allocate space for the line pointers from the temp file. */ - if ((char *) sbrk(H.Flines * sizeof (line)) == (char *) -1) + if ((char *) shim_sbrk(H.Flines * sizeof (line)) == (char *) -1) /* * Good grief. */ diff --git a/libuxre/re.h b/libuxre/re.h index 2738a05..64df2d0 100644 --- a/libuxre/re.h +++ b/libuxre/re.h @@ -35,7 +35,7 @@ /* #define __fnm_collate lc_collate */ /* <fnmatch.h> */ #include <limits.h> -#include <regex.h> +#include "regex.h" /* #include <fnmatch.h> */ #include <colldata.h> diff --git a/libuxre/regex.h b/libuxre/regex.h index 8dbd028..068c1f9 100644 --- a/libuxre/regex.h +++ b/libuxre/regex.h @@ -24,7 +24,6 @@ * Free Software Foundation, Inc. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - #ifndef LIBUXRE_REGEX_H #define LIBUXRE_REGEX_H /* from unixsrc:usr/src/common/head/regex.h /main/uw7_nj/1 */ |
