forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

my own malloc for psp

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Yoshihiro



Joined: 14 May 2005
Posts: 12

PostPosted: Sat Jun 04, 2005 1:02 am    Post subject: my own malloc for psp Reply with quote

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
_________________


Last edited by Yoshihiro on Mon Jun 06, 2005 9:58 am; edited 1 time in total
Back to top
View user's profile Send private message
subbie



Joined: 05 May 2005
Posts: 122

PostPosted: Sat Jun 04, 2005 4:29 am    Post subject: Reply with quote

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
View user's profile Send private message
fcorbier



Joined: 25 Jun 2005
Posts: 1

PostPosted: Sat Jun 25, 2005 4:46 am    Post subject: Reply with quote

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
View user's profile Send private message
Agoln



Joined: 08 Jun 2005
Posts: 326
Location: Fort Wayne, IN

PostPosted: Sat Jun 25, 2005 5:27 am    Post subject: Reply with quote

fcorbier wrote:
so that allocated memory are always 4-bytes aligned.


Does MIPS need to be byte-aligned like SPARC?
Back to top
View user's profile Send private message AIM Address
ector



Joined: 12 May 2005
Posts: 195

PostPosted: Sat Jun 25, 2005 6:10 am    Post subject: Reply with quote

Agoln wrote:

Does MIPS need to be byte-aligned like SPARC?


Yes.
Back to top
View user's profile Send private message
Agoln



Joined: 08 Jun 2005
Posts: 326
Location: Fort Wayne, IN

PostPosted: Sat Jun 25, 2005 6:29 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
Yoshihiro



Joined: 14 May 2005
Posts: 12

PostPosted: Mon Jun 27, 2005 8:06 am    Post subject: Reply with quote

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::..
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group