Applied another patch by ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp>
Michael Meskes <meskes@postgresql.org>
Applied another patch by ITAGAKI Takahiro <itagaki.takahiro@oss.ntt.co.jp> to get memory allocation thread-safe. He also did some cleaning up.