/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* |_o_o|\\ Copyright (c) 1989 The Software Distillery.                    *
* |. o.| ||          All Rights Reserved                                  *
* | .  | ||          Written by Doug Walker                               *
* | o  | ||          The Software Distillery                              *
* |  . |//           235 Trillingham Lane                                 *
* ======             Cary, NC 27513                                       *
*                    BBS:(919)-471-6436                                   *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "mempriv.h"

extern struct MWGlobal mwg;

void *MWAllocMem(long size, long flags, long ismalloc, char *file, long line)
{
   struct MWAlc *hd;
   char *tmpchar;
   int memtype;

   /* Force warning for malloc'd memory not freed if requested */
   if(mwg.flags & MWF_MALLOCWRN) ismalloc = 0;

   if(!(mwg.flags & MWF_ACTIVE)) return(NULL);
   if(mwg.flags & MWF_CHECK) MWCheck();

   if(size <= 0)
   {
      MWPrintf("\7Bad AllocMem length %ld from line %ld file \"%s\"\n",
         size, line, file);
      return(NULL);
   }

   memtype = (flags & MEMF_CHIP ? MWT_CHIP : MWT_FAST);
   if(mwg.sum[memtype] + size > mwg.lim[memtype])
   {
      /* Over the limit, fail it */
      MWPrintf("MemWatch: ");
      if(memtype == MWT_CHIP)
         MWPrintf("CHIP ");
      else
         MWPrintf("FAST ");
      MWPrintf("memory allocation exceeds MWLimit amount\n");
      return(NULL);
   }

   while(!(tmpchar = (char *)AllocMem(sizeof(struct MWAlc)+size, flags)))
   {
      if(mwg.freed) MWPurge();
      else return(NULL);
   }
   
   hd = (struct MWAlc *)tmpchar;
   hd->size = size;
   hd->flags = flags;
   hd->myflags = (ismalloc ? MWF_ISMALLOC : 0L);
   hd->file = file;
   hd->line = line;
   memcpy(hd->header, MWHEADSTR, MW_HEADLEN);
   memcpy(hd->memory+size, MWTRAILSTR, MW_TRAILLEN);

   if(!(flags & MEMF_CLEAR) && !(mwg.flags & MWF_NOATRASH))
      memset(hd->memory, MWATRASH, size);   /* Trash the memory */

   hd->next = mwg.first;
   mwg.first = hd;

   if((mwg.sum[memtype] += size) > mwg.max[memtype]) 
      mwg.max[memtype] = mwg.sum[memtype];
   ++(mwg.num[memtype]);

   return((char *)hd->memory);
}

void MWFreeMem(void *mem, long size, long internal, 
               char *file, long line)
{
   struct MWAlc *mwa, *prev;
   int memtype;
   int error = 0;

   if(!(mwg.flags & MWF_ACTIVE)) return;
   if(mwg.flags & MWF_CHECK) MWCheck();

   for(prev = NULL, mwa = mwg.first; 
       mwa && mwa->memory != mem; 
       prev = mwa, mwa = mwa->next);

   if(!mwa)
   {
      for(mwa=mwg.freed; mwa && mwa->memory != mem; mwa=mwa->next);
      if(mwa)
      {
         MWPrintf("\7Memory freed twice\n");
         MWPrintAlc(mwa);
      }
      MWPrintf("\7Invalid FreeMem call, addr 0x%08lx length %ld\n"\
                  "                       line %ld file \"%s\"\n",
            mem, size, line, file);
      error = 1;
   }
   else if(!internal && mwa->size != size)
   {
      MWPrintf("\7Incorrect FreeMem length %ld line %ld file \"%s\"\n", 
         size, line, file);
      MWPrintAlc(mwa);
      error = 2;
   }
   else if(MWCheckA(mwa))
     error = 2;

   if(error)
   {
      MWHold();
      if(error == 1) return;
   }

   memtype = (mwa->flags & MEMF_CHIP ? MWT_CHIP : MWT_FAST);
   mwg.sum[memtype] -= mwa->size;
   --mwg.num[memtype];

   if(prev) prev->next = mwa->next;
   else     mwg.first = mwa->next;

   if(!(mwg.flags & MWF_NOFTRASH))
      memset(mwa->memory, MWFTRASH, mwa->size);  /* Trash it */

   if(mwg.flags & MWF_NOFKEEP)
     FreeMem((char *)mwa, mwa->size + sizeof(struct MWAlc));
   else
   {
      mwa->next = mwg.freed;
      mwg.freed = mwa;
   }
}

void *MWrealloc(void *mem, long size, char *file, long line)
{
   void *new;
   struct MWAlc *mwa;

   if(!(new = MWAllocMem(size, 0, 1, file, line)))
      return(NULL);

   for(mwa = mwg.first; 
       mwa && mwa->memory != mem; 
       mwa = mwa->next);

   if(mwa && mwa->size < size) size = mwa->size;

   memcpy(new, mem, size);

   MWFreeMem(mem, 0, 1, file, line);

   return(new);
}
