/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by Mike Parker.				    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* *don't* include threaded-malloc.h - we don't want the #defines */

extern char *malloc();
extern char *realloc();

static char *threadhead = 0;

char *th_malloc(nb)
int nb;
{
 char *rv;

 rv = malloc(nb+(2*sizeof(char *)));
 if (rv == 0) return(0);
 ((char **)rv)[0] = threadhead;
 ((char **)rv)[1] = 0;
 if (threadhead) ((char **)threadhead)[1] = rv;
 threadhead = rv;
 return(rv+(2*sizeof(char *)));
}

char *th_realloc(oldptr,newnb)
char *oldptr;
int newnb;
{
 char *old;
 char *new;
 char *prev;
 char *next;

 old = oldptr - (2 * sizeof(char *));
 next = ((char **)old)[0];
 prev = ((char **)old)[1];
 new = realloc(old,newnb+(2*sizeof(char *)));
 if (new == old) return(oldptr);
 if (new == 0)
  { if (next) ((char **)next)[1] = prev;
    if (prev) ((char **)prev)[0] = next; else threadhead = next;
    return(0);
  }
 ((char **)new)[0] = next;
 ((char **)new)[1] = prev;
 if (next) ((char **)next)[1] = new;
 if (prev) ((char **)prev)[0] = new; else threadhead = new;
 return(new+(2*sizeof(char *)));
}

th_free(ptr)
char *ptr;
{
 char *next;
 char *prev;

 ptr -= 2 * sizeof(char *);
 next = ((char **)ptr)[0];
 prev = ((char **)ptr)[1];
 if (next) ((char **)next)[1] = prev;
 if (prev) ((char **)prev)[0] = next; else threadhead = next;
 free(ptr);
}

th_freeall()
{
 register char *cp1;
 register char *cp2;

 for (cp1=threadhead;cp1;cp1=cp2)
  { cp2 = ((char **)cp1)[0];
    free(cp1);
  }
 threadhead = 0;
}
