[AD] Changing malloc/memset to calloc

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


I've run into an issue on iOS where the operating system is trying to free memory up for my program while I'm allocating memory (a common case on iOS and probably other low resource environments.) malloc is failing. I've been talking to a developer tech support person at Apple and here's what she said:

"Also, I notice you use the memset function to initialize the memory (that was allocated by malloc). A better choice is to use the calloc routine to allocate the memory in the first place. The calloc function reserves the required virtual address space for the memory but waits until the memory is actually used before initializing it. memset, on the other hand, forces the virtual memory system to map the corresponding pages into physical memory in order to zero-initialize them."

So it's possible that always doing malloc->memset is causing memory to be mapped more quickly than it would be with calloc. In any case, as I said, malloc is failing in my app and I'm not using all that much memory. Background: on iOS, when your program starts, the OS will begin sending low memory warnings to background apps, which should then free memory if possible. It can also flat out kill background apps. But it takes time between the OS sending low memory warnings, and the apps responding, and it's during this time that my program is allocating lots of memory. This time happens to be program startup, which is very commonly where you allocate a lot of memory.

I heard it through the grapevine that tjaden didn't want to include al_calloc, and so I don't want to convert all malloc/memset to calloc if someone is going to change it all back. What I can do is do the conversion and then if it fixes the problem, commit it, or, just commit it anyway (which I think is sensible given the information from the DTS person.)

Trent



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/