/***************************************************
LEAK LIST EXAMPLE:
The program will store the file name and line numbers of all the
LEAK LIST EXAMPLE:
The program will store the file name and line numbers of all the
dynamically allocated pointers using new_malloc and new_free
and maintains the pointers in a linked list.
At the time of exit ofthe program, display_leak_list must be called to check if thereare any unreleased pointers.
The resultant list will display the unreleased pointers list, file name and the line number where this pointer was allocated.
DRAW BACKS: If free() is called on already released pointers, thebehaviour is undefined. Assert statements may be used (doesntwork) to take corrective action.
***************************************************************/
#include "stdio.h"
***************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include <>
#include <>
struct leaklist{
struct leaklist{
int line_number;
char *file_name;
int* mem_ptr;
struct leaklist *next;
};
struct leaklist *list_head;
//typedef struct __leaklist leaklist;
void add_to_leak_list(struct leaklist **list_head, void *ptr,
int buf_size, char *str, int line_no)
{
struct leaklist *new_ptr;
struct leaklist *temp_ptr;
temp_ptr = *list_head;
if(temp_ptr == NULL)
{
temp_ptr = (struct leaklist *)malloc(sizeof(struct leaklist));
temp_ptr->mem_ptr = ptr;
temp_ptr->file_name = (char *)malloc(strlen(str) + 1);
strcpy(temp_ptr->file_name,str);
//printf("File_name: %s\n",temp_ptr->file_name);
temp_ptr->line_number = line_no;
//printf("add_to_leak_list(): temp_ptr->line_number = %d \n",
//temp_ptr->line_number);
//memcpy(temp_ptr->mem_ptr,ptr,buf_size);
temp_ptr->next = NULL;
*list_head = temp_ptr;
//printf("add_to_leak_list(): first node %d\n",*list_head);
}
else
{
while(temp_ptr->next != NULL)
{
temp_ptr = temp_ptr->next;
}
new_ptr = (struct leaklist *)malloc(sizeof(struct leaklist));
if(new_ptr == NULL)
{
printf("\n malloc returned NULL : EXITING \n");
exit(0);
}
//printf("add_to_leak_list(): value of ptr = %d \n", new_ptr);
new_ptr->mem_ptr = ptr;
new_ptr->file_name = (char *)malloc(strlen(str) + 1);
strcpy(new_ptr->file_name,str);
new_ptr->line_number = line_no;
//printf("add_to_leak_list(): temp_ptr->line_number = %d \n",
//temp_ptr->line_number);
//memcpy(temp_ptr->mem_ptr,ptr,buf_size);
new_ptr->next = NULL;
temp_ptr->next = new_ptr;
}
}
void delete_from_leak_list(struct leaklist **list_head, void *ptr)
{
struct leaklist *old_ptr = NULL, *temp_ptr = NULL;
temp_ptr = *list_head;
if(NULL == ptr NULL == temp_ptr)
return;
while(temp_ptr->next != NULL)
{
if(temp_ptr->mem_ptr == ptr)
{
//if the node to be deleted is the head node
if(temp_ptr == *list_head)
{
*list_head = temp_ptr->next;
free(temp_ptr->file_name);
free(temp_ptr);
temp_ptr = NULL;
return;
}
else
{
//if the node to be deleted lies between start and
// end nodes
old_ptr->next = temp_ptr->next;
free(temp_ptr->file_name);
free(temp_ptr);
temp_ptr = NULL;
return;
}
}
else
{
old_ptr = temp_ptr;
temp_ptr = temp_ptr->next;
}
}
if(temp_ptr->next == NULL)
{
//check if the node is the last node in the list
if(temp_ptr->mem_ptr == ptr)
{
if(temp_ptr == *list_head)
*list_head = NULL;
else
old_ptr->next = NULL;
free(temp_ptr->file_name);
free(temp_ptr);
temp_ptr = NULL;
}
}
return;
}
void * my_malloc(unsigned int buf_size, char *str, int line_no)
{
void *ptr = NULL;
ptr = (void *)malloc(buf_size);
add_to_leak_list(&list_head,ptr,buf_size,str,line_no);
//printf("\n my_malloc(): __FILE__ %s \n",str);
//printf("my_malloc(): ptr=%u, buf_size=%d, line_no=%d \n",
//ptr, buf_size,line_no);
return ptr;
}
void my_free(void *buf_ptr)
{
//printf("my_free(): deleting from leak list buf_ptr = %u \n",buf_ptr);
//assert(buf_ptr != NULL);
delete_from_leak_list(&list_head,buf_ptr);
free(buf_ptr);
return;
}
void display_leak_list(struct leaklist **list_head)
{
int count = 0;
struct leaklist *temp_ptr;
temp_ptr = *list_head;
printf("\n");
if(temp_ptr == NULL)
{
printf("list is empty \n");
return;
}
while(temp_ptr != NULL)
{
printf("display_leak_list(): ");
printf("mem_ptr:%u, ",temp_ptr->mem_ptr);
printf("line_no:%d \n",temp_ptr->line_number);
printf("file_name:%s \n",temp_ptr->file_name);
temp_ptr = temp_ptr->next;
count++;
}
printf("Total Unreleased Pointers = %d,\n",count);
}
int main()
{
int a = 1, *ptr, *ptr1, *ptr2;
char name[] = "test1\n";
char *str1;
//ptr = (int *)malloc(sizeof(int));
ptr = (int *)my_malloc(sizeof(int),__FILE__,__LINE__);
*ptr = a;
printf("main(): value of ptr=%u, *ptr=%d\n",ptr,*ptr);
//str1 = (char*)malloc(sizeof(name));
str1 = (char *)my_malloc(sizeof(name), __FILE__,__LINE__);
strcpy(str1,name);
printf("main(): value of str1=%s \n",str1);
ptr1 = (int *)my_malloc(sizeof(int),__FILE__,__LINE__);
*ptr1 = 666;
printf("main(): value of ptr1=%u, *ptr1=%d\n",ptr1,*ptr1);
ptr2 = (int *)my_malloc(sizeof(int),__FILE__,__LINE__);
*ptr2 = 777;
printf("main(): value of ptr2=%u, *ptr2=%d\n",ptr2,*ptr2);
//free(ptr);
//free(str1);
//my_free(ptr);
my_free(ptr1);
my_free(str1);
//my_free(ptr1); //Releasing ptr1 2nd time does not work
//my_free(ptr2);
my_free(ptr); //Release pointers out of order
display_leak_list(&list_head);
return 0;
}
struct leaklist *list_head;
//typedef struct __leaklist leaklist;
void add_to_leak_list(struct leaklist **list_head, void *ptr,
int buf_size, char *str, int line_no)
{
struct leaklist *new_ptr;
struct leaklist *temp_ptr;
temp_ptr = *list_head;
if(temp_ptr == NULL)
{
temp_ptr = (struct leaklist *)malloc(sizeof(struct leaklist));
temp_ptr->mem_ptr = ptr;
temp_ptr->file_name = (char *)malloc(strlen(str) + 1);
strcpy(temp_ptr->file_name,str);
//printf("File_name: %s\n",temp_ptr->file_name);
temp_ptr->line_number = line_no;
//printf("add_to_leak_list(): temp_ptr->line_number = %d \n",
//temp_ptr->line_number);
//memcpy(temp_ptr->mem_ptr,ptr,buf_size);
temp_ptr->next = NULL;
*list_head = temp_ptr;
//printf("add_to_leak_list(): first node %d\n",*list_head);
}
else
{
while(temp_ptr->next != NULL)
{
temp_ptr = temp_ptr->next;
}
new_ptr = (struct leaklist *)malloc(sizeof(struct leaklist));
if(new_ptr == NULL)
{
printf("\n malloc returned NULL : EXITING \n");
exit(0);
}
//printf("add_to_leak_list(): value of ptr = %d \n", new_ptr);
new_ptr->mem_ptr = ptr;
new_ptr->file_name = (char *)malloc(strlen(str) + 1);
strcpy(new_ptr->file_name,str);
new_ptr->line_number = line_no;
//printf("add_to_leak_list(): temp_ptr->line_number = %d \n",
//temp_ptr->line_number);
//memcpy(temp_ptr->mem_ptr,ptr,buf_size);
new_ptr->next = NULL;
temp_ptr->next = new_ptr;
}
}
void delete_from_leak_list(struct leaklist **list_head, void *ptr)
{
struct leaklist *old_ptr = NULL, *temp_ptr = NULL;
temp_ptr = *list_head;
if(NULL == ptr NULL == temp_ptr)
return;
while(temp_ptr->next != NULL)
{
if(temp_ptr->mem_ptr == ptr)
{
//if the node to be deleted is the head node
if(temp_ptr == *list_head)
{
*list_head = temp_ptr->next;
free(temp_ptr->file_name);
free(temp_ptr);
temp_ptr = NULL;
return;
}
else
{
//if the node to be deleted lies between start and
// end nodes
old_ptr->next = temp_ptr->next;
free(temp_ptr->file_name);
free(temp_ptr);
temp_ptr = NULL;
return;
}
}
else
{
old_ptr = temp_ptr;
temp_ptr = temp_ptr->next;
}
}
if(temp_ptr->next == NULL)
{
//check if the node is the last node in the list
if(temp_ptr->mem_ptr == ptr)
{
if(temp_ptr == *list_head)
*list_head = NULL;
else
old_ptr->next = NULL;
free(temp_ptr->file_name);
free(temp_ptr);
temp_ptr = NULL;
}
}
return;
}
void * my_malloc(unsigned int buf_size, char *str, int line_no)
{
void *ptr = NULL;
ptr = (void *)malloc(buf_size);
add_to_leak_list(&list_head,ptr,buf_size,str,line_no);
//printf("\n my_malloc(): __FILE__ %s \n",str);
//printf("my_malloc(): ptr=%u, buf_size=%d, line_no=%d \n",
//ptr, buf_size,line_no);
return ptr;
}
void my_free(void *buf_ptr)
{
//printf("my_free(): deleting from leak list buf_ptr = %u \n",buf_ptr);
//assert(buf_ptr != NULL);
delete_from_leak_list(&list_head,buf_ptr);
free(buf_ptr);
return;
}
void display_leak_list(struct leaklist **list_head)
{
int count = 0;
struct leaklist *temp_ptr;
temp_ptr = *list_head;
printf("\n");
if(temp_ptr == NULL)
{
printf("list is empty \n");
return;
}
while(temp_ptr != NULL)
{
printf("display_leak_list(): ");
printf("mem_ptr:%u, ",temp_ptr->mem_ptr);
printf("line_no:%d \n",temp_ptr->line_number);
printf("file_name:%s \n",temp_ptr->file_name);
temp_ptr = temp_ptr->next;
count++;
}
printf("Total Unreleased Pointers = %d,\n",count);
}
int main()
{
int a = 1, *ptr, *ptr1, *ptr2;
char name[] = "test1\n";
char *str1;
//ptr = (int *)malloc(sizeof(int));
ptr = (int *)my_malloc(sizeof(int),__FILE__,__LINE__);
*ptr = a;
printf("main(): value of ptr=%u, *ptr=%d\n",ptr,*ptr);
//str1 = (char*)malloc(sizeof(name));
str1 = (char *)my_malloc(sizeof(name), __FILE__,__LINE__);
strcpy(str1,name);
printf("main(): value of str1=%s \n",str1);
ptr1 = (int *)my_malloc(sizeof(int),__FILE__,__LINE__);
*ptr1 = 666;
printf("main(): value of ptr1=%u, *ptr1=%d\n",ptr1,*ptr1);
ptr2 = (int *)my_malloc(sizeof(int),__FILE__,__LINE__);
*ptr2 = 777;
printf("main(): value of ptr2=%u, *ptr2=%d\n",ptr2,*ptr2);
//free(ptr);
//free(str1);
//my_free(ptr);
my_free(ptr1);
my_free(str1);
//my_free(ptr1); //Releasing ptr1 2nd time does not work
//my_free(ptr2);
my_free(ptr); //Release pointers out of order
display_leak_list(&list_head);
return 0;
}
/*****************************************************/
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.