Allow custom memory allocation callbacks on buffer

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