| View previous topic :: View next topic |
| Author |
Message |
Yoshihiro

Joined: 14 May 2005 Posts: 12
|
Posted: Sat Jun 04, 2005 1:02 am Post subject: my own malloc for psp |
|
|
hi i've make a malloc for psp that can be optimized it's jus for help :)
the c code :
| Code: |
/*
// 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;
}
|
the header for the cpp code :
| Code: |
extern "C"
{
void malloc_psp_init(void);
void free_psp(void *firstbyte);
void *malloc_psp(long numbytes);
}
|
friendly Yoshihiro _________________
.jpg)
Last edited by Yoshihiro on Mon Jun 06, 2005 9:58 am; edited 1 time in total |
|
| Back to top |
|
 |
subbie
Joined: 05 May 2005 Posts: 122
|
Posted: Sat Jun 04, 2005 4:29 am Post subject: |
|
|
cool.
Am I right in assuming that this line will effect how much we can allocate from? "static char alloc_buffer[8*1024*1024]; "
Also how is fragmentation. If we free all will we have access to the whole buffer size again or will it be broken into fragmented pieces? |
|
| Back to top |
|
 |
fcorbier
Joined: 25 Jun 2005 Posts: 1
|
Posted: Sat Jun 25, 2005 4:46 am Post subject: |
|
|
Hey Yoshihiro,
I'm using your malloc in my PSPRick project. It works fine but I made a small modification for it. I changed:
| Code: | | numbytes = numbytes + sizeof(struct mem_control_block); |
to:
| Code: | | numbytes = ((numbytes+3)&~0x3) + sizeof(struct mem_control_block); |
so that allocated memory are always 4-bytes aligned.
-fcorbier |
|
| Back to top |
|
 |
Agoln

Joined: 08 Jun 2005 Posts: 326 Location: Fort Wayne, IN
|
Posted: Sat Jun 25, 2005 5:27 am Post subject: |
|
|
| fcorbier wrote: | | so that allocated memory are always 4-bytes aligned. |
Does MIPS need to be byte-aligned like SPARC? |
|
| Back to top |
|
 |
ector
Joined: 12 May 2005 Posts: 195
|
Posted: Sat Jun 25, 2005 6:10 am Post subject: |
|
|
| Agoln wrote: |
Does MIPS need to be byte-aligned like SPARC? |
Yes. |
|
| Back to top |
|
 |
Agoln

Joined: 08 Jun 2005 Posts: 326 Location: Fort Wayne, IN
|
Posted: Sat Jun 25, 2005 6:29 am Post subject: |
|
|
Well, then thanks for the patch note for making it byte-aligned. I tried debugging a bus error for HOURS on end trying to figure out what was wrong before learning about byte-alignment and why I only got the errors on the SPARC machines.........
I HATE bus errors... |
|
| Back to top |
|
 |
Yoshihiro

Joined: 14 May 2005 Posts: 12
|
Posted: Mon Jun 27, 2005 8:06 am Post subject: |
|
|
| fcorbier wrote: | Hey Yoshihiro,
I'm using your malloc in my PSPRick project. It works fine but I made a small modification for it. I changed:
| Code: | | numbytes = numbytes + sizeof(struct mem_control_block); |
to:
| Code: | | numbytes = ((numbytes+3)&~0x3) + sizeof(struct mem_control_block); |
so that allocated memory are always 4-bytes aligned.
-fcorbier |
Thank's for your fix fcorbier :D.
..::Yoshihiro::.. _________________
.jpg) |
|
| Back to top |
|
 |
|