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;
}
queue->size--;
free(current);
return;
}
previous = current;
@ -60,11 +61,11 @@ DiskRequest peek(DiskQueue *queue) {
return *queue->head;
}
DiskRequest dequeue(DiskQueue *queue) {
DiskRequest *dequeue(DiskQueue *queue) {
DiskRequest *head = queue->head;
queue->head = head->next;
queue->size--;
return *head;
return head;
}
void destroyDiskQueue(DiskQueue *queue) {

View File

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

View File

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