Fixes for porting to FreeBSD

A few commits to allow compiling on FreeBSD, verified on FreeBSD 9 with clang.