Reallocarray and other memory allocation cleanups
I started converting Xlib to use reallocarray a few years back and made good progress, along with cleaning up some other issues I found along the way, before putting it aside to work on other things. Instead of letting this rot in my local repo, might as well merge it upstream, as it is an improvement over what we have now.