Fix memory leaks and code cleanup

This commit is contained in:
Isaac Shoebottom 2023-12-02 21:55:24 -04:00
parent 8a5b7ce7fc
commit 64f9b63215
3 changed files with 13 additions and 13 deletions

View File

@ -47,6 +47,7 @@ void delete(DiskQueue *queue, DiskRequest request) {
previous->next = current->next; previous->next = current->next;
} }
queue->size--; queue->size--;
free(current);
return; return;
} }
previous = current; previous = current;
@ -60,11 +61,11 @@ DiskRequest peek(DiskQueue *queue) {
return *queue->head; return *queue->head;
} }
DiskRequest dequeue(DiskQueue *queue) { DiskRequest *dequeue(DiskQueue *queue) {
DiskRequest *head = queue->head; DiskRequest *head = queue->head;
queue->head = head->next; queue->head = head->next;
queue->size--; queue->size--;
return *head; return head;
} }
void destroyDiskQueue(DiskQueue *queue) { void destroyDiskQueue(DiskQueue *queue) {

View File

@ -23,7 +23,7 @@ void delete(DiskQueue *queue, DiskRequest request);
DiskRequest peek(DiskQueue *queue); DiskRequest peek(DiskQueue *queue);
DiskRequest dequeue(DiskQueue *queue); DiskRequest *dequeue(DiskQueue *queue);
DiskRequest *findClosest(DiskQueue *queue, int position, double time); DiskRequest *findClosest(DiskQueue *queue, int position, double time);

View File

@ -36,11 +36,12 @@ int diskMovement(int position, int destination) {
void fcfs(int start) { void fcfs(int start) {
int position = start; int position = start;
while (queue->size > 0) { while (queue->size > 0) {
DiskRequest request = dequeue(queue); DiskRequest *request = dequeue(queue);
while (seekTime < request.time) { seekTime++; } while (seekTime < request->time) { seekTime++; }
seekTime += timeToProcessRequest(position, request.position); seekTime += timeToProcessRequest(position, request->position);
movement += diskMovement(position, request.position); movement += diskMovement(position, request->position);
position = request.position; position = request->position;
free(request);
} }
} }
@ -53,11 +54,9 @@ void cscan(int start) {
seekTime++; seekTime++;
} else { } else {
if (request->position < position) { if (request->position < position) {
seekTime += timeToProcessRequest(position, 9999); // + 1 simulates the time it takes to move to the end of the disk (it is a circular disk)
movement += diskMovement(position, 9999); seekTime += timeToProcessRequest(position, 9999 + 1);
// Special case for when we are at the end of the disk, wrap around to the beginning movement += diskMovement(position, 9999 + 1);
seekTime += timeToProcessRequest(9999, 10000); // 10000 is the beginning of the disk
movement += diskMovement(9999, 10000);
position = 0; position = 0;
// Rescan for closest, so we can efficiently handle requests // Rescan for closest, so we can efficiently handle requests
request = findClosest(queue, position, seekTime); request = findClosest(queue, position, seekTime);