In a purpose to achieve a school project, I must recode malloc and free functions using only mmap and munmap. I must also compile those functions in a dynamic library (.so file) and use this during the run time.
I am working under mac OS Sierra 10.12.6.
Here is my problem, when I run my very simple program without any malloc call, but using the dynamic library, I can notice some malloc call causing unwanted pages reclaim. The test program I am talking about :
int main()
{
int i;
char *addr;
addr = 0;
i = 0;
while (i < 1024) {
i++;
}
return (0);
}
I am trying to learn how to use debugging tools such as strace, but right now I can notice the malloc call simply using a printf inside my own sources. Indeed, when I run the test, the printf is called while no malloc is used. Here is the commands I am using to compile and run everything :
gcc -shared srcs... -o lib_malloc.so
gcc test.c -o test -L ./ -lmalloc
./run.sh ./test
(sources files are already compiled as objects files with -Wall -Wextra -Werror flags before linking at the first line)
Here is run.sh file :
#!/bin/sh
export DYLD_LIBRARY_PATH=.
export DYLD_INSERT_LIBRARIES="lib_malloc.so"
export DYLD_FORCE_FLAT_NAMESPACE=1
$@
I have written a simple memory print function as well. Its purpose is to print every allocated block by my own malloc calling my linked lists and printing it... If I add it to the end of my test.c code, I can see some blocks, like this :
TINY : 0x106f65000
0x106f65052 - 0x106f65072 : 4 octets
0x106f6509c - 0x106f650bc : 4 octets
0x106f650e6 - 0x106f65106 : 4 octets
0x106f65130 - 0x106f6513f : 1 octets
0x106f65169 - 0x106f65196 : 5 octets
0x106f651c0 - 0x106f651fa : 7 octets
0x106f65224 - 0x106f65251 : 5 octets
0x106f6527b - 0x106f652ad : 6 octets
0x106f652d7 - 0x106f65306 : 5 octets
0x106f65330 - 0x106f6533e : 1 octets
0x106f65368 - 0x106f653a8 : 8 octets
0x106f653d2 - 0x106f65403 : 6 octets
0x106f6542d - 0x106f65470 : 8 octets
0x106f6549a - 0x106f654ce : 6 octets
0x106f654f8 - 0x106f6552e : 6 octets
0x106f65558 - 0x106f65564 : 1 octets
0x106f6558e - 0x106f655bb : 5 octets
0x106f655e5 - 0x106f6564b : 12 octets
0x106f65675 - 0x106f65685 : 2 octets
0x106f656af - 0x106f656ef : 8 octets
0x106f65719 - 0x106f65727 : 1 octets
0x106f65751 - 0x106f65851 : 32 octets
We can notice that only "tiny" unwanted spaces have been allocated anyway ... I might have done a stupid error somewhere or misunderstand something , if someone understand what's happening, it will rescue me so much ! I am searching a solution since days, all my source code for those functions is finish. I can share more code if needed. Help please !
I apologize for my english, I am currently practicing , thank you :)