Add custom memory functions

Forked from txdv@memfuncs4, see #73 for discussion.