[comp.sys.amiga.tech] Re^2: realloc

crash@boake2 (Frank J. Edwards) (01/20/90)

> Realloc is indeed missing, at least from the earlier versions of Manx.
> Here is a fairly old posting that contains the source to realloc().
> As mentioned in the post, it is also available from Manx's bulletin
> board.

    [FIX -- FIX -- entire source file included below -- FIX -- FIX]

>> cc realloc.c
>> lb c.lib -a movmem realloc.o
>> cc +L realloc.c
>> lb c32.lib -a movmem realloc.o
>> -- 
>> < Mark R. Rinfret,  mrr@amanpt1.ZONE1.COM | ...rayssd!galaxia!amanpt1!mrr    >
>> < Aquidneck Management Associates       Home: 401-846-7639                   >
>> < 6 John Clarke Road                    Work: 401-849-8900 x56               >
>> < Middletown, RI 02840              "If I just had a little more time...">
> Andrew Heybey, atheybey@ptt.lcs.mit.edu, uunet!ptt.lcs.mit.edu!atheybey

WARNING!!!  DANGER, WILL ROBINSON!!!  (Sorry, I've always wanted to do
that ;-)

The code (re)posted is _NOT_ correct!  I have included the correct listing
at the end of this posting.  I will point out the differences here first.

1)  The use of the `size_t' typedef will cause the movmem() call to
    receive 12 bytes (4 longwords) on the stack.  If you're not
    compiling in the 32-bit mode, this is WRONG.  (see me previous
    posting for patches to the `heapmem.o' file provided with
    Manx 3.6a).  Perhaps there should be a hardcoded movmem() call
    in this module which will always accept a (long) length.  Note
    that other parts of this subroutine use (long)s (size already
    alloc'd) and thus must be truncated to an (int) for this function
    call.

2)  This code shows a call to `movmem(size)' which I #ifdef'd out
    and replaced with `malloc(size)'

And I think that'll do it.  I've trimmed any trailing lines off the end
of the source, so just cut below to EOF and you've got it.  Enjoy!
-----
Please use "uunet!pdn!boake2!ckctpa!crash" as "ckctpa" does not yet
appear in the USEnet maps.  (Sheeesh!  I just sent it in yesterday!)
-----
Frank J. Edwards		ComputerKnowledge Corp
2677 Arjay Court		12740 Hillcrest, Suite 212
Palm Harbor, FL  34684-4505	Dallas, TX  75230
Phone:  (813) 786-3675		(214) 385-9700 / (800) 227-9700
-----

/***********************************************************************
The following is the source code for realloc.  Compile it with the
appropriate memory model options, and insert in the c libraries.
***********************************************************************/

/* Copyright (C) 1987 by Manx Software Systems, Inc. */

unsigned long _Heapsize = 40 * 1024L;
typedef long size_t;        /* This is a problem for `osize', below */

#define bump(p,i) ((l_t *)((char *)(p)+(i)))
#define ptrdiff(p1,p2) (unsigned long)((char *)(p1)-(char *)(p2))

typedef struct list {
    struct list *next;
} l_t;
static l_t first, *current;
static l_t *endmarker = &first, *restart = &first;
static size_t keep;

#define INUSE   1
#define inuse(p) (*(size_t *)(p)&INUSE)
#define markblk(p) (*(size_t *)(p) |= INUSE)
#define unmark(p) (*(size_t *)(p) &= ~INUSE)
#define chain(p)    ((l_t *)(*(size_t *)(p) & ~INUSE))
#define BLOCK   (512*sizeof(l_t))   /* # of bytes to ask sbrk for */

char *realloc(area, size)
register char *area; unsigned size;
{
    register char *cp, *end;
    size_t osize;
    char *movmem(), *malloc();

    end = (char *)chain((l_t *)area-1);
    if ((osize = ptrdiff(end, area)) > size) {
        osize = size;
        end = (char *)bump(area, osize);
    }
    free(area);
#ifdef WRONG
    if ((cp = movmem(size)) != 0 && cp != area)
#else /* RIGHT */
    if ((cp = malloc(size)) != 0 && cp != area)
#endif
    {
        movmem(area, cp, (int) osize);  /* Note addition of (int) */
        if ((char *)current >= area && (char *)current < end)
            *(size_t *)bump(cp, ptrdiff(current,area)) = keep;
    }
    return cp;
}

/*EOF*/