cqemuxv6kmalloc

How to correctly implement kmalloc using C?


I have implemented kmalloc in the Makefile, defs.h, kmalloc.c, sysproc.c, usys.S, syscall.h, and syscall.c. I have a test case called test_1.c to test my implementation of kmalloc. I took the source code from xv6, I applied my implementations and changes, then run it on qemu.

I execute ./test-mmap.sh to know if I pass the test case. Turns out I didn't pass, I got error : "test_1.c: error: implicit declaration function of kmalloc". But I have implemented the kmalloc correctly and in the correct files. I am confuse, what am I missing here?


Solution

  • When compiling test.c the compiler (pre-processor) includes these files:

    #include "param.h"
    #include "types.h"
    #include "stat.h"
    #include "user.h"
    #include "fs.h"
    #include "fcntl.h"
    #include "syscall.h"
    #include "traps.h"
    #include "memlayout.h"
    

    None of those files have an explicit declaration for kmalloc(), so the compiler complains about an implicit declaration of kmalloc() when it sees it at line 18.

    There is an explicit declaration for kmalloc() in defs.h (at line 81), but that file isn't included by test.c so the compiler has no idea it exists.

    To solve this problem, either add #include "defs.h" at the top of test1.c or in something that is already included by test1.c (e.g. maybe at the top of syscall.h); or add an explicit declaration ("void* kmalloc(uint);") at the top of test1.c or in something that is already included by test.c.

    Note that depending on how you solve the problem you may (or may not) end up with an "implicit declaration of kfree()" problem next; which can be solved in the same way.