Big refactor of spatial.distance

Two patches for `scipy.spatial.distance`. The first refactors the code by generating a lot of boilerplate using macros, reducing the code by about a thousand lines, and leaving only one module to be compiled. The second fixes a few unchecked `malloc` calls.