Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MEMSIZE 100
- char elements[MEMSIZE] = "";
- struct node_t
- {
- int start, size;
- struct node_t *next;
- };
- mymalloc (char memory[], struct node_t **freelist, struct node_t **usedlist,
- int size);
- printmemory (char memory[], struct node_t *freelist, struct node_t *usedlist);
- mymalloc (char memory[], struct node_t **freelist, struct node_t **usedlist,
- int size)
- {
- // Fill up the memory array with Xs
- //Check if there is any available space to add the new memory
- //If not stop the proccess
- // Else start creating at the found location
- struct node_t *currFree = *freelist;
- struct node_t *new = NULL;
- struct node_t *prev = NULL;
- while(currFree!=NULL){
- if(currFree->size>=size){
- // Start Allocation of memory
- // Drawing in the array with Xs
- for (int i = currFree->start; i < size;i++){
- memory[i] = 'X';
- }
- //Fill up the used list
- if(*usedlist == NULL){
- *usedlist = (struct node_t *) malloc (sizeof (struct node_t));
- (*usedlist)->start = currFree->start;
- (*usedlist)->size = size;
- (*usedlist)->next = NULL;
- }
- else{
- new = (struct node_t *) malloc (sizeof (struct node_t));
- new->start = currFree->start;
- new->size = size;
- new->next = (*usedlist)->next;
- (*usedlist)->next = new;
- }
- printf("Variable starts at address %d\n",currFree->start);
- //Fill Up the free list
- currFree->size = currFree->size - size;
- if(currFree->size == 0){
- if(prev == NULL){
- free(currFree);
- *freelist=NULL;
- }
- else{
- prev->next = currFree->next;
- free(currFree);
- }
- }
- else{
- currFree->start = currFree->start + size;
- }
- return;
- }
- currFree=currFree->next;
- }
- printf("There is no contiguous memory range that is large enough\n");
- }
- printmemory (char memory[], struct node_t *freelist, struct node_t *usedlist)
- {
- int usedCounter = 0;
- printf ("memory map\n");
- for (int i = 0; i < MEMSIZE; i++)
- {
- if (memory[i] == '\0')
- {
- printf (".");
- }
- else
- {
- printf ("X");
- }
- }
- printf ("\nfree list\n");
- struct node_t *curr = freelist;
- while (curr != NULL)
- {
- printf ("start=%04d, size=%04d\n", curr->start, curr->size);
- curr = curr->next;
- }
- curr = usedlist;
- printf ("used list\n");
- while (curr != NULL)
- {
- printf ("start=%04d, size=%04d\n", curr->start, curr->size);
- usedCounter += curr->size;
- curr = curr->next;
- }
- printf ("%0d bytes free, %0d bytes used\n", MEMSIZE - usedCounter,
- usedCounter);
- }
- int
- main (void)
- {
- struct node_t *freelist = (struct node_t *) malloc (sizeof (struct node_t));
- freelist->start = 0;
- freelist->size = MEMSIZE;
- freelist->next = NULL;
- struct node_t *usedlist = NULL;
- char cmd;
- do
- {
- int correctCmd = 0;
- char commands[] = { 'q', 'm', 'p', 'v', 'f', 'g', 'e' };
- printf ("Command: ");
- scanf (" %c", &cmd);
- for (int i = 0; i < strlen (commands); i++)
- {
- if (commands[i] == cmd)
- {
- correctCmd = 1;
- }
- }
- if (!correctCmd)
- {
- printf ("Invalid command '%c'\n", cmd);
- }
- else
- {
- if (cmd == 'p'){
- printmemory (elements, freelist, usedlist);
- }
- if (cmd == 'm'){
- char name;
- int nBytes;
- printf("Variable name? ");
- scanf(" %c",&name);
- printf("Malloc how many bytes? ");
- scanf(" %d",&nBytes);
- mymalloc(elements,&freelist,&usedlist,nBytes);
- }
- }
- }
- while (cmd != 'q');
- printf ("Bye!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement