svxs
Joined: 19 Jun 2009 Posts: 15
|
Posted: Fri Jun 19, 2009 10:18 am Post subject: Can't allocate 1 MB, but can alloc 2? |
|
|
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? |
|