List of commits:
Subject Hash Author Date (UTC)
master: initial commit of mem_check ede7de7550d3243f98279b5bd69eba18748105fa Josiah_Mullins_E6440 2020-02-12 17:42:39
Commit ede7de7550d3243f98279b5bd69eba18748105fa - master: initial commit of mem_check
Author: Josiah_Mullins_E6440
Author date (UTC): 2020-02-12 17:42
Committer name: Josiah_Mullins_E6440
Committer date (UTC): 2020-02-12 17:42
Parent(s):
Signing key:
Tree: 909542c65930f8eafe6181a67f3ae6471c28b184
File Lines added Lines deleted
mem_check.c 248 0
mem_check.h 44 0
File mem_check.c added (mode: 100644) (index 0000000..e0372fd)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 //non-NULL is the error code.
6 uint64_t* is_array_empty(uint64_t* addr, uint16_t addr_len)
7 {
8 uint64_t* addr_end;
9
10 addr_end = addr + addr_len;
11 do{
12 if(*addr != 0) return addr;
13 addr++;
14 }while(addr != addr_end);
15 return NULL;
16 }
17
18 //NULL is the error code.
19 uint64_t* is_in_array(uint64_t* addr, uint16_t addr_len, uint64_t point)
20 {
21 uint64_t* addr_end;
22
23 addr_end = addr + addr_len;
24 do{
25 if(*addr == point) return addr;
26 addr++;
27 }while(addr != addr_end);
28 return NULL;
29 }
30
31 uint64_t* add_to_array(uint64_t* addr, uint16_t* addr_len, uint64_t data)
32 {
33 uint64_t* addr_ret;
34 uint64_t* addr_new;
35
36 addr_ret = is_in_array(addr,*addr_len,data);//see if already in array
37 if(addr_ret != NULL)
38 {
39 printf("The newly allocated address %lx is already in array\n",data);
40 return NULL;
41 }
42 addr_ret = is_in_array(addr,*addr_len,0);//look for empty slot.
43 if(addr_ret == NULL)
44 {
45 (*addr_len)++;
46 addr_new = (uint64_t*)realloc(addr,(*addr_len)*sizeof(uint64_t));
47 if(addr_new == NULL)
48 {
49 puts("Unable to reallocate main array.");
50 return NULL;
51 }
52 addr_new[*addr_len-1] = data;
53 }
54 else
55 {
56 addr_new = addr;
57 *addr_ret = data;
58 }
59
60 return addr_new;
61 }
62
63 //one is error.
64 uint8_t replace_in_array(uint64_t* addr, uint16_t addr_len, uint64_t New, uint64_t old)
65 {
66 uint64_t* addr_tmp;
67
68 addr_tmp = is_in_array(addr,addr_len,old);
69 if(addr_tmp == NULL)//not in array
70 {
71 puts("old address is not allocated.");
72 return 1;
73 }
74 *addr_tmp = New;
75 return 0;
76 }
77
78 //one is the error code.
79 uint8_t del_from_array(uint64_t* addr, uint16_t addr_len, uint64_t data)
80 {
81 uint64_t* addr_ret;
82
83 addr_ret = is_in_array(addr,addr_len,data);
84 if(addr_ret == NULL)
85 {
86 puts("The address freed was not allocated.");
87 return 1;
88 }
89 *addr_ret = 0;
90
91 return 0;
92 }
93
94 void print_array(uint64_t* addr,uint16_t addr_len)
95 {
96 uint64_t* addr_end;
97
98 addr_end = addr + addr_len;
99 for(;addr<addr_end;addr++)
100 {
101 printf("%p contains %lx\n",addr,*addr);
102 }
103 return;
104 }
105
106 //this function counts how many lines are in
107 //the file and the maximum number of character
108 //per line
109 //error code if zero
110 uint32_t line_counter(FILE* fp,uint8_t* char_count_max)
111 {
112 uint16_t line_count;
113 uint16_t char_read,ii;
114 uint16_t char_count;
115 char *buf;
116
117 buf = calloc(4096,sizeof(char));
118 if(buf == NULL)
119 {
120 puts("Unable to allocate memory.");
121 return 0;
122 }
123 line_count = 0;
124 char_read = 0;
125 char_count = 0;
126 *char_count_max = 0;
127 ii=0;
128 do{
129 //if buffer is exhausted.
130 if(ii == char_read)
131 {
132 char_read = fread(buf,1,4096,fp);
133 if(char_read == 0) goto END;
134 ii=0;
135 }
136 if((buf[ii]<43) || (buf[ii]>122))
137 {
138 line_count++;
139 if(char_count > *char_count_max)
140 {
141 if(char_count > UINT8_MAX)
142 {
143 line_count= 0;
144 goto END;
145 }
146 *char_count_max = (uint8_t)char_count;
147 }
148 char_count=0;
149 }
150 else char_count++;
151 ii++;
152 }while(line_count!=UINT32_MAX);
153
154
155 END:
156 free(buf);
157 rewind(fp);
158 return line_count;
159 }
160
161 int main(void)
162 {
163 FILE* fp;
164 uint64_t* addr;
165 uint64_t* tmp;
166 uint64_t pointer0,pointer1;
167 uint16_t line_count,addr_len,ii;
168 uint8_t char_line_max;
169 char op;
170
171
172 fp = fopen("mem_check.log","r");
173 if(fp==NULL)
174 {
175 puts("Unable to open mem_check.log");
176 return -1;
177 }
178
179 line_count = line_counter(fp,&char_line_max);
180 if(line_count == 0)
181 {
182 puts("There was an error with the file format.");
183 fclose(fp);
184 return -1;
185 }
186
187 addr_len = (line_count>>2)+ 1;//divide by four and add 1.
188 addr = (uint64_t*)calloc(sizeof(uint64_t),addr_len);
189 if(addr == NULL)
190 {
191 puts("Unable to allocate memory");
192 fclose(fp);
193 return -1;
194 }
195
196 ii = 0;
197 do{
198
199 if(fscanf(fp,"%c%lx,%lx\n",&op,&pointer0,&pointer1) == EOF) break;
200 //printf("%c%lx,%lx\n",op,pointer0,pointer1);
201 if(op == 'a')
202 {
203 addr = add_to_array(addr,&addr_len,pointer0);
204 if(addr == NULL)
205 {
206 ERROR:
207 fclose(fp);
208 return -1;
209 }
210 //print_array(addr,addr_len);
211 //putchar('\n');
212 }
213 else if(op == 'f')
214 {
215 if(del_from_array(addr,addr_len,pointer0))
216 {
217 free(addr);
218 goto ERROR;
219 }
220 //print_array(addr,addr_len);
221 //putchar('\n');
222 }
223 else if(op == 'r')
224 {
225 if(replace_in_array(addr,addr_len,pointer1,pointer0))
226 {
227 free(addr);
228 goto ERROR;
229 }
230 //print_array(addr,addr_len)
231 }
232 else if(op == '\n') break;
233 else
234 {
235 printf("corrupted mem.log: op = %d\n",op);
236 free(addr);
237 goto ERROR;
238 }
239 }while(ii != line_count);
240
241 tmp = is_array_empty(addr,addr_len);
242 if(tmp == NULL) puts("There are no detected memory leaks");
243 else printf("%p not freed\n",tmp);
244
245 free(addr);
246 fclose(fp);
247 return 0;
248 }
File mem_check.h added (mode: 100644) (index 0000000..8173af2)
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #ifndef MEMORY_CHECK
6 #define MEMORY_CHECK 1
7 #endif
8
9 #if(MEMORY_CHECK==1)
10 FILE* fp_mem_log;
11 #endif
12
13 #define allocate_memory(data,size) #if(MEMORY_CHECK==1)\
14 data = malloc(size);
15 fprintf(fp_mem_log,"a%p,0x0\n",(void*)data)
16 #else\
17 data = malloc(size);\
18 #endif
19
20 #define realloc_memory(data,size) #if(MEMORY_CHECK==1)\
21 fprintf(fp_mem_log,"r%p,",(void*)data);\
22 data = realloc(data,size);\
23 fprintf(fp_mem_log,"%p\n",(void*)data);\
24 #else
25 data = realloc(data,size);
26 #endif
27
28
29 #define free_memory(data) #if(MEMORY_CHECK==1)\
30 fprintf(fp_mem_log,"f%p,0x0\n",(void*)data);\
31 #endif\
32 free(data)
33
34 #define init_check_mem() #if(MEMORY_CHECK==1)\
35 fp_mem_log = fopen("check_mem.log","w");
36 if(fp_mem_log == NULL){\
37 printf("Unable to open log file. errno: %d\n",errno);
38 return -1;\
39 }\
40 #endif
41
42 #define close_check_mem() #if(MEMORY_CHECK==1)\
43 fclose(fp_mem_log)
44 #endif
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/ORpKTKoVQnFhs/Memory_Check

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/ORpKTKoVQnFhs/Memory_Check

Clone this repository using git:
git clone git://git.rocketgit.com/user/ORpKTKoVQnFhs/Memory_Check

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main