SHARE
TWEET

Untitled

a guest Oct 13th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MEMSIZE 100
  6.  
  7. char elements[MEMSIZE] = "";
  8.  
  9. struct node_t
  10. {
  11.   int start, size;
  12.   struct node_t *next;
  13. };
  14.  
  15. mymalloc (char memory[], struct node_t **freelist, struct node_t **usedlist,
  16.       int size);
  17.  
  18. printmemory (char memory[], struct node_t *freelist, struct node_t *usedlist);
  19.  
  20. mymalloc (char memory[], struct node_t **freelist, struct node_t **usedlist,
  21.       int size)
  22. {
  23.   // Fill up the memory array with Xs
  24.  
  25.     //Check if there is any available space to add the new memory
  26.     //If not stop the proccess
  27.     // Else start creating at the found location    
  28.     struct node_t *currFree = *freelist;
  29.     struct node_t *new = NULL;
  30.     struct node_t *prev = NULL;
  31.     while(currFree!=NULL){
  32.         if(currFree->size>=size){
  33.             // Start Allocation of memory
  34.             // Drawing in the array with Xs
  35.             for (int i = currFree->start; i < size;i++){
  36.               memory[i] = 'X';
  37.             }
  38.             //Fill up the used list
  39.             if(*usedlist == NULL){
  40.                 *usedlist = (struct node_t *) malloc (sizeof (struct node_t));
  41.                 (*usedlist)->start = currFree->start;
  42.                 (*usedlist)->size = size;
  43.                 (*usedlist)->next = NULL;
  44.             }
  45.             else{
  46.                 new = (struct node_t *) malloc (sizeof (struct node_t));
  47.                 new->start = currFree->start;
  48.                 new->size = size;
  49.                 new->next = (*usedlist)->next;
  50.                 (*usedlist)->next = new;
  51.             }
  52.             printf("Variable starts at address %d\n",currFree->start);
  53.             //Fill Up the free list
  54.             currFree->size = currFree->size - size;
  55.             if(currFree->size == 0){
  56.                 if(prev == NULL){
  57.                     free(currFree);
  58.                     *freelist=NULL;
  59.                 }
  60.                 else{
  61.                     prev->next = currFree->next;
  62.                     free(currFree);    
  63.                 }
  64.             }
  65.             else{
  66.                 currFree->start = currFree->start + size;
  67.             }
  68.             return;
  69.         }
  70.         currFree=currFree->next;
  71.     }
  72.     printf("There is no contiguous memory range that is large enough\n");
  73. }
  74.  
  75. printmemory (char memory[], struct node_t *freelist, struct node_t *usedlist)
  76. {
  77.   int usedCounter = 0;
  78.   printf ("memory map\n");
  79.   for (int i = 0; i < MEMSIZE; i++)
  80.     {
  81.       if (memory[i] == '\0')
  82.     {
  83.       printf (".");
  84.     }
  85.       else
  86.     {
  87.       printf ("X");
  88.     }
  89.     }
  90.   printf ("\nfree list\n");
  91.   struct node_t *curr = freelist;
  92.   while (curr != NULL)
  93.     {
  94.       printf ("start=%04d, size=%04d\n", curr->start, curr->size);
  95.       curr = curr->next;
  96.     }
  97.   curr = usedlist;
  98.   printf ("used list\n");
  99.   while (curr != NULL)
  100.     {
  101.       printf ("start=%04d, size=%04d\n", curr->start, curr->size);
  102.       usedCounter += curr->size;
  103.       curr = curr->next;
  104.     }
  105.   printf ("%0d bytes free, %0d bytes used\n", MEMSIZE - usedCounter,
  106.       usedCounter);
  107. }
  108.  
  109. int
  110. main (void)
  111. {
  112.   struct node_t *freelist = (struct node_t *) malloc (sizeof (struct node_t));
  113.   freelist->start = 0;
  114.   freelist->size = MEMSIZE;
  115.   freelist->next = NULL;
  116.   struct node_t *usedlist = NULL;
  117.   char cmd;
  118.   do
  119.     {
  120.       int correctCmd = 0;
  121.       char commands[] = { 'q', 'm', 'p', 'v', 'f', 'g', 'e' };
  122.       printf ("Command: ");
  123.       scanf (" %c", &cmd);
  124.       for (int i = 0; i < strlen (commands); i++)
  125.     {
  126.       if (commands[i] == cmd)
  127.         {
  128.           correctCmd = 1;
  129.         }
  130.     }
  131.  
  132.       if (!correctCmd)
  133.     {
  134.       printf ("Invalid command '%c'\n", cmd);
  135.     }
  136.       else
  137.     {
  138.         if (cmd == 'p'){
  139.           printmemory (elements, freelist, usedlist);
  140.         }
  141.         if (cmd == 'm'){
  142.             char name;
  143.             int nBytes;
  144.             printf("Variable name? ");
  145.             scanf(" %c",&name);
  146.             printf("Malloc how many bytes? ");
  147.             scanf(" %d",&nBytes);
  148.             mymalloc(elements,&freelist,&usedlist,nBytes);
  149.         }
  150.     }
  151.     }
  152.   while (cmd != 'q');
  153.   printf ("Bye!\n");
  154.   return 0;
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top