the c code :
Code: Select all
/*
// PSP
Malloc(x) & Free() Yoshihiro
*/
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <stddef.h>
#include <stdarg.h>
int has_initialized = 0;
void *managed_memory_start;
void *last_valid_address;
struct mem_control_block {
int is_available;
int size;
};
typedef struct BLOCK {
int size;
struct BLOCK *next;
int bucket;
} BLOCK;
char allocbuf[ALLOCSIZE];
char *allocp1 = allocbuf;
register char *stack_ptr asm ("$sp");
static char alloc_buffer[8*1024*1024];
caddr_t
_sbrk_psp (int incr)
{
/* defined */
static char *heap_end = alloc_buffer;
static int total;
char *prev_heap_end;
prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr)
{
abort ();
}
heap_end += incr;
total += incr;
return (caddr_t) prev_heap_end;
}
caddr_t sbrk_psp (int incr) { return _sbrk_psp(incr); }
void malloc_psp_init()
{
last_valid_address = sbrk_psp(0);
managed_memory_start = last_valid_address;
has_initialized = 1;
}
void free_psp(void *firstbyte) {
struct mem_control_block *mcb;
mcb = firstbyte - sizeof(struct mem_control_block);
mcb->is_available = 1;
return;
}
void *malloc_psp(long numbytes) {
void *current_location;
struct mem_control_block *current_location_mcb;
void *memory_location;
if(! has_initialized) {
malloc_psp_init();
}
numbytes = numbytes + sizeof(struct mem_control_block);
memory_location = 0;
current_location = managed_memory_start;
while(current_location != last_valid_address)
{
current_location_mcb =
(struct mem_control_block *)current_location;
if(current_location_mcb->is_available)
{
if(current_location_mcb->size >= numbytes)
{
current_location_mcb->is_available = 0;
memory_location = current_location;
break;
}
}
current_location = current_location +
current_location_mcb->size;
}
if(! memory_location)
{
sbrk_psp(numbytes);
memory_location = last_valid_address;
last_valid_address = last_valid_address + numbytes;
current_location_mcb = memory_location;
current_location_mcb->is_available = 0;
current_location_mcb->size = numbytes;
}
memory_location = memory_location + sizeof(struct mem_control_block);
return memory_location;
}
void *
realloc(void *ptr, size_t size)
{
BLOCK *b;
char *newptr;
int copysize;
if (ptr == 0)
return malloc_psp(size);
b = (BLOCK *)((char *)ptr-4);
copysize = b->size & ~1;
if (size <= copysize)
{
#if 0
if (copysize < 2*MIN_SAVE_EXTRA
|| (size >= copysize-512 && size >= copysize/2))
#endif
return ptr;
copysize = size;
}
newptr = (char *)malloc_psp(size);
memcpy(newptr, ptr, copysize);
free_psp(ptr);
return newptr;
}
Code: Select all
extern "C"
{
void malloc_psp_init(void);
void free_psp(void *firstbyte);
void *malloc_psp(long numbytes);
}
friendly Yoshihiro