/*
 *  Support routines for for the GNU regular expression package.
 *  Edwin Hoogerbeets 18/07/89
 *
 *  This file may be copied and distributed under the GNU Public
 *  Licence. See the comment at the top of regex.c for details.
 *
 *  Adapted from Elib by Jim Mackraz, mklib by Edwin Hoogerbeets, and the
 *  GNU regular expression package by the Free Software Foundation.
 */

#include <exec/memory.h>
#define memcpy(dst,src,n) movmem((src),(dst),(n))

extern long *AllocMem();
extern void FreeMem();

char *malloc(size)
long size;
{
  /* good temp's are hard to come by... */
  long *temp = AllocMem(size + sizeof(long), MEMF_PUBLIC|MEMF_CLEAR);

  if ( temp ) {
    temp[0] = size;
    ++temp;
  }

  return((char *)temp);
}

void free(NelsonMandala)
long *NelsonMandala;
{
  if ( NelsonMandala ) {
    FreeMem((char *) &NelsonMandala[-1], NelsonMandala[-1] + sizeof(long) );
  }
}

/* Protect our programming environment! Join a memory realloc program! */
char *realloc(source,size)
char *source;
long size;
{
  char *destination = malloc(size);

  if ( destination ) {
    memcpy(destination,source,size);
    free(source);

    return(destination);
  } else {
    return(NULL);
  }
}

/* define TESTMALLOC to create a stand alone program that tests the
 * above routines
 */

#ifdef TESTMALLOC
#include <stdio.h>

main()
{
  long *foo;

  printf("doing malloc\n");
  foo = (long *) malloc(100);
  printf("malloc of 100 bytes gives pointer: %lx with %ld at -1\n",
         foo,foo[-1]);

  foo[42] = 42L;

  printf("doing realloc\n");

  foo = (long *) realloc((char *) foo, 200);

  printf("realloc to 200 bytes gives pointer: %lx with %ld at -1\n",
         foo,foo[-1]);
  printf("and position 42 contains %ld\n",foo[42]);


  printf("doing free\n");

  free(foo);
}
#endif


