-static mspace mySpace = NULL;
-void *malloc( size_t size ){
- if( NULL == mySpace ){
- //void * mem = MYMALLOC( MSPACE_SIZE );
- mySpace = create_mspace( MSPACE_SIZE, 1 );
- AddUserHeapToSnapshot();
+
+void *system_malloc( size_t size ){
+ static void *(*mallocp)(size_t size);
+ char *error;
+ void *ptr;
+
+ /* get address of libc malloc */
+ if (!mallocp) {
+ mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc");
+ if ((error = dlerror()) != NULL) {
+ fputs(error, stderr);
+ exit(EXIT_FAILURE);
+ }
+ }
+ ptr = mallocp(size);
+ return ptr;
+}
+
+void system_free( void * ptr ){
+ static void (*freep)(void *);
+ char *error;
+
+ /* get address of libc free */
+ if (!freep) {
+ freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
+ if ((error = dlerror()) != NULL) {
+ fputs(error, stderr);
+ exit(EXIT_FAILURE);
+ }
+ }
+ freep(ptr);
+}
+
+/** Non-snapshotting free for our use. */
+void MYFREE(void *ptr) {
+#if USE_MPROTECT_SNAPSHOT
+ static void (*freep)(void *);
+ char *error;
+
+ /* get address of libc free */
+ if (!freep) {
+ freep = ( void ( * )( void * ) )dlsym(RTLD_NEXT, "free");
+ if ((error = dlerror()) != NULL) {
+ fputs(error, stderr);
+ exit(EXIT_FAILURE);
+ }
+ }
+ freep(ptr);
+#else
+ mspace_free( sStaticSpace, ptr );
+#endif
+}
+
+
+/** This global references the mspace for the snapshotting heap */
+mspace mySpace = NULL;
+
+/** This global references the unaligned memory address that was malloced for the snapshotting heap */
+void * basemySpace = NULL;
+
+/** Bootstrap allocation. Problem is that the dynamic linker calls
+ * require calloc to work and calloc requires the dynamic linker to
+ * work. */
+
+#define BOOTSTRAPBYTES 4096
+char bootstrapmemory[BOOTSTRAPBYTES];
+size_t offset=0;
+
+void * HandleEarlyAllocationRequest( size_t sz ){
+ /*Align to 8 byte boundary*/
+ sz=(sz+7)&~7;
+
+ if (sz > (BOOTSTRAPBYTES-offset)) {
+ printf("OUT OF BOOTSTRAP MEMORY\n");
+ exit(EXIT_FAILURE);