Manual Data Alignment in Cocoa

Member
Posts: 201
Joined: 2002.06
Post: #1
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.
Quote this message in a reply
Member
Posts: 201
Joined: 2002.06
Post: #2
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
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 Smile
Quote this message in a reply
Member
Posts: 201
Joined: 2002.06
Post: #4
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.
Quote this message in a reply
Post Reply