Extract malloc() and free() from semaphore spinlocked code