Add malloc attribute to memory allocation functions
The malloc attribute allows compiler to do some optmizations on functions that behave like malloc/calloc, like assuming that the returned pointer do not alias other pointers.
Closes #2529 (closed)