Implement shrink_to_fit() and alloc-aware copy.

This also fixes a bug when copying from heap to internal.