Fix soundness and correctness issues in the jemalloc interface, improving performance. Contributed by @Jade