/* * Wrap the log_set_file() function in testsc.c, and respond to it by * opening or closing log files. */ static void wrap_logsetfile(void *wrapctx, void **user_data) { if (outfile) { dr_close_file(outfile); outfile = INVALID_FILE; } const char *outfilename = drwrap_get_arg(wrapctx, 0); if (outfilename) { outfile = dr_open_file(outfilename, DR_FILE_WRITE_OVERWRITE); DR_ASSERT(outfile != INVALID_FILE); } /* * Reset the allocation list to empty, whenever we open or close a * log file. */ while (alloc_ends->next != alloc_ends) free_allocation(alloc_ends->next); next_alloc_index = 0; }
void *memalloc(int handle, long n_bytes){ struct info * h = &MemAllocs[handle]; long alloc_bytes = n_bytes; if(alloc_bytes == 0){ printf("memalloc error: invalid alloc size\n"); return NULL; } printf("Attempting to allocate: %lu bytes\n", alloc_bytes); void *returnptr = h->memptr; unsigned int f_marker = h->flags; switch(f_marker){ case (BUDDY_ALLOC): //Buddy printf("Buddy Alloc:\n"); returnptr = buddy_allocation(h, n_bytes); break; case (SLAB_ALLOC): //Slab printf("Slab\n"); break; case (FREE1_ALLOC): //Free Alloc 1 printf("Free Alloc: First Fit\n"); returnptr = free_allocation(h, n_bytes); if(returnptr!=NULL) h->last_alloc = returnptr; break; case (FREE2_ALLOC): //Free Alloc 2 printf("Free Alloc: Next Fit\n"); returnptr = free_allocation(h, n_bytes); if(returnptr!=NULL) h->last_alloc = returnptr; break; case (FREE3_ALLOC ): //Free Alloc 3 printf("Free Alloc: Best Fit\n"); returnptr = free_allocation(h, n_bytes); if(returnptr!=NULL) h->last_alloc = returnptr; break; case (FREE4_ALLOC): //Free Alloc 4 printf("Free Alloc: Worst Fit\n"); returnptr = free_allocation(h, n_bytes); if(returnptr!=NULL) h->last_alloc = returnptr; break; default: //Undefined returnptr = NULL; break; } if(returnptr == NULL) printf("Could not find enough space\n"); return returnptr; }
/* * Record that memory has been freed. Note that we may free something * that was allocated when we weren't logging, so we must cope with * find_allocation returning NULL. */ static void freed(void *ptr) { struct allocation *alloc = find_allocation(ptr); if (alloc) free_allocation(alloc); }