I've been learning how to allocate chunks of memory. I've learned the windows way:
unsigned char* memory = (unsigned char*)VirtualAlloc(0, memorySize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
But for Linux I had no idea how to do it. A little bit of searching led me to mmap. I'm trying to do the same thing I did with VirtualAlloc, but with mmap. I'm still confused and not sure if I'm doing it right:
unsigned char* memory = (unsigned char*) mmap(0, memory->memorySize, PROT_READ | PROT_WRITE, MAP_PRIVATE, 0, 0);
Question about how to handle game objects and allocated memory
Let's say I allocate 1 gig of memory using VirtualAlloc (or mmap). Now I want to grab a small chunk of that memory using this:
uint8* AllocateMemory(Memory_Allocator* allocator, uint64 size) {
assert(allocator->bytes_remaining >= size);
uint8* mem = allocator->next;
allocator->next += size;
return mem;
}
I use the function above to allocate N amount of bytes by doing the following:
Player* players = (Player*)AllocateMemory(sizeof(Player) * numPlayers);
From there I can for loop players and do everything I need to do.
Is this a good way of approaching managing memory in game development? At least to get started, is there anything I should I look out for?
Hope this all makes sense, and thanks!