Heap Assignment/malloc.c
struct _block *findFreeBlock(struct _block **last, size_t size) 
{
   struct _block *curr = heapList;
   struct _block *blocks = curr; // a placeholder for use of future looping
   struct _block *heapsize = curr;

#if defined FIT && FIT == 0
   /* First fit */
   while (curr && !(curr->free && curr->size >= size)) 
   {
      *last = curr;
      curr  = curr->next;
   }
#endif

#if defined BEST && BEST == 0
   size_t temp = 10000000; // a large number to compare to
   struct _block *best = NULL; // my temp variable for holding the smallest block leftover
   while (curr != NULL) // while we have something that needs to be allocated
   {
      *last = curr;
      if(curr->free && curr->size > size )
         if((curr->size - size) < temp)   // do it like a bubble sort, with the temp variable getting changed
         {
            temp = curr->size - size;
            best = curr;
         }
      curr = curr->next;
   }

   curr = best;

   

#endif

#if defined WORST && WORST == 0
   size_t temp = 0; // a small number to compare to
   struct _block *worst = NULL; // my temp variable for holding the smallest block leftover
   while (curr != NULL) // while we have something that needs to be allocated
   {
      *last = curr;
      if(curr->free && curr->size > size )
         if((curr->size - size) > temp)   // second verse same as the first but with flipped directions
         {
            temp = curr->size - size;
            worst = curr;
         }
      curr = curr->next;
   }

   curr = worst;


#endif

#if defined NEXT && NEXT == 0

   // Using the next location available with large enough size after an allocation looping back to the first
   if(memory)
   {
      curr = memory;
   }
   while (curr && !(curr->free && curr->size >= size)) 
   {
      *last = curr;
      curr  = curr->next;
   }
   memory = curr;
#endif

   // To find the number of blocks we need to count through all available blocks whether used or not.
   // Need to have this outside all of the fit test cases so it applie to them all, about an hour of confusing infinite loops for that one
   while(blocks) // Start with a placeholder variable that has the location of the original first curr
   {
      num_blocks++;
      blocks = blocks->next;
   }
   // To find the heap size we need to find the amount of blocks and add the size of each block to the heapsize
   while(heapsize)
   {
      max_heap = max_heap + heapsize->size; // heapsize starts at 0, adding the size of each block to the size will give total size
      heapsize = heapsize->next;
   }

   return curr;
}
/*
 * \brief findFreeBlock
 *
 * \param last pointer to the linked list of free _blocks
 * \param size size of the _block needed in bytes 
 *
 * \return a _block that fits the request or NULL if no free _block matches
 *
 * \TODO Implement Next Fit
 * \TODO Implement Best Fit
 * \TODO Implement Worst Fit
 */
\brief findFreeBlock
\param last pointer to the linked list of free _blocks
\param size size of the _block needed in bytes

\return a _block that fits the request or NULL if no free _block matches

\TODO Implement Next Fit
\TODO Implement Best Fit
\TODO Implement Worst Fit
\ brief findFreeBlock \ param last pointer to the linked list of free _blocks \ param size size of the _block needed in bytes \ return a _block that fits the request or NULL if no free _block matches \ TODO Implement Next Fit \ TODO Implement Best Fit \ TODO Implement Worst Fit
struct _block *findFreeBlock(struct _block **last, size_t size) 
{
   struct _block *curr = heapList;
   struct _block *blocks = curr; 
   struct _block *heapsize = curr;
#if defined FIT && FIT == 0
   while (curr && !(curr->free && curr->size >= size)) 
   {
      *last = curr;
      curr  = curr->next;
   }
#endif
#if defined BEST && BEST == 0
   size_t temp = 10000000; 
   struct _block *best = NULL; 
   while (curr != NULL) 
   {
      *last = curr;
      if(curr->free && curr->size > size )
         if((curr->size - size) < temp)   
         {
            temp = curr->size - size;
            best = curr;
         }
      curr = curr->next;
   }
   curr = best;
#endif
#if defined WORST && WORST == 0
   size_t temp = 0; 
   struct _block *worst = NULL; 
   while (curr != NULL) 
   {
      *last = curr;
      if(curr->free && curr->size > size )
         if((curr->size - size) > temp)   
         {
            temp = curr->size - size;
            worst = curr;
         }
      curr = curr->next;
   }
   curr = worst;
#endif
#if defined NEXT && NEXT == 0
   if(memory)
   {
      curr = memory;
   }
   while (curr && !(curr->free && curr->size >= size)) 
   {
      *last = curr;
      curr  = curr->next;
   }
   memory = curr;
#endif
   while(blocks) 
   {
      num_blocks++;
      blocks = blocks->next;
   }
   while(heapsize)
   {
      max_heap = max_heap + heapsize->size; 
      heapsize = heapsize->next;
   }
   return curr;
}
struct _block * findFreeBlock ( struct _block * * last , size_t size ) { struct _block * curr = heapList ; struct _block * blocks = curr ; struct _block * heapsize = curr ; #if defined FIT && FIT == 0 while ( curr && ! ( curr -> free && curr -> size >= size ) ) { * last = curr ; curr = curr -> next ; } #endif #if defined BEST && BEST == 0 size_t temp = 10000000 ; struct _block * best = NULL ; while ( curr != NULL ) { * last = curr ; if ( curr -> free && curr -> size > size ) if ( ( curr -> size - size ) < temp ) { temp = curr -> size - size ; best = curr ; } curr = curr -> next ; } curr = best ; #endif #if defined WORST && WORST == 0 size_t temp = 0 ; struct _block * worst = NULL ; while ( curr != NULL ) { * last = curr ; if ( curr -> free && curr -> size > size ) if ( ( curr -> size - size ) > temp ) { temp = curr -> size - size ; worst = curr ; } curr = curr -> next ; } curr = worst ; #endif #if defined NEXT && NEXT == 0 if ( memory ) { curr = memory ; } while ( curr && ! ( curr -> free && curr -> size >= size ) ) { * last = curr ; curr = curr -> next ; } memory = curr ; #endif while ( blocks ) { num_blocks ++ ; blocks = blocks -> next ; } while ( heapsize ) { max_heap = max_heap + heapsize -> size ; heapsize = heapsize -> next ; } return curr ; }
\brief findFreeBlock
 \param last pointer to the linked list of free _blocks
 \param size size of the _block needed in bytes
\ brief findFreeBlock \ param last pointer to the linked list of free _blocks \ param size size of the _block needed in bytes
// a placeholder for use of future looping /* First fit */ // a large number to compare to // my temp variable for holding the smallest block leftover // while we have something that needs to be allocated // do it like a bubble sort, with the temp variable getting changed // a small number to compare to // my temp variable for holding the smallest block leftover // while we have something that needs to be allocated // second verse same as the first but with flipped directions // Using the next location available with large enough size after an allocation looping back to the first // To find the number of blocks we need to count through all available blocks whether used or not. // Need to have this outside all of the fit test cases so it applie to them all, about an hour of confusing infinite loops for that one // Start with a placeholder variable that has the location of the original first curr // To find the heap size we need to find the amount of blocks and add the size of each block to the heapsize // heapsize starts at 0, adding the size of each block to the size will give total size