unify memory guard code