Skip to content

Commit

Permalink
use malloc-based sbrk() replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
amin committed Jun 5, 2021
1 parent 263fc45 commit b12bce6
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 32 deletions.
19 changes: 5 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,9 @@ OSTYPE = -DVMUNIX
TERMLIB = termlib

#
# Since ex uses sbrk() internally, a conflict with the libc's version of
# malloc() must be avoided. There are two ways to work around this problem.
# The first is to allocate a static pool for all malloc purposes. This will
# work on any kind of system.
#
#MALLOC=malloc.o
#
# If mmap() can be used to allocate anonymous memory, this is the preferred
# choice as it allows to grow memory dynamically as it is needed. This will
# usually work unless you are compiling for a vector machine or another
# unusual enviroment.
MALLOC=mapmalloc.o
# Malloc-based sbrk() substitution.
#
MALLOC=sbreak.o

###############################################################################
# #
Expand Down Expand Up @@ -350,7 +341,7 @@ ex.pkg: all
rm -rf $(PKGROOT) $(PKGPROTO) $(PKGTEMP)/$@

ex.o: config.h ex_argv.h ex.h ex_proto.h ex_temp.h ex_tty.h ex_tune.h
ex.o: ex_vars.h libterm/libterm.h
ex.o: ex_vars.h sbreak.h libterm/libterm.h
ex_addr.o: config.h ex.h ex_proto.h ex_re.h ex_tune.h ex_vars.h
ex_cmds.o: config.h ex_argv.h ex.h ex_proto.h ex_temp.h ex_tty.h ex_tune.h
ex_cmds.o: ex_vars.h ex_vis.h libterm/libterm.h
Expand All @@ -372,7 +363,7 @@ ex_re.o: config.h ex.h ex_proto.h ex_re.h ex_tune.h ex_vars.h
ex_set.o: config.h ex.h ex_proto.h ex_temp.h ex_tty.h ex_tune.h ex_vars.h
ex_set.o: libterm/libterm.h
ex_subr.o: config.h ex.h ex_proto.h ex_re.h ex_tty.h ex_tune.h ex_vars.h
ex_subr.o: ex_vis.h libterm/libterm.h
ex_subr.o: ex_vis.h sbreak.h libterm/libterm.h
ex_tagio.o: config.h ex.h ex_proto.h ex_tune.h ex_vars.h
ex_temp.o: config.h ex.h ex_proto.h ex_temp.h ex_tty.h ex_tune.h ex_vars.h
ex_temp.o: ex_vis.h libterm/libterm.h
Expand Down
15 changes: 2 additions & 13 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,8 @@ Prerequisites for ports to other systems are:

- The system must provide an ANSI C-89 compiler and POSIX.1-1990 functions.

- The system must provide an sbrk() call to increase the memory heap size.
If only a fake sbrk() call is provided that works by pre-allocating
several MB, vi will probably work too.

- The system library must allow replacement of malloc() and printf() by the
versions provided by vi. For malloc(), it also must make its own internal
memory requests using the vi malloc(). Otherwise, vi will likely die with
a segmentation fault because the storage allocated by sbrk() interferes
with usual Unix library implementations of malloc().

The last two requirements could probably be eliminated with some effort, but
it would not result in any real improvements for usual the Unix platforms vi
is targeted at, so it has not be done yet.
- The system library must allow replacement of printf() by the version
provided by vi.


Terminal capabilities
Expand Down
3 changes: 2 additions & 1 deletion ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "sbreak.h"

#ifdef TRACE
char tttrace[] = { '/','d','e','v','/','t','t','y','x','x',0 };
Expand Down Expand Up @@ -529,7 +530,7 @@ main(register int ac, register char *av[])
* this as ed does, saving a little core, but it will probably
* not often make much difference.
*/
fendcore = (line *) sbrk(0);
fendcore = (line *) sbreak(0);
endcore = fendcore - 2;

#ifdef SIGTSTP
Expand Down
9 changes: 5 additions & 4 deletions ex_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static char sccsid[] = "@(#)ex_subr.c 1.41 (gritter) 12/25/06";
#include "ex_re.h"
#include "ex_tty.h"
#include "ex_vis.h"
#include "sbreak.h"

short lastsc;

Expand Down Expand Up @@ -490,14 +491,14 @@ morelines(void)
pg = 4096;
pg /= sizeof (line);
}
if ((char *)sbrk(pg * sizeof (line)) == (char *)-1)
if ((char *)sbreak(pg * sizeof (line)) == (char *)-1)
return (-1);
endcore += pg;
endcore = sbreak(0) - sizeof(line);
return (0);
#else /* !_SC_PAGESIZE */
if (sbrk(1024 * sizeof (line)) == (char *)-1)
if (sbreak(1024 * sizeof (line)) == (char *)-1)
return (-1);
endcore += 1024;
endcore = sbreak(0) - sizeof(line);
return (0);
#endif /* !_SC_PAGESIZE */
}
Expand Down
106 changes: 106 additions & 0 deletions sbreak.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* AT&T Unix 7th Edition memory allocation routines.
*
* Modified for ex by Gunnar Ritter, Freiburg i. Br., Germany,
* July 2000.
*
* Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Redistributions of source code and documentation must retain the
* above copyright notice, this list of conditions and the following
* disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed or owned by Caldera
* International, Inc.
* Neither the name of Caldera International, Inc. nor the names of
* other contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
* LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef VMUNIX

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "config.h"
#include "ex.h"

#ifdef LANGMSG
#include <nl_types.h>
extern nl_catd catd;
#else
#define catgets(a, b, c, d) (d)
#endif

#define POOL 32768

char *pool = NULL;
intptr_t ps = 0;

char *
poolsbrk(intptr_t unused)
{
intptr_t os, ns;

if (pool == NULL) {
if ((pool = malloc(POOL)) == NULL)
error(catgets(catd, 1, 241,
"No memory pool"));
ps = POOL;
}
return NULL;
}

void
shiftptr(size_t incr)
{
fendcore = (line *) ((char *)fendcore + incr);
endcore = (line *) ((char *)endcore + incr);
addr1 = (line *) ((char *)addr1 + incr);
addr2 = (line *) ((char *)addr2 + incr);
dol = (line *) ((char *)dol + incr);
dot = (line *) ((char *)dot + incr);
one = (line *) ((char *)one + incr);
truedol = (line *) ((char *)truedol + incr);
unddol = (line *) ((char *)unddol + incr);
zero = (line *) ((char *)zero + incr);
}

void *
sbreak(size_t incr)
{
char *oldpool = pool;

if (incr == 0)
return pool + ps;
if ((pool = realloc(pool, ps + incr)) == NULL)
return (void *)-1;
ps += incr;
if (pool != oldpool)
shiftptr(pool - oldpool);
return pool;
}

#endif /* VMUNIX */
2 changes: 2 additions & 0 deletions sbreak.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
char *poolsbrk(intptr_t inc);
void *sbreak(size_t incr);

0 comments on commit b12bce6

Please sign in to comment.