tvm_t* tvm_create(char* filename) { tvm_t* vm = (tvm_t*)malloc(sizeof(tvm_t)); if(!vm) return NULL; vm->pMemory = create_memory(MIN_MEMORY_SIZE); if(!vm->pMemory) { free(vm); return NULL; } vm->pProgram = create_program(); if(!vm->pProgram) { destroy_memory(vm->pMemory); free(vm); return NULL; } create_stack(vm->pMemory, MIN_STACK_SIZE); if(interpret_program(vm->pProgram, filename, vm->pMemory) != 0) { // avoid memory leak when the interpret_program failed! tvm_destroy(vm); return NULL; } //if(!vm || !vm->pMemory || !vm->pProgram) return NULL; return vm; }
// Returns the strategy object to tournament program strategy A3_elmassari_lequerec_negueloua_efficient() { srand(time(NULL)); strategy test = malloc(sizeof(struct strategy_s)); test->mem = create_memory(); test->name = "strategy1"; test->play_move = &(play_move); test->free_strategy = &(free_strategy); return test; }
tvm_t* tvm_create(char* filename) { tvm_t* vm = (tvm_t*)malloc(sizeof(tvm_t)); vm->pMemory = create_memory(MIN_MEMORY_SIZE); vm->pProgram = create_program(); create_stack(vm->pMemory, MIN_STACK_SIZE); if(!vm || !vm->pMemory || !vm->pProgram) return NULL; return vm; }
//Before running run: "ulimit -s unlimited" int main(int argc, char* argv[]) { parse_opts(argc, argv); srand(time(NULL)); int tick = 0; struct list **chunks_lifetime = (struct list **)malloc(sizeof(struct list*) * max_ticks);// The stack memory has somee restrictions for (int i = 0; i < max_ticks; i++) { chunks_lifetime[i] = create_list(); } struct memory *mem = create_memory(memory_size); printf("Starting\n"); while (1) { if (need_trace) { printf("Tick %d:\n", tick); } while (!is_list_empty(chunks_lifetime[tick])) { int8_t *ptr = get_and_remove(chunks_lifetime[tick]); if (need_trace) { printf(" Removing chunk with size %d bytes\n", get_chunk_size(ptr)); } free_chunk(mem, ptr); } SIZE_TYPE chunk_size = rand_range(min_chunk_size, max_chunk_size) / 4 * 4; int8_t *ptr = allocate_chunk(mem, chunk_size); if (ptr == NULL) { printf("Oops. We have no free memory to allocate %d bytes on tick %d. Exiting\n", chunk_size, tick); break; } int chunk_lifetime = rand_range(min_lifetime, max_lifetime); if (tick + chunk_lifetime >= max_ticks) { printf("The maximum number of ticks(%d) reached. Exiting\n", max_ticks); break; } add_to_list(chunks_lifetime[tick + chunk_lifetime], ptr); if (need_trace){ printf(" Allocating chunk with size %d bytes. Its lifetime is %d ticks\n", chunk_size, chunk_lifetime); } tick++; } for (int i = 0; i < max_ticks; i++) { free_list(chunks_lifetime[i]); } free_memory(mem); free(chunks_lifetime); return 0; }
int main(int argc, char **argv) { char input; char *target; // Target input file name. char *alg; // fcfs or multi. int memsize; // Given virtual memsize. if (argc != 7) { print_usage(argv[0]); return -1; } // Getting the options for program execution. while ((input = getopt(argc, argv, "f:a:m:")) != EOF) { switch ( input ) { case 'f': // Ascertaining which file to read the input from. target = optarg; break; case 'a': // Ascertaining which algorithm to use. if(strcmp(optarg, FCFS) == 0) alg = optarg; else if(strcmp(optarg, MULTI) == 0) alg = optarg; else { // Exit if optarg unknown fprintf(stderr, "Invalid scheduling option %s\n", optarg); print_usage(argv[0]); exit(1); } break; case 'm': // Ascertaining total memory size. memsize = atoi(optarg); break; default: // Should not get here. print_usage(argv[0]); return -1; } } /* ** Here we read processes into disk. Throughout the life of the ** simulation, this disk linked list will hold both processes that ** haven't yet been started (future processes whose start times are ** greater than the current simulation time) as well as processes ** that have been swapped back to disk because memory was too full. */ int num_processes = 0; Process *disk_head = read_processes(target, memsize, &num_processes); if (disk_head == NULL) { fprintf(stderr, "%s couldn't be read properly, exiting...\n", target); return -1; } Memory *memory = create_memory(memsize); simulate(disk_head, num_processes, memory, alg); return 0; }
main() { int index = 0; int size = 0; pMem pOwnHead;//原始内存链表 pMem pGetHead;//分配内存链表 pGet pGetaddr = NULL;//记录链表 pGet temp_addr ; init_memory(&pOwnHead);//初始化头结点 create_memory(pOwnHead,10);//创建10个结构体 init_memory(&pGetHead);//初始化分配内存链表 init_get_addr(&pGetaddr);// 初始化记录链接表 temp_addr = pGetaddr; while(index!=5) { printf("输入要进行的操作:\n"); printf(" 1--------Create\n"); printf(" 2--------Delete\n"); printf(" 3--------Display the free space\n"); printf(" '5'--------Exit\n"); scanf("%d",&index); switch(index) { case CREATE: { printf("输入要开辟的空间:\n"); scanf("%d",&size); get_memory(pOwnHead,pGetHead,pGetaddr,size); }break; case DELETE: { if(temp_addr->next!=pGetaddr) { free_memory(pOwnHead,pGetHead,temp_addr->next); temp_addr = temp_addr->next; } }break; case DISPLAY1: { printf("空闲的空间为:\n"); display_free(pOwnHead); printf("已经分配的空间为:\n"); display_free(pGetHead); }break; case '5':break; default:break; } } }