#define _tapwave_os_stuff_

#include "palmdep.h"
#include "endianutils.h"
#include "pnobridge.h"
#include "oscompat.h"
#include "StarterRsc.h"

int errno = 0;

char *strdup(const char *s) {
	char *str;
	str=(char *)malloc(StrLen(s)+1);
	strcpy(str,s);
	return str;
}

extern UInt32 currentOwnerID;
void*
__sys_alloc(size_t size)
{
    void * ptr = MemChunkNew(0,size,0x1200|currentOwnerID);//MemGluePtrNewL(size);//MemPtrNew(size);    
    if (ptr == NULL){
    	char msg[32];
    	int bytes,max;
    	sprintf(msg,"cannot sys_alloc %d Bytes.",size);
    	DEBUGS(msg);
//    	menu_info(" ");
    	MemHeapFreeBytes ( 0, &bytes, &max );
	  	sprintf(msg,"free ram : %d/%d",bytes>>10,max>>10);
    	DEBUGS(msg);
//		menu_info(" ");
	    //ErrFatalDisplayIf(ptr == NULL, msg);
	}
    return ptr;
}

void
__sys_free(void* ptr)
{
    (void) MemChunkFree/*MemPtrFree*/(ptr);
}

size_t
__sys_pointer_size(void* ptr)
{
    return (size_t) MemPtrSize(ptr);
}

void* MyRealloc(void* ptr, UInt32 size)
{
	size_t orig_size;
	void* p;
	if (ptr == 0)
		return malloc(size);
	if (size == 0)
	{
		free(ptr);
		return 0;
	}
	orig_size = __sys_pointer_size(ptr);
	p = malloc(size);
	if (p != 0)
	{
		orig_size = orig_size < size ? orig_size : size;
		memcpy(p, ptr, orig_size);
		free(ptr);
	}
	return p;
}