Question: * * * please keep the main.c file same. try to fix fscMalloc.c file * File: fscMalloc.h #ifndef FSCMALLOC _ H #define FSCMALLOC _ H

*** please keep the main.c file same.
try to fix fscMalloc.c file
* File: fscMalloc.h
#ifndef FSCMALLOC_H
#define FSCMALLOC_H
#ifdef __cplusplus
extern "C"{
#endif
#include /* for size_t */
#include /* for FILE**/
typedef struct {
size_t size;
int magic;
} fsc_alloc_header_t;
typedef struct _fsc_node_t {
size_t size;
struct _fsc_node_t* next;
} fsc_free_node_t;
typedef struct {
fsc_free_node_t* head;
int magicNumber;
} memoryStructure;
typedef enum {
FIRST_FIT_RETURN_FIRST,
FIRST_FIT_RETURN_SECOND,
BEST_FIT_RETURN_FIRST,
BEST_FIT_RETURN_SECOND,
WORST_FIT_RETURN_FIRST,
WORST_FIT_RETURN_SECOND,
NEXT_FIT_RETURN_FIRST,
NEXT_FIT_RETURN_SECOND
} fscAllocationMethod;
enum { MBToB =1048576};
void* fscMemorySetup(memoryStructure*, fscAllocationMethod, size_t sizeInBytes);
void* fscMalloc(memoryStructure*, size_t sizeInBytes); // returns memory, 0 if failure
void fscFree(memoryStructure*, void *); // returns memory to the pool
void fscMemoryCleanup(memoryStructure*);
void printFreeList(FILE * out, fsc_free_node_t* head);
#ifdef __cplusplus
}
#endif
#endif /* FSCMALLOC_H */
* File: memoryTest.h
#ifndef MEMORYTEST_H
#define MEMORYTEST_H
#ifdef __cplusplus
extern "C"{
#endif
int memoryTestOne();
int memoryTestTwo();
int memoryTestThree();
#ifdef __cplusplus
}
#endif
#endif /* MEMORYTEST_H */
file: fscMalloc.c
void *fscMemorySetup(memoryStructure *m, fscAllocationMethod am, size_t sizeInBytes){
if (am != FIRST_FIT_RETURN_FIRST){
fprintf(stderr, "This code only supports the FIRST_FIT_RETURN_FIRST allocation method
");
return NULL;
}
m->head = malloc(sizeof(fsc_free_node_t));
if (m->head == NULL){
fprintf(stderr, "Memory allocation failed
");
return NULL;
}
m->head->size = sizeInBytes;
m->head->next = NULL;
m->magicNumber =12345; // Example magic number
return m;
}
void *fscMalloc(memoryStructure *m, size_t requestedSizeInBytes){
fsc_free_node_t *current = m->head;
fsc_free_node_t *prev = NULL;
while (current != NULL){
if (current->size >= requestedSizeInBytes){
void *allocatedMemory =(void *)((char *)current + sizeof(fsc_free_node_t));
size_t remainingSize = current->size - requestedSizeInBytes;
if (remainingSize >0){
fsc_free_node_t *newNode =(fsc_free_node_t *)((char *)allocatedMemory + requestedSizeInBytes);
newNode->size = remainingSize;
newNode->next = current->next;
if (prev != NULL){
prev->next = newNode;
} else {
m->head = newNode;
}
} else {
if (prev != NULL){
prev->next = current->next;
} else {
m->head = current->next;
}
}
return allocatedMemory;
}
prev = current;
current = current->next;
}
fprintf(stderr, "Failed to allocate memory
");
return NULL;
}
void fscFree(memoryStructure *m, void *returnedMemory){
fsc_free_node_t *newFreeNode =(fsc_free_node_t *)((char *)returnedMemory - sizeof(fsc_free_node_t));
newFreeNode->next = m->head;
m->head = newFreeNode;
}
void fscMemoryCleanup(memoryStructure *m){
fsc_free_node_t *current = m->head;
while (current != NULL){
fsc_free_node_t *temp = current;
current = current->next;
free(temp);
}
m->head = NULL;
m->magicNumber =0;
}
void printFreeList(FILE *out, fsc_free_node_t *head){
fprintf(out, "About to dump the free list:
");
fsc_free_node_t *current = head;
while (current != NULL){
fprintf(out, "Node address: %p\tNode size (stored): %lu\tNode size (actual): %lu\tNode next: %p
",
current, current->size, current->size + sizeof(fsc_free_node_t), current->next);
current = current->next;
}
}
* File: main.c
int main(int argc, char** argv){
setlocale(LC_NUMERIC,"");
if (0!= memoryTestOne()){
fprintf(stderr,"Failure in memory test one");
exit (EXIT_FAILURE);
}
if (0!= memoryTestTwo()){
fprintf(stderr,"Failure in memory test two");
exit (EXIT_FAILURE);
}
if (0!= memoryTestThree()){
fprintf(stderr,"Failure in memory test three");
exit (EXIT_FAILURE);
}
printf("Tests Succeeded");
return (EXIT_SUCCESS);
}
output:
About to dump the free list:
Node address: 0x17ae300 ode s z s o ed):10485760 ode size (actual): 10485776 o e n x : ( i )
Error on line 30
Failure in memory test one
RUN FINISHED; exit value 1; real time: 40ms; user: 0ms; system: 0ms

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!