| View previous topic :: View next topic |
| Author |
Message |
maroxe
Joined: 01 Sep 2009 Posts: 18
|
Posted: Tue Sep 01, 2009 6:34 pm Post subject: Why memalign? |
|
|
Hi,
I red in many tutorial that i should use memalign to allocate memory for my vertices.
for example, this is my Vertex class:
| Code: | struct Vertex {
float u, v;
unsigned int color;
float x, y, z;
};
|
this is how i should allocate memory:
| Code: | | vertices = (Vertex*) memalign(16, 3*sizeof(Vertex)); |
and this is the call of drawing function:
| Code: | | ceGumDrawArray(GU_TRIANGLES, GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D | GU_TEXTURE_32BITF, 3, 0, vertices); |
I tried to replacte memalign(16, 3*sizeof(Vertex)); by new Vector[3]; ad every thing worked allright. So why memalign? |
|
| Back to top |
|
 |
Ameen

Joined: 14 Jun 2009 Posts: 12 Location: Bahrain
|
Posted: Wed Sep 02, 2009 2:10 am Post subject: |
|
|
I recall using memalign on the PS3 in order to align the data for a faster data transfer using the DMA engine.
I'm not sure, but I think the alignment here is being used for the same concept. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Wed Sep 02, 2009 6:41 am Post subject: |
|
|
Many bits of the PSP hardware require the data to be aligned to certain boundaries. If 'new' worked for you, then you just got lucky.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
psPea
Joined: 01 Sep 2007 Posts: 64
|
Posted: Wed Sep 02, 2009 6:59 am Post subject: |
|
|
I know that malloc allocates memory aligned on 16 bytes boundary, so maybe new does too. _________________ Click ME! |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Wed Sep 02, 2009 9:01 am Post subject: |
|
|
| psPea wrote: | | I know that malloc allocates memory aligned on 16 bytes boundary, so maybe new does too. |
That's not guaranteed! It depends on the implementation, and many allocate on 8 or 4 byte boundaries. Some are 16-byte boundary + 4 (allocate to a 16 byte boundary, then push a long, then return the result). That's why you use memalign instead of malloc/new - to GUARANTEE the boundary you need IS respected by the allocation. |
|
| Back to top |
|
 |
psPea
Joined: 01 Sep 2007 Posts: 64
|
Posted: Wed Sep 02, 2009 9:22 am Post subject: |
|
|
>That's not guaranteed! It depends on the implementation
Well I was talking about the implimentation in the psp toolchain
I saw it on this forum but couldn't I find the thread. _________________ Click ME! |
|
| Back to top |
|
 |
maroxe
Joined: 01 Sep 2009 Posts: 18
|
Posted: Wed Sep 02, 2009 12:16 pm Post subject: |
|
|
thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Wed Sep 02, 2009 12:34 pm Post subject: |
|
|
| maroxe wrote: | thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc? |
Yes. If I remember correctly, the most you can count on for newlib is the largest natural type the CPU handles - for most CPUs, that's double, but on the PSP, it's just long/single.
Someone correct me if I'm wrong on that. :) |
|
| Back to top |
|
 |
maroxe
Joined: 01 Sep 2009 Posts: 18
|
Posted: Wed Sep 02, 2009 12:44 pm Post subject: |
|
|
I red in the doc of sceGuDrawArray:
| Quote: | | Every vertex must align to 32 bits, which means that you HAVE to pad if it does not add up! |
|
|
| Back to top |
|
 |
mrbrown
Joined: 17 Jan 2004 Posts: 1536
|
Posted: Thu Sep 03, 2009 4:24 am Post subject: |
|
|
| J.F. wrote: | | maroxe wrote: | thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc? |
Yes. If I remember correctly, the most you can count on for newlib is the largest natural type the CPU handles - for most CPUs, that's double, but on the PSP, it's just long/single.
Someone correct me if I'm wrong on that. :) |
You can specify the default alignment for malloc() when building newlib with the MALLOC_ALIGNMENT define. This is currently set to 16 bytes - it's that high for precisely the reason this thread was started - people don't often know which data structures need to be aligned for hardware.
I wanted to point out though, that when dealing with an array of structs allocated via new, you have to be extremely careful about passing them off to hardware. The compiler will insert a bit of housekeeping data at the beginning of your allocation, so that it can properly destroy things when you call delete[]. IOW, you can't count on 128-bit alignment when using array new.
Anyway, if sceGuDrawArray() requires 32-bit alignment then it shouldn't matter because whatever housekeeping that array new tacks on will likely be at least 32-bit aligned. _________________ "He was warned..." |
|
| Back to top |
|
 |
maroxe
Joined: 01 Sep 2009 Posts: 18
|
Posted: Thu Sep 03, 2009 4:49 am Post subject: |
|
|
i steal don't understand, if a data is 32bits aligned, it will be 16bits aligned. So why in every tutorial for PSP we use memalign(16, ...)?
Also, the list passed to sceGu, f don't specify "__attribute__((aligned(16)))" no vertexis shown at the screen |
|
| Back to top |
|
 |
mrbrown
Joined: 17 Jan 2004 Posts: 1536
|
Posted: Thu Sep 03, 2009 5:05 am Post subject: |
|
|
The argument passed to memalign() and __attribute__((aligned())) is specified in bytes, not bits.
The default alignment for PSP newlib malloc() is 16 bytes, or 128 bits. _________________ "He was warned..." |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Thu Sep 03, 2009 8:00 am Post subject: |
|
|
| mrbrown wrote: | | The default alignment for PSP newlib malloc() is 16 bytes, or 128 bits. |
That's good to know. As long as it stays that way, a malloc or new is fine. |
|
| Back to top |
|
 |
maroxe
Joined: 01 Sep 2009 Posts: 18
|
Posted: Thu Sep 03, 2009 11:56 pm Post subject: |
|
|
| Thank's all, it's clearer know |
|
| Back to top |
|
 |
|