Allow custom memory allocation callbacks on buffer

Forked from txdv@memfuncs4, see #73 for discussion.
Merge pull request #76 from jmendeth/memory-allocation

Add custom memory functions