use managed memory allocation

Use managed memory allocation where possible to simplify release
and error paths. Also use devm_kzalloc where kmalloc is used,
since devm_kmalloc came as late as 3.13.