my own malloc for psp

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
Yoshihiro
Posts: 12
Joined: Sat May 14, 2005 12:17 am

my own malloc for psp

Post by Yoshihiro »

hi i've make a malloc for psp that can be optimized it's jus for help :)

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 &#123;

	int is_available;

	int size;

&#125;;

typedef struct BLOCK &#123;
  int size;
  struct BLOCK *next;
  int bucket;
&#125; BLOCK;


char allocbuf&#91;ALLOCSIZE&#93;;

char *allocp1 = allocbuf;

register char *stack_ptr asm &#40;"$sp"&#41;;
static char alloc_buffer&#91;8*1024*1024&#93;;

caddr_t
_sbrk_psp &#40;int incr&#41;
&#123;
/* defined */
  static char *heap_end = alloc_buffer;
  static int total;
  char *prev_heap_end;

  prev_heap_end = heap_end;
  if &#40;heap_end + incr > stack_ptr&#41;
    &#123;
     abort &#40;&#41;;
    &#125;
  heap_end += incr;
  total += incr;
  return &#40;caddr_t&#41; prev_heap_end;
&#125;

caddr_t sbrk_psp &#40;int incr&#41; &#123; return _sbrk_psp&#40;incr&#41;; &#125;


void malloc_psp_init&#40;&#41;

&#123;

	last_valid_address = sbrk_psp&#40;0&#41;;

	managed_memory_start = last_valid_address;

 	has_initialized = 1;

&#125;

void free_psp&#40;void *firstbyte&#41; &#123;

	struct mem_control_block *mcb;


	mcb = firstbyte - sizeof&#40;struct mem_control_block&#41;;

	mcb->is_available = 1;

	return;
&#125;

void *malloc_psp&#40;long numbytes&#41; &#123;

	void *current_location;


	struct mem_control_block *current_location_mcb;

	
	void *memory_location;

	
	if&#40;! has_initialized&#41; 	&#123;

		malloc_psp_init&#40;&#41;;

	&#125;

	
	numbytes = numbytes + sizeof&#40;struct mem_control_block&#41;;

	
	memory_location = 0;

	current_location = managed_memory_start;

	while&#40;current_location != last_valid_address&#41;

	&#123;

		current_location_mcb =

			&#40;struct mem_control_block *&#41;current_location;

		if&#40;current_location_mcb->is_available&#41;

		&#123;

			if&#40;current_location_mcb->size >= numbytes&#41;

			&#123;

		
				current_location_mcb->is_available = 0;

				memory_location = current_location;

				break;

			&#125;

		&#125;

		current_location = current_location +

			current_location_mcb->size;

	&#125;

	
	if&#40;! memory_location&#41;

	&#123;

		sbrk_psp&#40;numbytes&#41;;

		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;

	&#125;

	
	memory_location = memory_location + sizeof&#40;struct mem_control_block&#41;;

	
	return memory_location;

 &#125;


void *
realloc&#40;void *ptr, size_t size&#41;
&#123;
  BLOCK *b;
  char *newptr;
  int copysize;

  if &#40;ptr == 0&#41;
    return malloc_psp&#40;size&#41;;

  b = &#40;BLOCK *&#41;&#40;&#40;char *&#41;ptr-4&#41;;
  copysize = b->size & ~1;
  if &#40;size <= copysize&#41;
  &#123;
#if 0
    if &#40;copysize < 2*MIN_SAVE_EXTRA
	|| &#40;size >= copysize-512 && size >= copysize/2&#41;&#41;
#endif
      return ptr;
    copysize = size;
  &#125;

  newptr = &#40;char *&#41;malloc_psp&#40;size&#41;;

  memcpy&#40;newptr, ptr, copysize&#41;;
  free_psp&#40;ptr&#41;;
  

  return newptr;
&#125;
the header for the cpp code :

Code: Select all

extern "C"
&#123;
void malloc_psp_init&#40;void&#41;;

void free_psp&#40;void *firstbyte&#41;;

void *malloc_psp&#40;long numbytes&#41;;
&#125;

friendly Yoshihiro
Last edited by Yoshihiro on Mon Jun 06, 2005 9:58 am, edited 1 time in total.
Image
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

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?
fcorbier
Posts: 1
Joined: Sat Jun 25, 2005 4:18 am

Post by fcorbier »

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: Select all

numbytes = numbytes + sizeof&#40;struct mem_control_block&#41;; 
to:

Code: Select all

numbytes = &#40;&#40;numbytes+3&#41;&~0x3&#41; + sizeof&#40;struct mem_control_block&#41;; 
so that allocated memory are always 4-bytes aligned.

-fcorbier
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

fcorbier wrote:so that allocated memory are always 4-bytes aligned.
Does MIPS need to be byte-aligned like SPARC?
ector
Posts: 195
Joined: Thu May 12, 2005 10:22 pm

Post by ector »

Agoln wrote: Does MIPS need to be byte-aligned like SPARC?
Yes.
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

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...
User avatar
Yoshihiro
Posts: 12
Joined: Sat May 14, 2005 12:17 am

Post by Yoshihiro »

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: Select all

numbytes = numbytes + sizeof&#40;struct mem_control_block&#41;; 
to:

Code: Select all

numbytes = &#40;&#40;numbytes+3&#41;&~0x3&#41; + sizeof&#40;struct mem_control_block&#41;; 
so that allocated memory are always 4-bytes aligned.

-fcorbier
Thank's for your fix fcorbier :D.


..::Yoshihiro::..
Image
Post Reply