OS X compatibility fixes

Code fixes for compatibility with OS X, see https://github.com/minetest/minetest/issues/1242. I have tested this on OS X and Linux.
this counter is quite dangerous you'll either have to put a mutex around it or ensure it's protected by some atomic mechanism, e.g. c++11 atomic