refactor to support new ffi API, also the ref interface about how to write pointer's memory