@@ -58,6 +58,10 @@ static mem_chunk_t *tail;
5858
5959static size_t current_free_size ;
6060
61+ /**< Start and end addresses of memory acquired from sbrk. */
62+ static void * heap_start ;
63+ static void * heap_end ;
64+
6165/**
6266 * @brief Removes a free memory block from the list of free blocks in hash table.
6367 *
@@ -73,6 +77,8 @@ static void HMMremove_free_block(mem_chunk_t *block)
7377 return ;
7478 }
7579 size_t cur_size = block -> size ;
80+ if (cur_size < ALIGNMENT )
81+ return ;
7682 size_t idx = (cur_size / ALIGNMENT ) - 1 ;
7783 // If the index is within bounds, traverse the free list to remove the block.
7884
@@ -102,16 +108,6 @@ static void HMMremove_free_block(mem_chunk_t *block)
102108 }
103109 }
104110}
105- /**
106- * @brief Checks if a memory block is found in the list of free blocks in hash table.
107- *
108- * @param block Pointer to the memory block to be checked.
109- * @return 1 if the block is found, 0 otherwise.
110- */
111- static unsigned char HMMis_block_found (mem_chunk_t * block )
112- {
113- return block -> is_added ;
114- }
115111/**
116112 * @brief Adds a free memory block to the list of free blocks in hash table.
117113 *
@@ -123,6 +119,8 @@ static void HMMadd_free_block(mem_chunk_t *block)
123119 return ;
124120// Calculate the index for the block based on its size.
125121 size_t cur_size = block -> size ;
122+ if (cur_size < ALIGNMENT )
123+ return ;
126124 size_t idx = (cur_size / ALIGNMENT ) - 1 ;
127125 // If the index is within bounds and the block is not already added, add it to the free list.
128126
@@ -152,6 +150,8 @@ static void HMMadd_free_block(mem_chunk_t *block)
152150 */
153151static mem_chunk_t * HMMget_free_block (size_t size )
154152{
153+ if (size < ALIGNMENT )
154+ return NULL ;
155155 size_t idx = (size / ALIGNMENT ) - 1 ;
156156 if (idx < MULTIPLES_MAX )
157157 {
@@ -247,6 +247,7 @@ static mem_chunk_t *HMMget_free_chunk(size_t size)
247247 splitted -> prev = current ;
248248 splitted -> next = current_next ;
249249 splitted -> is_free = 1 ;
250+ splitted -> is_added = 0 ;
250251 if (current_next )
251252 current_next -> prev = splitted ;
252253 current -> next = splitted ;
@@ -270,7 +271,12 @@ static mem_chunk_t *HMMget_free_chunk(size_t size)
270271 // If no free block is large enough, allocate new memory from the system.
271272
272273 size_t allocation_size = ALLOCATED_BYTES ;
273- size_t num_allocated_bytes = ((size + sizeof (mem_chunk_t ) + allocation_size ) / allocation_size ) * allocation_size ;
274+ if (size > (SIZE_MAX - sizeof (mem_chunk_t ) - allocation_size ))
275+ {
276+ return NULL ;
277+ }
278+ size_t total_requested = size + sizeof (mem_chunk_t ) + allocation_size ;
279+ size_t num_allocated_bytes = (total_requested / allocation_size ) * allocation_size ;
274280 void * new_free_space = sbrk (num_allocated_bytes );
275281 if (new_free_space == (void * )-1 )
276282 {
@@ -289,6 +295,10 @@ static mem_chunk_t *HMMget_free_chunk(size_t size)
289295 // Initialize a new memory chunk and add it to the list.
290296
291297 mem_chunk_t * new_chunk = (mem_chunk_t * )new_free_space ;
298+ if (heap_start == NULL )
299+ heap_start = new_free_space ;
300+ heap_end = (unsigned char * )new_free_space + num_allocated_bytes ;
301+ new_chunk -> is_added = 0 ;
292302 new_chunk -> is_free = 1 ;
293303 new_chunk -> size = num_allocated_bytes - sizeof (mem_chunk_t );
294304 new_chunk -> prev = tail ;
@@ -315,6 +325,15 @@ static void HMMfree(void *ptr)
315325 }
316326 // Get the memory chunk from the given pointer.
317327
328+ if (heap_start == NULL || heap_end == NULL )
329+ {
330+ return ;
331+ }
332+ if ((unsigned char * )ptr < ((unsigned char * )heap_start + sizeof (mem_chunk_t )) ||
333+ (unsigned char * )ptr >= (unsigned char * )heap_end )
334+ {
335+ return ;
336+ }
318337 mem_chunk_t * alloacted_member = (mem_chunk_t * )((unsigned char * )ptr - sizeof (mem_chunk_t ));
319338 // Check if the memory is already free, if not, free it.
320339
@@ -378,6 +397,7 @@ static void HMMfree(void *ptr)
378397 {
379398 return ;
380399 }
400+ heap_end = new_break ;
381401 }
382402 }
383403}
@@ -536,8 +556,8 @@ void HMMtraverse(void)
536556 size_t cnt = 1 ;
537557 while (cur )
538558 {
539- printf ("Node number: %d , Address: %10p, free: %lu , size: %lu \r\n" , cnt , cur , cur -> is_free , cur -> size );
559+ printf ("Node number: %zu , Address: %10p, free: %u , size: %zu \r\n" , cnt , ( void * ) cur , ( unsigned int ) cur -> is_free , cur -> size );
540560 cnt ++ ;
541561 cur = cur -> next ;
542562 }
543- }
563+ }
0 commit comments