This adds checks to make sure munmap and bfree are only called when there was actually memory mapped and allocated.
This moves some functions that are generic to a separate source file. While doing so the api to those functions was improved to be more generic and not depend on knowledge about the internal structure of the plugin.