Info needed: UNIX for 286/386 machines (really malloc)

Steve Nuchia steve at nuchat.UUCP
Sun Mar 13 01:07:12 AEST 1988


Re: flamage about the 286 malloc.

It really is a crock.  I looked into it and wrote the following
allocator in support of pathalias, and it works with good
efficiency (both space and time).

If someone wants to layer the free() logic, say from K&R,
on top of this it would make a very usable replacement.

This only applies to large model, since sbrk isn't broken
in small model (small consolation).
--
/*
 *	a primitive malloc replacement that works efficiently
 *	in spite of the braindamage in uPort's brk/sbrk.
 *
 *	This routine does not support a free() call.  Recommend
 *	someone layer a reasonable alloc/free package on top
 *	of this system interface routine and repost it.
 *
 *		Steve Nuchia
 *		released to the public domain
 *		12 March 1988
 *		steve at nuchat	(713) 334 6720
 */

char	*malloc(n)
unsigned n;
{
static	struct
{
	char		*p;
	unsigned	s;
}   seg[64];
static	int	ns;
register int	i;

    if ( n & 1 ) n++;
    for ( i = 0; i < ns; i++ ) if ( seg[i].s >= n ) break;
    if ( i == ns )
    {
	if ( ++ns > 64 ) abort();
	seg[i].p = sbrk(0);
	seg[i].s = 0x0F000;
	if ( n > seg[i].s ) seg[i].s = n;
	while ( seg[i].s && brk(seg[i].p + seg[i].s) == -1 ) seg[i].s -= 512;
	seg[i].p[0] = seg[i].p[seg[i].s - 1] = 0;
    }
    seg[i].s -= n;
    seg[i].p += n;
    return ( seg[i].p - n );
}
-- 
Steve Nuchia	    | [...] but the machine would probably be allowed no mercy.
uunet!nuchat!steve  | In other words then, if a machine is expected to be
(713) 334 6720	    | infallible, it cannot be intelligent.  - Alan Turing, 1947



More information about the Comp.unix.microport mailing list