Manual Data Alignment in Cocoa - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Manual Data Alignment in Cocoa (/thread-7705.html)
Manual Data Alignment in Cocoa - geezusfreeek - Jun 13, 2002 04:42 PM
I know that in Classic, the best way to manually align your data is by allocating a block of memory larger than you need and calculating an aligned pointer inside that block. Is there an equivelent to NewPtr() from Classic in Cocoa that allows you to specify how large the block of memory is? I would think that such a basic concept should be easy to find, but I can't find it anywhere.
Oops, I put this in the wrong section. Oh well. I don't feel like copying it over and it would just be more annoying if it was in two places. Sorry.
Manual Data Alignment in Cocoa - geezusfreeek - Jun 13, 2002 05:12 PM
I miraculously found something called NSAllocateMemoryPages inside the Foundation framework that allows you to allocate a specific number of bytes in memory, but the return type is void. How are you supposed to obtain a pointer to the block you just allocated? Now I'm really confused.
Manual Data Alignment in Cocoa - OneSadCookie - Jun 13, 2002 05:37 PM
You probably want malloc (and remember to free the pointer after). There is also calloc which is like NewPtrClear. Both return pointers to 16-byte aligned memory on MacOSX. #include <stdlib.h>
There's also valloc (#include <unistd.h>), which allocates page-aligned (ie 4K-aligned) blocks.
If you're using Cocoa, you can use Cocoa's own internal memory management stuff to get an autoreleased pointer, like NSString's cString method does, but I can't quite see how from the docs
Manual Data Alignment in Cocoa - geezusfreeek - Jun 13, 2002 06:04 PM
Oh. Haha, I only read about that stuff about a hundred times by now. My mind is stuck on Carbon. I can't get it through my head that the standard C routines are preferable in Cocoa. Thanks.