int main(int argc, char** argv) { // Provide argc and argv to interested modules sys_argc = argc; sys_argv = argv; // Initialize memory system mem_init(); // Initialize stack and exception system stack_init(); // Initialize static symbol table sym_init(); // Initialize value klass system klass_init(); // Phase 1 stack_annot_push("init1_Function"); init1_Function(); stack_annot_pop(); stack_annot_push("init1_Object"); init1_Object(); stack_annot_pop(); ripe_module1a(); ripe_module1b(); // Phase 1.5 stack_annot_push("phase 1.5"); common_init_phase15(); klass_init_phase15(); stack_annot_pop(); // Phase 2 stack_annot_push("init2_Function"); init2_Function(); stack_annot_pop(); stack_annot_push("init2_Object"); init2_Object(); stack_annot_pop(); ripe_module2(); ripe_module3(); // Call main. Value rv = ripe_main(); if (is_int64(rv)){ return unpack_int64(rv); } mem_deinit(); return 0; }
int main(void) { int num = 0; char *_ptr; _ptr = malloc(4096); mgr = mem_init(_ptr, 1024); mem_print(mgr); /* test */ { printf("test malloc:\n"); num = test_malloc(50); printf("memory out at %d times. first size=%d end size=%d\n", num, 50, 50+num*10); mem_print(mgr); strcpy(ptr[1], "*****@*****.**"); strcpy(ptr[3], "tain@linuxmint: /opt/broadcom/7851/a0"); mem_print(mgr); // mem_dump(mgr); } FREE(mgr, ptr[1]); mem_print(mgr); getchar(); FREE(mgr, ptr[3]); mem_print(mgr); getchar(); { printf("test free: num=%d\n", num); test_free(num + 2); mem_print(mgr); // mem_dump(mgr); } mem_deinit(mgr); free(_ptr); return 0; }
/************** * Main routine **************/ int main(int argc, char **argv) { int i; char c; char **tracefiles = NULL; /* null-terminated array of trace file names */ int num_tracefiles = 0; /* the number of traces in that array */ trace_t *trace = NULL; /* stores a single trace file in memory */ stats_t *libc_stats = NULL;/* libc stats for each trace */ stats_t *bad_stats = NULL; /* bad malloc stats for each trace */ stats_t *mm_stats = NULL; /* mm (i.e. student) stats for each trace */ int run_libc = 0; /* If set, run libc malloc (set by -l) */ int run_bad = 0; /* If set, run bad malloc (set by -b) */ int check_heap = 0; /* If set, run the student heap checker (set by -c) */ int autograder = 0; /* If set, emit summary info for autograder (-g) */ /* temporaries used to compute the performance index */ double secs, ops, util, avg_mm_util, avg_mm_throughput, p1, p2, perfindex; int numcorrect; /* * Read and interpret the command line arguments */ while ((c = getopt(argc, argv, "f:t:hvVgalbc")) != EOF) { switch (c) { case 'g': /* Generate summary info for the autograder */ autograder = 1; break; case 'f': /* Use one specific trace file only (relative to curr dir) */ num_tracefiles = 1; if ((tracefiles = (char **) realloc(tracefiles, 2*sizeof(char *))) == NULL) unix_error("ERROR: realloc failed in main"); strcpy(tracedir, "./"); tracefiles[0] = strdup(optarg); tracefiles[1] = NULL; break; case 't': /* Directory where the traces are located */ if (num_tracefiles == 1) /* ignore if -f already encountered */ break; strcpy(tracedir, optarg); if (tracedir[strlen(tracedir)-1] != '/') strcat(tracedir, "/"); /* path always ends with "/" */ break; case 'l': /* Run libc malloc */ run_libc = 1; break; case 'b': /* Run bad malloc to check the verifier. */ run_bad = 1; break; case 'c': check_heap = 1; break; case 'v': /* Print per-trace performance breakdown */ verbose = 1; break; case 'V': /* Be more verbose than -v */ verbose = 2; break; case 'h': /* Print this message */ usage(); exit(0); default: usage(); exit(1); } } /* * If no -f command line arg, then use the entire set of tracefiles * defined in default_traces[] */ if (tracefiles == NULL) { tracefiles = default_tracefiles; num_tracefiles = sizeof(default_tracefiles) / sizeof(char *) - 1; printf("Using default tracefiles in %s\n", tracedir); } /* Initialize the timing package */ init_fsecs(); /* * Optionally run and evaluate the libc malloc package */ if (run_libc) { if (verbose > 1) { printf("\nTesting libc malloc\n"); } /* Allocate libc stats array, with one stats_t struct per tracefile */ libc_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t)); if (libc_stats == NULL) { unix_error("libc_stats calloc in main failed"); } /* Evaluate the libc malloc package using the K-best scheme */ for (i = 0; i < num_tracefiles; i++) { trace = read_trace(tracedir, tracefiles[i]); libc_stats[i].ops = trace->num_ops; if (verbose > 1) printf("Checking libc malloc for correctness, "); libc_stats[i].valid = eval_mm_valid(&libc_impl, trace, i); if (check_heap) { libc_stats[i].checked = eval_mm_check(&libc_impl, trace, i); } if (libc_stats[i].valid) { if (verbose > 1) printf("and performance.\n"); libc_stats[i].secs = fsecs((void (*)(void *))eval_libc_speed, trace); } free_trace(trace); } /* Display the libc results in a compact table */ if (verbose) { printf("\nResults for libc malloc:\n"); printresults(num_tracefiles, tracefiles, libc_stats); } } /* Initialize the simulated memory system in memlib.c */ mem_init(); /* * Optionally run and evaluate the libc malloc package */ if (run_bad) { if (verbose > 1) { printf("\nTesting bad malloc\n"); } /* Allocate bad stats array, with one stats_t struct per tracefile */ bad_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t)); if (bad_stats == NULL) { unix_error("bad_stats calloc in main failed"); } /* Evaluate the bad malloc package using the K-best scheme */ for (i = 0; i < num_tracefiles; i++) { trace = read_trace(tracedir, tracefiles[i]); bad_stats[i].ops = trace->num_ops; printf("Checking bad malloc for correctness.\n"); bad_stats[i].valid = eval_mm_valid(&bad_impl, trace, i); if (check_heap) { bad_stats[i].checked = eval_mm_check(&bad_impl, trace, i); } free_trace(trace); } /* Display the bad results in a compact table */ if (verbose) { printf("\nResults for bad malloc:\n"); printresults(num_tracefiles, tracefiles, bad_stats); } } /* * Always run and evaluate the student's mm package */ if (verbose > 1) { printf("\nTesting mm malloc\n"); } /* Allocate the mm stats array, with one stats_t struct per tracefile */ mm_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t)); if (mm_stats == NULL) { unix_error("mm_stats calloc in main failed"); } /* Evaluate student's mm malloc package using the K-best scheme */ for (i = 0; i < num_tracefiles; i++) { trace = read_trace(tracedir, tracefiles[i]); mm_stats[i].ops = trace->num_ops; if (verbose > 1) { printf("Checking mm_malloc for correctness, "); } mm_stats[i].valid = eval_mm_valid(&my_impl, trace, i); if (check_heap) { mm_stats[i].checked = eval_mm_check(&my_impl, trace, i); } if (mm_stats[i].valid) { if (verbose > 1) { printf("efficiency, "); } mm_stats[i].util = eval_mm_util(trace, i); if (verbose > 1) { printf("and performance.\n"); } mm_stats[i].secs = fsecs((void (*)(void *))eval_mm_speed, trace); } free_trace(trace); } /* Free the simulated heap block. */ mem_deinit(); /* Display the mm results in a compact table */ if (verbose) { printf("\nResults for mm malloc:\n"); printresults(num_tracefiles, tracefiles, mm_stats); printf("\n"); } /* * Accumulate the aggregate statistics for the student's mm package */ secs = 0; ops = 0; util = 0; numcorrect = 0; for (i = 0; i < num_tracefiles; i++) { secs += mm_stats[i].secs; ops += mm_stats[i].ops; util += mm_stats[i].util; if (mm_stats[i].valid) { numcorrect++; } } avg_mm_util = util/num_tracefiles; /* * Compute and print the performance index */ if (errors == 0) { avg_mm_throughput = ops/secs; p1 = UTIL_WEIGHT * avg_mm_util; if (avg_mm_throughput > AVG_LIBC_THRUPUT) { p2 = (double)(1.0 - UTIL_WEIGHT); } else { p2 = ((double) (1.0 - UTIL_WEIGHT)) * (avg_mm_throughput/AVG_LIBC_THRUPUT); } perfindex = (p1 + p2)*100.0; printf("Perf index = %.0f (util) + %.0f (thru) = %.0f/100\n", p1 * 100, p2 * 100, perfindex); } else { /* There were errors */ perfindex = 0.0; printf("Terminated with %d errors\n", errors); } if (autograder) { printf("correct:%d\n", numcorrect); printf("perfidx:%.0f\n", perfindex); } /* Keep valgrind happy, free the statistics arrays. */ free(libc_stats); free(bad_stats); free(mm_stats); exit(0); }