/*
(c) Copyright 1988-1994 Microsoft Corporation.

Microsoft Bars&Pipes Professional Source Code License

This License governs use of the accompanying Software.  
Microsoft hopes you find this Software useful.

You are licensed to do anything you want with the Software. 

In return, we simply require that you agree:

1.      Not to remove any copyright notices from the Software.

2.      That the Software comes "as is", with no warranties.  
        None whatsoever. This means no implied warranty of merchantability or 
        fitness for a particular purpose or any warranty of non-infringement.  
        Also, you must pass this disclaimer on whenever you distribute the Software.

3.      That we will not be liable for any of those types of damages known as indirect, 
        special, consequential, or incidental related to the Software or this License, 
        to the maximum extent the law permits. Also, you must pass this limitation of 
        liability on whenever you distribute the Software.

4.      That if you sue anyone over patents that you think may apply to the Software 
        for that person’s use of the Software, your license to the Software ends automatically.

5.      That the patent rights Microsoft is licensing only apply to the Software, 
        not to any derivatives you make.

6.      That your rights under the License end automatically if you breach this in any way.
*/

#include "/CompilerSpecific.h"
#include <exec/types.h>
#include <exec/exec.h>
#include <proto/exec.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <string.h>
#include "all.h"

struct Cow {
    struct Cow *next;
    char *item;
    long itemsize;
};

static struct Cow *cowlist[64] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

static hash(in)

register long in;

{
    in = in >> 3;
    in &= 63;
    return(in);
}

#ifndef NO_DEBUG_MEMORY
#undef AllocMem
#undef FreeMem
#endif

SAVEDS  char *myalloc(size,flags)

long size, flags;

{
    register struct Cow *cow;
    register char *item;
    register unsigned short hashindex;
    /* geta4(); */
    cow = (struct Cow *) AllocMem(sizeof(struct Cow),MEMF_PUBLIC);
    item = (char *) AllocMem(size+1,flags);
    hashindex = hash((long)item);
    if (item && cow) {
        Forbid();
        cow->item = item;
        cow->itemsize = size;
        cow->next = cowlist[hashindex];
        cowlist[hashindex] = cow;
        Permit();
    }
    else {
        if (cow) FreeMem(cow,sizeof(struct Cow));
        if (item) FreeMem(item,size+1);
        item = 0;
    }
    return(item);
}

static memerrors = 0;
static memmiss = 0;

SAVEDS  void myfree(item,size)

register char *item;
long size;

{
    register unsigned short hashindex;
    register struct Cow *cow;
    register struct Cow *scan;
    /* geta4(); */
    hashindex = hash((long)item);
    scan = 0;
    Forbid();
    cow = cowlist[hashindex];
    for (;cow;cow = cow->next) {
        if (item == cow->item) {
            if (cow->itemsize != size) {
                memerrors++;
        dprintf("ERROR: Freeing %lx of size %ld instead of requested %ld\n",
                    item,cow->itemsize,size);
            }
/*setmem(item,1,cow->itemsize);*/
            FreeMem(item,cow->itemsize+1);
            if (!scan) cowlist[hashindex] = cow->next;
            else scan->next = cow->next;
            FreeMem(cow,sizeof(struct Cow));
            Permit();
            return;
        }
        scan = cow;
    }
    dprintf("ERROR: Couldn't find item %lx of size %ld, string %ls to free\n",
item,size,&item[2]);
    memmiss++;
    Permit();
}

/* checkmemory(record)

char *record;

{
    register struct Cow *cow = cowlist;
    register long test = (long) record;
    if (test & 3) return(0);
    if (!test) return(1);
    for (;cow;cow = cow->next) {
        if (cow->item == record) return(1);
    }
    return(0);
}
*/
/*static void dumpdata(data,size)

unsigned char *data;
unsigned short size;

{
    short i,j;
    char text[20];
    unsigned long d;
    for (i=0;i<size;i+= 16) {
        for (j=0;j<16;j++) {
            d = data[i+j];
            text[j] = d;
            printf("%lx ",d);
        }
        text[16] = 0;
        printf(" %ls\n",text);
    }
}
*/
void freeallmemory()

{
    register struct Cow *cow;
    register struct Cow *next;
    register unsigned short hashindex;
    for (hashindex = 0;hashindex < 64;hashindex++) {
        cow = cowlist[hashindex];
        cowlist[hashindex] = 0;
        for (;cow;) {
            next = cow->next;
Printf("ERROR: Unfreed item %lx, size %ld string:%ls\n",cow->item,cow->itemsize,
&cow->item[2]);
/*dumpdata(cow->item,cow->itemsize);*/
            FreeMem(cow->item,cow->itemsize+1);
            FreeMem(cow,sizeof(struct Cow));
            cow = next;
        }
    }
    printf("Total number of wrong size free mems: %ld\n",memerrors);
    printf("Total number of wrong addressed free mems: %ld\n",memmiss);
}    

void realFreeMem(pointer,size)

char *pointer;
long size;

{
    FreeMem(pointer,size);
}

char *realAllocMem(size,mask)

long size, mask;

{
    return(AllocMem(size,mask));
}

