[mem] move memory handling code to mem/ folder