int main() { struct match *info1, *info2, *info3; init_heap(); info1 = get_match(20151018, "Baseball", "Cubs", "Mets", 1, 4); print_info(info1); dump_heap(); del_match(info1); info1 = get_match(20151018, "Football", "Ravens", "49ers", 20, 25); info2 = get_match(20151019, "Baseball", "Royals", "Blue Jays", 8, 1); info3 = get_match(20151019, "Football", "Giants", "Eagles", 7, 27); dump_heap(); del_match(info2); dump_heap(); return 0; }
void handle_request(void) { void *allocs[100] = {0}; uint32_t alloc_idx; sendstr(" **** WELCOME TO THE HEAP SIMULATOR 2001! ****\n\n"); while (1) { sendstr("What would you like to do?\n"); sendstr(" 1) Allocate memory\n"); sendstr(" 2) Free memory\n"); sendstr(" 3) Read some data\n"); sendstr(" 4) Exit\n\n"); int cmd = readint(); uint16_t id; switch (cmd) { case 1: for (alloc_idx = 0; alloc_idx < sizeof(allocs) && allocs[alloc_idx] != NULL; ++alloc_idx) ; if (alloc_idx >= sizeof(allocs)) { sendstr("Run out of space, free something first!\n"); } else { sendstr("Ok, how much memory do you want (in bytes)?\n"); uint16_t bytes = readint(); allocs[alloc_idx] = (void *)alloc(bytes); sendstr(tprintf("Allocated with ID = %d\n", alloc_idx)); } dump_heap(); break; case 2: sendstr("Please enter the ID of the alloc to free\n"); id = readint(); if (id < sizeof(allocs) && allocs[id] != NULL) { dealloc(allocs[id]); allocs[id] = NULL; } else { sendstr("Alloc not found\n"); } dump_heap(); break; case 3: sendstr("Please enter the ID of the alloc where to read data\n"); id = readint(); if (id < sizeof(allocs) && allocs[id] != NULL) { sendstr("Ready! Send the data\n"); recvline(client_fd, allocs[id], 0x100); } else { sendstr("Alloc not found\n"); } dump_heap(); break; case 4: say_bye(); exit(0); break; default: sendstr("What!?\n"); }; } }
int main(void) { void *b1, *b2, *b3; dump_heap(); b1 = kmalloc(42); dump_heap(); b2 = kmalloc(23); dump_heap(); b3 = kmalloc(7); dump_heap(); b2 = krealloc(b2, 24); dump_heap(); kfree(b1); dump_heap(); b1 = kmalloc(5); dump_heap(); kfree(b2); dump_heap(); kfree(b3); dump_heap(); kfree(b1); dump_heap(); return 0; }
int main() { int data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int size = 10; //int *data = malloc(sizeof(int)*size); struct heap *h; int new_data = 30; h = build_max_heap(data, size); dump_heap(h); free(h); h = build_min_heap(data, size/2, size); dump_heap(h); printf("extract min=%d\n", min_heap_extract(h)); dump_heap(h); printf("insert %d\n", new_data); min_heap_insert(h, new_data); dump_heap(h); free(h); //free(data); return 0; }
int main(void) { unsigned lifetime[SLOTS]; void *blk[SLOTS]; int i, j, k; dump_heap(); memset(lifetime, 0, sizeof(lifetime)); memset(blk, 0, sizeof(blk)); for(i = 0; i < 1000; i++) { printf("Pass %6u\n", i); for(j = 0; j < SLOTS; j++) { /* age the block */ if(lifetime[j] != 0) { (lifetime[j])--; continue; } /* too old; free it */ if(blk[j] != NULL) { kfree(blk[j]); blk[j] = NULL; } /* alloc new block of random size Note that size_t==unsigned, but kmalloc() uses integer math, so block size must be positive integer */ #if defined(_32BIT) k = rand() % 40960 + 1; #else k = rand() % 4096 + 1; #endif blk[j] = kmalloc(k); if(blk[j] == NULL) printf("failed to alloc %u bytes\n", k); else /* give it a random lifetime 0-20 */ lifetime[j] = rand() % 21; } } /* let's see what we've wrought */ printf("\n\n"); dump_heap(); /* free everything */ for(j = 0; j < SLOTS; j++) { if(blk[j] != NULL) { kfree(blk[j]); blk[j] = NULL; } (lifetime[j]) = 0; } /* after all that, we should have a single, unused block */ dump_heap(); return 0; }
int main(int argc, char *argv[]) { struct heap **heap_dump = NULL; int i,proc_pid = 0; printf("memory dumper version 1.0.1 2013-08-14\n"); printf("--------------------------------------------------------------\n"); if(argc != 2) { printf("usage %s PID\n",argv[0]); exit(1); } else { proc_pid = atoi(argv[1]); } heap_dump = find_heap_values(proc_pid); if(heap_dump != NULL) { dump_heap(heap_dump,proc_pid); } else { printf("* Unknown error occured.\n"); } for(i=0; i<heap_structure_size; i++) { free(heap_dump[i]); } free(heap_dump); return 0; }
/** * Dumps the specified heap of memory to the program's STDOUT. * * @see protocol * @param heap Location of the heap to dump. * @param heap_size Number of nodes in the heap. * @param node Heap at which to begin dumping. * @param indent How many tabs to indent the start of each line. * */ void dump_heap(int *heap, int heap_size, int node, int indent) { int i; if (node <= heap_size) { for (i = 0; i < indent * 3; i++) putc((i % 3) == 0 ? '|' : ' ', protocol); fprintf(protocol, "Node %d (p: %d, f: %d)\n", node, heap[node-1], heap[heap[node-1]]); dump_heap(heap, heap_size, 2 * node, indent + 1); dump_heap(heap, heap_size, 2 * node + 1, indent + 1); } }
void *test_allocate(size_t bytes) { void *ptr = alloc(bytes); if (ptr == NULL) { printf("Got back NULL pointer for %d bytes\n", (int)bytes); } else { printf("alloc() gave us block at %p for %d bytes\n", ptr, (int)bytes); } dump_heap(heap_start); return ptr; }
static void tst2() { int_heap2 h(N); for (int i = 0; i < N * 10; i++) { if (i % 1000 == 0) std::cout << "i: " << i << std::endl; int cmd = rand() % 10; if (cmd <= 3) { // insert int val = rand() % N; if (!h.contains(val)) { TRACE("heap", tout << "inserting: " << val << "\n";); h.insert(val); TRACE("heap", dump_heap(h, tout););
int main() { put_structure("h", 2, 3); /* ?- X3 = h */ set_variable(2); /* (Z, */ set_variable(5); /* W), */ put_structure("f", 1, 4); /* X4 = f */ set_value(5); /* (W), */ put_structure("p", 3, 1); /* X1 = p */ set_value(2); /* (Z, */ set_value(3); /* X3, */ set_value(4); /* X4). */ print_register(1); /* drukowanie X1 */ dump_heap(); fail = false; get_structure("p", 3, 1); /* X1 = p */ unify_variable(2); /* (X2, */ unify_variable(3); /* X3, */ unify_variable(4); /* Y), */ get_structure("f", 1, 2); /* X2 = f */ unify_variable(5); /* (X), */ get_structure("h", 2, 3); /* X3 = h */ unify_value(4); /* (Y, */ unify_variable(6); /* X6), */ get_structure("f", 1, 6); /* X6 = f */ unify_variable(7); /* (X7), */ get_structure("a", 0, 7); /* X7 = a */ printf("fail = %d\n", (int)fail); print_register(1); /* drukowanie X1 */ dump_heap(); return 0; }
/** * Prints a description of the specified heap of memory to the program's STDOUT. * * @see protocol * @param heap Location of the heap to print. * @param heap_size Number of nodes in the heap. * @param title Title of the heap to print. */ void print_heap(int *heap, int heap_size, char *title) { int node, depth; node = 1; depth = 0; fprintf(protocol, "\nDump of %s (size %d)\n\n", title, heap_size); dump_heap(heap, heap_size, 1, 0); fprintf(protocol, "\x0c"); }
void shell::meminfo() { extern unsigned int memend; multiboot *mbt=multiboot::Instance(); cout.flags(hex|showbase); cout<<"Kernel starts at "<<(unsigned int)mbt->get_k_start()<<"\n"; cout<<"Kernel Ends at "<<(unsigned int)mbt->get_k_end()<<"\n"; cout.SetColour(RED,GREEN,0); cout<<"Memory Used = "<<(unsigned int)mbt->get_mem_used()<<"\n"; cout<<"Memory Avail = "<<(unsigned int)mbt->get_mem_avail()<<"\n"; cout.SetColour(WHITE,BLACK,0); cout.flags(dec); dump_heap(); cout<<"Total Memory "<<(memend/0x100000)+1<<" MB\n"; }
/** * Handle the occurence of a hit on the exit breakpoint. This includes gathering all information * neccessary to create the exit event. * * @param context the context for the breakpoint * @param errmsg the error message populated on error * * @return the information extracted about the exit event */ static event_result handle_exit_breakpoint(const ucontext_t *context, udi_errmsg *errmsg) { event_result result; result.failure = 0; result.wait_for_request = 1; exit_result exit_result = get_exit_argument(context, errmsg); if ( exit_result.failure ) { result.failure = exit_result.failure; return result; } udi_printf("exit entered with status %d\n", exit_result.status); exiting = 1; // create the event udi_event_internal exit_event = create_event_exit(get_user_thread_id(), exit_result.status); do { if ( exit_event.packed_data == NULL ) { result.failure = 1; break; } result.failure = write_event(&exit_event); udi_free(exit_event.packed_data); } while(0); if ( result.failure ) { udi_printf("failed to report exit status of %d\n", exit_result.status); } else { if ( udi_debug_on ) { dump_heap(); } } return result; }
void shell_cmd(const char* cmd) { if (!strcmp(cmd, "help")) { printf("check the source ;)\n"); } else if (!strcmp(cmd, "reboot")) { printf("rebooting...\n"); _triple_fault(); } else if (!strcmp(cmd, "heap")) { dump_heap(); } else if (!strcmp(cmd, "rand")) { uint8_t buf[16]; if (!rand_data(buf, 16)) { putbytes(buf, 16); putc('\n'); } else { printf("not enough entropy\n"); } } else if (!strcmp(cmd, "alloc")) { malloc(0x10000); } else if (!strcmp(cmd, "page")) { printf("trying to map single 4 MiB page...\n"); test_enable_paging(); printf("done\n"); } else if (!strcmp(cmd, "info")) { printf("pit ticks: %u\n", pit_ticks); printf("rtc ticks: %lu\n", rtc_ticks); printf("spurious irq count: %lu\n", spurious_irq_count); printf("rdtsc: 0x%lx\n", __builtin_ia32_rdtsc()); } else if (!strcmp(cmd, "test")) { thread_create(test, (void*) 42); } else if (!strcmp(cmd, "yield")) { thread_yield(); } else if (strcmp(cmd, "")) { // not empty printf("unknown cmd \"%s\"\n", cmd); } }
int main() { int i ; int j = 0 ; int blks[100] ; init_heap() ; for( i=0; i<20; i++ ) blks[j++] = lalloc( rand()%500 ) ; dump_heap( "after alloc" ) ; lfree( &blks[10] ) ; lfree( &blks[11] ) ; dump_heap( "coalesce with upper" ) ; lfree( &blks[14] ) ; lfree( &blks[13] ) ; dump_heap( "coalesce with lower" ) ; lfree( &blks[5] ) ; lfree( &blks[7] ) ; lfree( &blks[6] ) ; dump_heap( "coalesce with both" ) ; for( i=0; i<20; i++ ) if (blks[i] != 0 ) { lfree( &blks[i] ) ; } dump_heap( "free everything " ) ; blks[0] = lalloc( 40000 ) ; dump_heap( "blew the top off" ) ; return 0 ; }
void public_mSTATs() { int i; mstate ar_ptr; struct malloc_global_info mgi; struct malloc_arena_info mai; unsigned long in_use_b, system_b, avail_b; #if defined(THREAD_STATS) && THREAD_STATS long stat_lock_direct = 0, stat_lock_loop = 0, stat_lock_wait = 0; #endif #if 0 if(__malloc_initialized < 0) ptmalloc_init (); #endif _int_get_global_info(&mgi); system_b = in_use_b = mgi.mmapped_mem; #ifdef _LIBC _IO_flockfile (stderr); int old_flags2 = ((_IO_FILE *) stderr)->_flags2; ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; #endif for (i=0; (ar_ptr = _int_get_arena(i)); i++) { _int_get_arena_info(ar_ptr, &mai); avail_b = mai.fastavail + mai.binavail + mai.top_size; fprintf(stderr, "Arena %d:\n", i); fprintf(stderr, "system bytes = %10lu\n", (unsigned long)mai.system_mem); fprintf(stderr, "in use bytes = %10lu\n", (unsigned long)(mai.system_mem - avail_b)); #if MALLOC_DEBUG > 1 if (i > 0) dump_heap(heap_for_ptr(top(ar_ptr))); #endif system_b += mai.system_mem; in_use_b += mai.system_mem - avail_b; #if defined(THREAD_STATS) && THREAD_STATS stat_lock_direct += mai.stat_lock_direct; stat_lock_loop += mai.stat_lock_loop; stat_lock_wait += mai.stat_lock_wait; #endif } #if HAVE_MMAP fprintf(stderr, "Total (incl. mmap):\n"); #else fprintf(stderr, "Total:\n"); #endif fprintf(stderr, "system bytes = %10lu\n", system_b); fprintf(stderr, "in use bytes = %10lu\n", in_use_b); #ifdef NO_THREADS fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)mgi.max_total_mem); #endif #if HAVE_MMAP fprintf(stderr, "max mmap regions = %10u\n", (unsigned int)mgi.max_n_mmaps); fprintf(stderr, "max mmap bytes = %10lu\n", (unsigned long)mgi.max_mmapped_mem); #endif #if defined(THREAD_STATS) && THREAD_STATS fprintf(stderr, "heaps created = %10d\n", mgi.stat_n_heaps); fprintf(stderr, "locked directly = %10ld\n", stat_lock_direct); fprintf(stderr, "locked in loop = %10ld\n", stat_lock_loop); fprintf(stderr, "locked waiting = %10ld\n", stat_lock_wait); fprintf(stderr, "locked total = %10ld\n", stat_lock_direct + stat_lock_loop + stat_lock_wait); #endif #ifdef _LIBC ((_IO_FILE *) stderr)->_flags2 |= old_flags2; _IO_funlockfile (stderr); #endif }
int main() { book_info *info[20] = { NULL }, *info1; init_heap(); printf("The following should show 20 available entries (all entries have year_published != 0) - deduct 3 points if it does not\n"); dump_heap(); info[0] = get_new_book_info("Rant: The Oral Biography of Buster Casey", "Chuck Palahniuk", 2008); del_book_info(info[0]); info[0] = get_new_book_info("Less Than Zero", "Bret Easton Ellis", 1985); info[1] = get_new_book_info("What If: Serious Scientific Answers to Absurd Hypothetical Questions", "Randall Monroe", 2014); info[2] = get_new_book_info("Ender\'s Game", "Orson Scott Card", 1985); del_book_info(info[1]); info[1] = new_book_info(); if (info[1] == NULL) { printf("Got new_book_info() == NULL when should have been correct, -5\n"); score -= 5; } else { memset(info[1]->author, 0, sizeof(info[1]->author)); memset(info[1]->title, 0, sizeof(info[1]->title)); info[1]->year_published = 0; } int i; if (sizeof(book_info) != sizeof(my_book_info)) { printf("sizeof(book_info) == %d, not %d as it should - -5\n", sizeof(book_info), sizeof(my_book_info)); score -= 5; } int j, set_error = 0; for (i = 3; i < 20; i++) { info[i] = new_book_info(); if (info[i] == NULL) { printf("Got new_book_info() == NULL when should have been correct, -5\n"); score -= 5; break; } memset(info[i]->author, 0, sizeof(info[i]->author)); memset(info[i]->title, 0, sizeof(info[i]->title)); info[i]->year_published = 0; for (j = 0; j < i; j++) { if (info[i] == info[j]) { printf("%dth book_info is the same as %dth book_info - %p\n", i, j, info[i]); set_error = 1; } } } if (set_error) { printf("Allocator returns same pointer for multiple allocations - -5\n"); score -= 5; } set_error = 0; printf("The following should show 0 free entries (all entries have year_published == 0, except indexes 0 & 2, which should both contain 1985) - deduct 3 points if it does not\n"); dump_heap(); book_info *last_info = info[19]; book_info *next_info = new_book_info(); if (next_info != NULL) { printf("Could allocate at least 21 entries - -5\n"); score -= 5; del_book_info(next_info); next_info = NULL; } /* Free all entries */ for (i = 19; i >= 0; i--) { if (info[i]) { del_book_info(info[i]); info[i] = NULL; } } printf("The following should show 20 free entries (all entries have year_published != 0) - deduct 3 points if it does not\n"); dump_heap(); /* Allocate all entries */ for (i = 0; i < 20; i++) { info[i] = new_book_info(); if (info[i] == NULL) { printf("Got new_book_info() == NULL when should have been correct, -5\n"); score -= 5; break; } } next_info = new_book_info(); if (next_info != NULL) { printf("Could allocate at least 21 entries - -5\n"); score -= 5; del_book_info(next_info); next_info = NULL; } /* Delete every other entry */ for (i = 0; i < 20; i+= 2) { if (info[i]) { del_book_info(info[i]); info[i] = NULL; } } /* Now, reallocate them */ for (i = 0; i < 20; i+= 2) { info[i] = new_book_info(); if (info[i] == NULL) { printf("Got new_book_info() == NULL when should have been correct, -5\n"); score -= 5; break; } } next_info = new_book_info(); if (next_info != NULL) { printf("Could allocate at least 21 entries - -5\n"); score -= 5; del_book_info(next_info); next_info = NULL; } printf("If you don't see another line with \"Final Score\" at the beginning, the Final Score is: %d\n", score); del_book_info(NULL); score -= 5; printf("Failed to exit the program with a bad pointer in del_book_info() - -5\n"); printf("Final Score: %d\n", score); return 0; }