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 

pointer arithmetic / casting problem

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



Joined: 17 Oct 2005
Posts: 71
Location: New York

PostPosted: Tue Jan 31, 2006 6:55 am    Post subject: pointer arithmetic / casting problem Reply with quote

While porting a game from lynux I got stock with a runtime error (blue screen of death), pointing to line 54 (marked by a comment in code) of following function:
Code:

void *Z_Malloc (int size, int tag, void *user)
{
        int             extra;
        memblock_t      *start, *rover, *new, *base;
//
// scan through the block list looking for the first free block
// of sufficient size, throwing out any purgable blocks along the way
//
        size += sizeof(memblock_t);     // account for size of block header


//
// if there is a free block behind the rover, back up over them
//
        base = mainzone->rover;
        if (!base->prev->user)
                base = base->prev;

        rover = base;
        start = base->prev;

        do
        {
                if (rover == start)     // scaned all the way around the list
                        {
                        SoftError("OHSHIT\n");
                        Z_DumpHeap(0,200);
                        Error ("Z_Malloc: failed on allocation of %i bytes",size);
                        }
                if (rover->user)
                {
                        if (rover->tag < PU_PURGELEVEL)
                        // hit a block that can't be purged, so move base past it
                                base = rover = rover->next;
                        else
                        {
                        // free the rover block (adding the size to base)
                                base = base->prev;      // the rover can be the base block
                                Z_Free ((byte *)rover+sizeof(memblock_t));
                                base = base->next;
                                rover = base->next;
                        }
                }
                else
                        rover = rover->next;
        } while (base->user || base->size < size);

//
// found a block big enough
//
        extra = base->size - size;
        if (extra >  MINFRAGMENT)
        {       // there will be a free fragment after the allocated block
                new = (memblock_t *) ((byte *)base + size );
                new->size = extra;          //!!!EXECUTION BREAKS HERE!!!
                new->user = NULL;               // free block
                new->tag = 0;
                new->prev = base;
                new->next = base->next;
                new->next->prev = new;
                base->next = new;
                base->size = size;
        }

        if (user)
        {
                base->user = user;                      // mark as an in use block
                *(void **)user = (void *) ((byte *)base + sizeof(memblock_t));
        }
        else
        {
                if (tag >= PU_PURGELEVEL)
                        Error ("Z_Malloc: an owner is required for purgable blocks");
                base->user = (void *)2;         // mark as in use, but unowned
        }
        base->tag = tag;

        mainzone->rover = base->next;   // next allocation will start looking here


        return (void *) ((byte *)base + sizeof(memblock_t));
}




Code:

typedef struct memblock_s
{
         int     size;   // including the header and possibly tiny fragments
         void    **user; // NULL if a free block
         int     tag;    // purgelevel
         struct memblock_s   *next, *prev;
} memblock_t;

typedef struct
{
         int     size;          // total bytes malloced, including header
         memblock_t  blocklist; // start / end cap for linked list
         memblock_t  *rover;
} memzone_t;




Does anyone know what may have gone wrong?
Same code runs fine under cygwin.

Thank you in advance
Back to top
View user's profile Send private message
charliex



Joined: 26 Jan 2006
Posts: 16

PostPosted: Tue Jan 31, 2006 7:06 am    Post subject: Reply with quote

is size odd ? or does new end up odd
Back to top
View user's profile Send private message
deniska



Joined: 17 Oct 2005
Posts: 71
Location: New York

PostPosted: Tue Jan 31, 2006 7:17 am    Post subject: Reply with quote

charliex wrote:
is size odd ? or does new end up odd

Yeah,
It get's called with:
Z_Malloc ( 5, 1,..)
Back to top
View user's profile Send private message
charliex



Joined: 26 Jan 2006
Posts: 16

PostPosted: Tue Jan 31, 2006 7:33 am    Post subject: Reply with quote

can't use an unaligned pointer like that, the function needs to be changed to align addresses from the pool.
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