Heap Assignment/malloc.c
void *malloc(size_t size) 
{

   num_requested = num_requested + size;
   if( atexit_registered == 0 )
   {
      atexit_registered = 1;
      atexit( printStatistics );
   }

   /* Align to multiple of 4 */
   size = ALIGN4(size);

   /* Handle 0 size */
   if (size == 0) 
   {
      return NULL;
   }

   /* Look for free _block */
   struct _block *last = heapList;
   struct _block *next = findFreeBlock(&last, size);

   /* TODO: Split free _block if possible */
   
   if(next != NULL && next->size > size)
   {
      struct _block *old = next;  // hold the old value
      struct _block *new = next;  // set the new value to the original value
      struct _block *new2 = new;  // make another new value that is equal to the first new value
      new->size = old->size - size; // set the size of the new value to be the lower value
      new->free = false;  // flag the block as being used
      new->prev= next->prev;  // make the previous block to the new to be the same as the original
      new2->prev = new;  // set the previous for the second new value, attaching it
      new2->size = old->size - new->size; // change the size of the second new to be the difference in original and first new
      num_splits++;  // increment splits
      next = new;  // Change the current into the new to continue throuhg iterations
      
   }
   
   /* Could not find free _block, so grow heap */
   if (next == NULL) 
   {
      next = growHeap(last, size);
      num_grows++;
   }

   /* Could not find free _block or grow heap, so just return NULL */
   if (next == NULL) 
   {
      return NULL;
   }
   
   if(next->free == false)
   {
      num_reuses++;
   }
   /* Mark _block as in use */
   next->free = false;

   num_mallocs++;
   /* Return data address associated with _block */
   return BLOCK_DATA(next);
}
/*
 * \brief malloc
 *
 * finds a free _block of heap memory for the calling process.
 * if there is no free _block that satisfies the request then grows the 
 * heap and returns a new _block
 *
 * \param size size of the requested memory in bytes
 *
 * \return returns the requested memory allocation to the calling process 
 * or NULL if failed
 */
\brief malloc
finds a free _block of heap memory for the calling process.
if there is no free _block that satisfies the request then grows the
heap and returns a new _block

\param size size of the requested memory in bytes

\return returns the requested memory allocation to the calling process
or NULL if failed
\ brief malloc finds a free _block of heap memory for the calling process . if there is no free _block that satisfies the request then grows the heap and returns a new _block \ param size size of the requested memory in bytes \ return returns the requested memory allocation to the calling process or NULL if failed
void *malloc(size_t size) 
{
   num_requested = num_requested + size;
   if( atexit_registered == 0 )
   {
      atexit_registered = 1;
      atexit( printStatistics );
   }
   size = ALIGN4(size);
   if (size == 0) 
   {
      return NULL;
   }
   struct _block *last = heapList;
   struct _block *next = findFreeBlock(&last, size);
   if(next != NULL && next->size > size)
   {
      struct _block *old = next;  
      struct _block *new = next;  
      struct _block *new2 = new;  
      new->size = old->size - size; 
      new->free = false;  
      new->prev= next->prev;  
      new2->prev = new;  
      new2->size = old->size - new->size; 
      num_splits++;  
      next = new;  
   }
   if (next == NULL) 
   {
      next = growHeap(last, size);
      num_grows++;
   }
   if (next == NULL) 
   {
      return NULL;
   }
   if(next->free == false)
   {
      num_reuses++;
   }
   next->free = false;
   num_mallocs++;
   return BLOCK_DATA(next);
}
void * malloc ( size_t size ) { num_requested = num_requested + size ; if ( atexit_registered == 0 ) { atexit_registered = 1 ; atexit ( printStatistics ) ; } size = ALIGN4 ( size ) ; if ( size == 0 ) { return NULL ; } struct _block * last = heapList ; struct _block * next = findFreeBlock ( & last , size ) ; if ( next != NULL && next -> size > size ) { struct _block * old = next ; struct _block * new = next ; struct _block * new2 = new ; new -> size = old -> size - size ; new -> free = false ; new -> prev = next -> prev ; new2 -> prev = new ; new2 -> size = old -> size - new -> size ; num_splits ++ ; next = new ; } if ( next == NULL ) { next = growHeap ( last , size ) ; num_grows ++ ; } if ( next == NULL ) { return NULL ; } if ( next -> free == false ) { num_reuses ++ ; } next -> free = false ; num_mallocs ++ ; return BLOCK_DATA ( next ) ; }
\brief malloc
 finds a free _block of heap memory for the calling process.
\ brief malloc finds a free _block of heap memory for the calling process .
/* Align to multiple of 4 */ /* Handle 0 size */ /* Look for free _block */ /* TODO: Split free _block if possible */ // hold the old value // set the new value to the original value // make another new value that is equal to the first new value // set the size of the new value to be the lower value // flag the block as being used // make the previous block to the new to be the same as the original // set the previous for the second new value, attaching it // change the size of the second new to be the difference in original and first new // increment splits // Change the current into the new to continue throuhg iterations /* Could not find free _block, so grow heap */ /* Could not find free _block or grow heap, so just return NULL */ /* Mark _block as in use */ /* Return data address associated with _block */