memalloc refactoring