Add custom memory allocator for documents, some fixes for GCC.