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 

Can't allocate 1 MB, but can alloc 2?

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



Joined: 19 Jun 2009
Posts: 15

PostPosted: Fri Jun 19, 2009 10:18 am    Post subject: Can't allocate 1 MB, but can alloc 2? Reply with quote

It's the weirdest problem.

Code:

  #define ALIGNED(nBytes) __attribute__((aligned(nBytes)))

  // types:
  typedef unsigned char byte_t;
  // uint32_t, etc, are from <stdint.h> (this is c99 code)

  struct dim2d
  {
    uint32_t y;                                                    /* Height */
    uint32_t x;                                                     /* Width */
  };

  struct mipmap
  {
    struct dim2d xy;                                    /* Mipmap dimensions */
    byte_t ALIGNED (16) data[];                                /* Pixel data */
  };

  ...

  pImage->p_mipmaps[i] = malloc (sizeof (struct mipmap) + size);
  if ( pImage->p_mipmaps[i] == NULL )
    {
      // ignore this for now
      // for ( uint32_t b = 0; b < i; b++ )
      //  free (pImage->p_mipmaps[b]);

      pspDebugScreenPrintf ("Couldn't allocate %i bytes.\n"
                 , sizeof (struct mipmap) + size);

      for ( uint32_t i = 1; i < 5; i++ )
      {
        byte_t *p_x = malloc (i * 1024 * 1024);
        if ( p_x == NULL )
          pspDebugScreenPrintf ("Couldn't allocate %i bytes.\n"
                     , i * 1024 * 1024);
        else
        {
          pspDebugScreenPrintf ("COULD allocate %i bytes.\n"
                     , i * 1024 * 1024);
          free (p_x);
        }
      }

      return DDS_OUT_OF_MEMORY;
    }

  ...


will display:

Code:

  Couldn't allocate 8208 bytes.
  Couldn't allocate 1048576 bytes.
  COULD allocate 2091752 bytes.
  COULD allocate 3145728 bytes.
  COULD allocate 4194304 bytes.


It only happens after I've already done a lot of malloc() and free()'s. Otherwise, it works fine. It also works fine if I change it up weirdly:

Code:

  ...
  struct mipmap
  {
    struct dim2d xy;                                   /* Mipmap dimensions */
    // Got rid of ALIGNED (16) from this:
    byte_t data[];                                             /* Pixel data */
  };
  ...
  // Use memalign, align to 64 (16 still breaks):
  pImage->p_mipmaps[i] = memalign (64, sizeof (struct mipmap) + size);


Any ideas what's going wrong?
Back to top
View user's profile Send private message
svxs



Joined: 19 Jun 2009
Posts: 15

PostPosted: Sat Jun 20, 2009 6:10 am    Post subject: Reply with quote

Turned out to be some heap corruption from an off-by-one error in another function that was being called repeatedly before this chunk of code. Works fine after I found that.
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