Check malloc results

For now this will assert in most out-of-memory cases, so better error handling should be added in future commits.