/************************************************************************/
/* File Name : lc4_memory.c */
/* Purpose : This file implements the linked_list helper functions */
/* to manage the LC4 memory */
/* */
/* Author(s) : tjf and you */
/************************************************************************/
#include
#include “lc4_memory.h”
#include
#include
/*
* adds a new node to linked list pointed to by head – should keep list in order by address
*/
int add_to_list (row_of_memory** head,
short unsigned int address,
short unsigned int contents) {
/* allocate memory for a single node – pointing to beginning of heap */
row_of_memory* row_ptr = malloc (sizeof(row_of_memory));
/* return -1 if malloc fails */
if (row_ptr == NULL){
printf(“Heap memory allocation failed.”);
return -1;
}
/* populate fields in newly allocated node w/ address&contents */
row_ptr->address = address;
row_ptr->label = NULL;
row_ptr->contents = contents;
row_ptr->assembly = NULL;
row_ptr->next = NULL;
/* if head==NULL, node created is the new hea d of the list! */
if(*head==NULL){
*head = row_ptr;
}
/* otherwise, traverse linked list until you reach an address before the address passed in */
row_of_memory* head_ptr = *head; // save the start of list
while ((*head)->next != NULL){
*head = (*head)->next;
}
/* insert node into the list – perform necessary “surgery” on this list */
// glue new instr to end of list
(*head)->next = row_ptr;
/* return 0 for success, -1 if malloc fails */
return 0 ;
}
/*
* search linked list by address field, returns node if found
*/
row_of_memory* search_address (row_of_memory* head,
short unsigned int address )
{
/* traverse linked list, searching each node for “address” */
while((head != NULL) && (head->address != address))
head = head->next;
/* return pointer to node in the list if item is found */
/* return NULL if list is empty or if “address” isn’t found */
return head;
}
/*
* search linked list by opcode field, returns node if found
*/
row_of_memory* search_opcode (row_of_memory* head,
short unsigned int opcode )
{
/* traverse linked list until node is found with matching opcode
AND “assembly” field of node is empty */
while((head != NULL) && ((head->contents >>12) != opcode) && (head->assembly != NULL))
head = head->next;
/* return pointer to node in the list if item is found */
/* return NULL if list is empty or if no matching nodes */
return head;
}
void print_list (row_of_memory* head )
{
/* make sure head isn’t NULL */
if (head != NULL){
printf(“%s %d %d %s \n”,head->label, head->address, head->contents, head->assembly);
/* print out a header */
/* traverse linked list, print contents of each node */
print_list(head->next);
} else {
printf(“Printing Error.”);
}
return ;
}
/*
* delete entire linked list
*/
int delete_list (row_of_memory** head )
{
/* delete entire list node by node */
do{
row_of_memory* ptr2 = *head;
row_of_memory* ptr1;
while(ptr2->next != NULL){
free(ptr2->label);
free(ptr2->assembly);
free(ptr2);
ptr1 = ptr2;
ptr2 = ptr2->next;
}
if(ptr2->next == NULL){
free(ptr2->label);
free(ptr2->assembly);
free(ptr2);
break;
}
/* if no errors, set head = NULL upon deletion */
ptr1->next = NULL;
head = NULL;
} while(1);
/* return 0 if no error, -1 for any errors that may arise */
return 0 ;
}