[Task-ID: ODUHIGH-485] Memory Leak Detector Tool
[o-du/l2.git] / tools / Memory_Leak_Detector / scan.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 typedef struct _p{
6 char *func;
7 int size;
8 char *add;
9 }Alloc;
10
11 int allocline;
12 int freeline;
13 Alloc alloc[1000000];
14 Alloc freemem[1000000];
15
16 char allocStr1[] = "ALLOC";
17 char allocStr2[] = "ALLOC_WC";
18 char freeStr[] = "FREE";
19 char shrbAllocStr1[] = "ALLOC_SHRABL_BUF";
20 char shrbAllocStr2[] = "ALLOC_SHRABL_BUF_WC";
21 char shrbAllocStr3[] = "SHRABL_STATIC_BUF_ALLOC";
22 char shrbFreeStr1[] = "FREE_SHRABL_BUF";
23 char shrbFreeStr2[] = "SHRABL_STATIC_BUF_FREE";
24
25 int main()
26 {
27    char buffer[10000];
28    int i=0;
29
30    /* Open input file pointer */
31    FILE *allocfp=fopen("alloc.txt","r");
32    FILE *freefp=fopen("free.txt","r");
33
34    /* Open output file pointer */
35    FILE *allocop=fopen("allocoutput.txt","w");
36    FILE *freeop=fopen("freeoutput.txt","w");
37
38    /* If alloc.txt is open, scan line by line and store "memory size" and
39     * "memory address" in alloc[] structure */
40    if (!allocfp)
41    {
42       return 0;
43    }
44    while(fgets(buffer, 10000, allocfp))
45    {
46       alloc[i].func=malloc(20);
47       alloc[i].add=malloc(20);
48       sscanf(buffer,"%s %d %s", (alloc[i].func), &(alloc[i].size), (alloc[i].add));
49       //printf("%d %s\n",alloc[i].size, alloc[i].add);
50       i++;
51    }
52    allocline=i;
53
54    /* If free.txt is open, scan line by line and store "memory size" and
55     * "memory address" in freemem[] structure */
56    i=0;
57    if (!freefp)
58    {
59       return 0;
60    }
61    while(fgets(buffer, 10000, freefp))
62    {
63       freemem[i].func= malloc(20);
64       freemem[i].add=malloc(20);
65       sscanf(buffer,"%s %d %s", (freemem[i].func), &(freemem[i].size), (freemem[i].add));
66       //printf("%d %s\n",freemem[i].size, freemem[i].add);
67       i++;
68    }
69    freeline=i;
70
71    /* Scan through all entries in alloc[] and freemem[].
72     * If an entry is found in both array, with same size and address, remove this
73     * entry from both */
74    int count1=0;
75    int count2=0;
76    for(count1; count1<allocline; count1++)
77    {
78       for(count2=0; count2<freeline; count2++)
79       {
80          if(( alloc[count1].size == freemem[count2].size ) &&
81                ( !(strcmp(alloc[count1].add,freemem[count2].add)))
82            )
83          {
84             if(
85                   (!(strcmp(alloc[count1].func, allocStr1) && strcmp(alloc[count1].func, allocStr2)) && !(strcmp(freemem[count2].func, freeStr))) ||
86                   (!(strcmp(alloc[count1].func, shrbAllocStr1) && strcmp(alloc[count1].func, shrbAllocStr2) && strcmp(alloc[count1].func, shrbAllocStr3))\
87                   && !(strcmp(freemem[count2].func, shrbFreeStr1) && strcmp(freemem[count2].func, shrbFreeStr2)))
88               )
89             {
90                //printf("%d %s\n",alloc[count1].size, alloc[count1].add);
91                freemem[count2].size=-1;
92                free(freemem[count2].func);
93                freemem[count2].func = 0;
94                free(freemem[count2].add);
95                freemem[count2].add=0;
96
97                alloc[count1].size=-1;
98                free(alloc[count1].func);
99                alloc[count1].func=0;
100                free(alloc[count1].add);
101                alloc[count1].add=0;
102                break;
103             }
104          }
105          else
106          {
107          }
108       }
109       if (count2 == freeline)
110       {
111          //printf("%d %s\n",alloc[count1].size, alloc[count1].add);
112       }
113    }
114
115    /* Add the remaining entries of array[] in allocoutput.txt. These are memory
116     * addresses allocated but not freed */
117    printf("=========== %d %d\n",allocline,freeline);
118    for(count1=0; count1<allocline; count1++)
119    {
120       if( (alloc[count1].size!=-1) && (alloc[count1].add !=0) )
121       {
122          fprintf(allocop,"%d %s\n",alloc[count1].size, alloc[count1].add);
123       }
124    }
125
126    /* Add the remaining entries of freemem[] in freeoutput.txt. These are memory
127     * addresses freed but not allocated */
128    for(count2=0; count2<freeline; count2++)
129    {
130       if( (freemem[count2].size!=-1) && (freemem[count2].add !=0) )
131       {
132          fprintf(freeop,"%d %s\n",freemem[count2].size, freemem[count2].add);
133       }
134    }
135    return 0;
136 }