/*
 * Stand-Alone RTLinux Memory Allocator
 * 
 * Written by Vicente Esteve LLoret
 * Released under the terms of the GPL Version 2
 *
 */

#include <arch/e820.h>
#include <arch/mprot.h>
#include <arch/page.h>
#include <rtl_malloc.h>
#include <rtl_conf.h>
#include <ctype.h>


#if CONFIG_KERNEL_MEMORYPROT
extern char _init_allocspace;
static char *endmem=(char *) &_init_allocspace;
#else
extern char _end;
//static char *endmem=(char *) &_end;
static char *endmem=(char *) 1500000;
#endif

char *kmalloc(unsigned int size,int type)
{
#if 0
	char *ptr =  endmem;

#if CONFIG_CONTEXT_MEMORYPROT
  if (size & RTL_PAGE_MASK)
     endmem += ((size + RTL_PAGE_SIZE) & RTL_PAGE_MASK);
  else
     endmem += size;
#else
  endmem += size;
#endif
#endif
  return rtl_malloc(size);
};

//***************** LIBC Compatibility **************************

void *malloc(size_t size) {
  return rtl_malloc(size);
};

void *realloc(void *ptr,size_t new_len) {
  return rtl_realloc(ptr,new_len);
};

void *calloc(size_t nelem,size_t elem_size) {
  return rtl_calloc(nelem,elem_size);
};

void free(void *ptr) {
  rtl_free(ptr);	
};

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

unsigned long GetMemorySize(void)
{
  return ((*((unsigned long *) 0xf00) + 1024) * 1024 ); 
};


void init_memory(void)
{
  init_memory_pool(0,5,1024*10,endmem);
  associate_buffer(endmem);
}

