refactor malloc/free stuff into macros