/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id: malloc.c,v 1.8 2003/04/16 07:21:11 reddawg Exp $

**************************************************************************************/

#include <sys/types.h>
#include <sys/sys.h>
#include <stdio.h>

struct memDescriptor {
  struct memDescriptor *prev;        //4
  struct memDescriptor *next;        //4
  void                 *baseAddr;    //4
  unsigned long        limit;        //4
  unsigned char        status;       //1
  char                 reserved[15]; //15
  };

struct memDescriptor *kernDesc      = 0x0;
struct memDescriptor *freeKernDesc  = 0x0;
struct memDescriptor *emptyKernDesc = 0x0;

void insertFreeDesc(struct memDescriptor *freeDesc);

void initMalloc() {
  int i=0;
  struct memDescriptor *tmpDesc1 = 0x0;
  struct memDescriptor *tmpDesc2 = 0x0;
  emptyKernDesc = (struct memDescriptor *)getPage(1);
  tmpDesc1 = emptyKernDesc;
  tmpDesc1->prev = 0x0;
  for (i=1;i<((4096/sizeof(struct memDescriptor)));i++) {
    tmpDesc2 = &emptyKernDesc[i];
    tmpDesc2->prev = tmpDesc1;
    tmpDesc1->next = tmpDesc2;
    tmpDesc1 = tmpDesc2;
    }
  tmpDesc1->next = 0x0;
  //Return
  return;
  }

void *getEmptyDesc() {
  struct memDescriptor *tmpDesc = emptyKernDesc;
  if (tmpDesc != 0x0) {
    emptyKernDesc       = tmpDesc->next;
    emptyKernDesc->prev = 0x0;
    tmpDesc->next       = 0x0;
    tmpDesc->prev       = 0x0;
    return(tmpDesc);
    }
  printf("Error Finding Empty Descriptor!\n");
  return(0x0);
  }

void *malloc(uInt len) {
  struct memDescriptor *tmpDesc1 = 0x0;
  struct memDescriptor *tmpDesc2 = 0x0;
  //If Kernel Descriptor Is NULL Initialize Malloc
  if (emptyKernDesc == 0x0) {
    initMalloc();
    }
  len = (len + 15) & 0xFFFFFFF0;
  for (tmpDesc1 = freeKernDesc;tmpDesc1;tmpDesc1=tmpDesc1->next) {
    if (tmpDesc1->limit >= len) {
      tmpDesc1->status = 0x1;
      if (tmpDesc1->prev != 0x0) {
        tmpDesc1->prev->next = tmpDesc1->next;
        tmpDesc1->next->prev = tmpDesc1->prev;
        }
      else {
        freeKernDesc = tmpDesc1->next;
        freeKernDesc->prev = 0x0;
        }
      tmpDesc1->prev = 0x0;
      tmpDesc1->next = kernDesc;
      kernDesc->prev = tmpDesc1;
      kernDesc = tmpDesc1;
      if (tmpDesc1->limit > (len + 16)) {
        tmpDesc2 = getEmptyDesc();
        tmpDesc2->limit = tmpDesc1->limit - len;
        tmpDesc1->limit = len;
        tmpDesc2->baseAddr = tmpDesc1->baseAddr + len;
        tmpDesc2->status = 0x0;
        insertFreeDesc(tmpDesc2);
        }
      return(tmpDesc1->baseAddr);
      }
    }
  tmpDesc1 = getEmptyDesc();
  if (tmpDesc1 != 0x0) {
    tmpDesc1->baseAddr = (struct memDescriptor *)getPage((len+4095)/4096);
    tmpDesc1->limit    = len;
    tmpDesc1->status   = 0x1;
    tmpDesc1->next     = kernDesc;
    tmpDesc1->prev     = 0x0;
    if (kernDesc != 0x0) {
      kernDesc->prev = tmpDesc1;
      }
    kernDesc           = tmpDesc1;
    if ((len/4096) > 0) {
      tmpDesc2           = getEmptyDesc();
      tmpDesc2->status   = 0x0;
      tmpDesc2->baseAddr = tmpDesc1->baseAddr + tmpDesc1->limit;
      tmpDesc2->limit    = ((len + 4095)/4096)*4096 - tmpDesc1->limit;
      insertFreeDesc(tmpDesc2);
      }
    /*
    printf("[0x");
    printf("%X",tmpDesc1->baseAddr);
    printf("]\n");
    */
    return(tmpDesc1->baseAddr);
    }
  //Return Null If Unable To Malloc
  return(0x0);
  }

void free(void *baseAddr) {
  long *data = 0x0;
  long i     = 0x0;
  struct memDescriptor *tmpDesc1 = 0x0;
  struct memDescriptor *tmpDesc2 = 0x0;

  if (baseAddr == 0x0) {
    return;
    }
  for (tmpDesc1=kernDesc;tmpDesc1;tmpDesc1=tmpDesc1->next) {
    if (tmpDesc1->baseAddr == baseAddr) {
      tmpDesc1->status = 0x0;
      if (tmpDesc1->prev) {
        tmpDesc2 = tmpDesc1->prev;
        tmpDesc2->next = tmpDesc1->next;
        }
      if (tmpDesc1->next) {
        tmpDesc2 = tmpDesc1->next;
        if (tmpDesc2) {
          tmpDesc2->prev = tmpDesc1->prev;
          }
        }
      if ((kernDesc == tmpDesc1) && (tmpDesc1->prev)) {
        kernDesc = tmpDesc1->prev;
        }
      else {
        kernDesc = tmpDesc1->next;
        }
      insertFreeDesc(tmpDesc1);
      data = baseAddr;
      for (i=0;i < (tmpDesc1->limit/4);i++) {
        data[i] = 0x0;
        }
      return;
      }
    }
  printf("Error Freeing User Descriptor! [0x%X]\n",(unsigned int) baseAddr);
  return;
  }

void insertFreeDesc(struct memDescriptor *freeDesc) {
  struct memDescriptor *tmpDesc;
  freeDesc->status = 0x0;
  if (freeKernDesc != 0x0) {
    for (tmpDesc=freeKernDesc;tmpDesc;tmpDesc=tmpDesc->next) {
      if ((freeDesc->limit >= tmpDesc->limit) && (!tmpDesc->next)) {
        tmpDesc->next  = freeDesc;
        freeDesc->prev = tmpDesc;
        freeDesc->next = 0x0;
        return;
        }
      else if ((freeDesc->limit >= tmpDesc->limit) && (freeDesc->limit <= tmpDesc->next->limit)) {
        freeDesc->next      = tmpDesc->next;
        freeDesc->prev      = tmpDesc;
        tmpDesc->next->prev = freeDesc;
        tmpDesc->next       = freeDesc;
        return;
        }
      }
    }
  else {
    freeDesc->prev = 0x0;
    freeDesc->next = 0x0;
    freeKernDesc = freeDesc;
    }
  return;
  } 