Implement allocators

Mostly adapted code from https://github.com/thestinger/rust-core/commit/b9889c20f805c1aacadd92f99da1b26c5c4d3449.

Two small problems remain.