void kref(char *name, FILE *fp, Table_T identifiers) { char buf[512], *filename = ""; int linenum; if (name) filename = name; for (linenum = 1; fgets(buf, sizeof buf, fp) != NULL; linenum++) { int i, j; for (i = 0; (j = getword(buf, &i, first, rest)) > 0; i = j) { char *id = Str_sub(buf, j, i); const char *ln = Atom_int(linenum); Seq_T seq; Table_T files; files = Table_get(identifiers, id); if (files == NULL) { files = Table_new(0, (int (*)(const void *, const void *))strcmp, strhash); Table_put(identifiers, id, files); } else FREE(id); seq = Table_get(files, filename); if (seq == NULL) { seq = Seq_new(0); Table_put(files, Str_dup(filename, 1, 0, 1), seq); Seq_addlo(seq, (void *)ln); } else if (Seq_get(seq, 0) != ln) Seq_addlo(seq, (void *)ln); } } }
T Umsections_new (const char *section, int (*error)(void *errstate, const char *message), void *errstate) { T Umsections = malloc(sizeof(*Umsections)); Umsections->sections = Table_new(TABLE_HINT, NULL, NULL); Seq_T temp; temp = Seq_new(SEQ_HINT); Seq_T temp2; temp2 = Seq_new(SEQ_HINT); Umsections->error = error; Umsections->errstate = errstate; Umsections->currentSection = temp; Umsections->sectionOrder = temp2; Seq_addlo(Umsections->sectionOrder, temp); Table_put(Umsections->sections, Atom_string(section), temp); /* Table_map((Umsections)->sections,applyFree,NULL); Table_free(&((Umsections)->sections)); Seq_free(&((Umsections)->sectionOrder)); // Seq_free(((Umsectinos)->currentSection)); free(Umsections); exit(1);*/ return Umsections; }
/* Unmaps identified segment from memory, allows unchecked runtime error if * segment doesn't exist. */ void Segment_unmap(T seg_memory, ID_SIZE id) { WORD_SIZE *temp; Seq_addlo(seg_memory->unmapped_ids, (void *)(uintptr_t)id); temp = Seq_get(seg_memory->segments, id); free(temp); Seq_put(seg_memory->segments, id, NULL); }
/* initialize the new mem and return it */ Mem new_mem(Segment_T program) { Mem myMem = malloc(sizeof(*myMem)); assert(myMem != NULL); myMem->segs = Seq_new(2); assert(myMem->segs != NULL); Seq_addlo(myMem->segs, (void*) program); myMem->unmapped = Stack_new(); myMem->prog_counter = 0; return myMem; }
static void add_program_segment(UM_machine machine, char *file) { struct stat fileStats; FILE *FILE = fopen(file, "r"); /* exit if the file does not exist or if stat fails */ if(stat(file, &fileStats) < 0){ fprintf(stderr, "Stat failed. Invalid file.\n"); exit(EXIT_FAILURE); } int fileSize = fileStats.st_size; /* exit if fileSize indicates the wrong number of bytes */ if(fileSize%4 != 0){ fprintf(stderr, "Program file formatted incorrectly.\n"); exit(EXIT_FAILURE); } int num_instructions = fileSize / BYTES_PER_INSTRUCTION; UM_segment program_segment = get_new_segment_of_size(num_instructions); /* get the instructions from the input file */ for(int i=0; i<num_instructions; i++){ UM_instruction next_instruction = get_next_instruction(FILE); UM_instruction *instruction_i_p = UArray_at(program_segment->words, i); *instruction_i_p = next_instruction; } /* put the program segment in the mapped segments sequence */ Seq_addlo(machine->address_space->mapped_segments, program_segment); machine->address_space->ID_counter++; machine->num_instructions = num_instructions; fclose(FILE); }