Move memory functions from misc2.c