learning dragonfly or C...

George Georgalis george at galis.org
Wed Nov 10 23:00:54 PST 2004


A couple days ago some code in a patch caught my eye and I decided
to take a closer look. The patch itself might be more interesting
than the result because it simplifies a function.

Let me set the context a bit, I know very little C but most of Bash,
my second language after BASIC was Pascal. I didn't do anything
major in Pascal but it left me with an impression of a 'right' way
to do things and what advanced concepts looked like.

Now it seems if I don't jump into C quickly, the separation between
my understanding of dragonfly applications and the underlying code
will only grow. So here goes, incomplete answers are better than no
answers so if you can point me in the right direction, that would be
appreciated.


When jumping into a block of code that has functions and
declarations, what is an efficient way to to find those definition
amidst all the includes, paths and Makefiles of the whole tree,
without completely losing your place? (eg I'd like to reference
O_RDONLY and fp_close)


The code that caught my eye is the in the top of the first diff in
http://leaf.dragonflybsd.org/~dillon/vfsx22.patch

In checkpt.c, this:

static int
mmap_vp(struct vn_hdr *vnh)
{
        struct vnode **vpp, *vp;
        Elf_Phdr *phdr;
        struct file *fp;
        int error;
        TRACE_ENTER;
        vpp = &vp;

        phdr = &vnh->vnh_phdr;

        if ((error = ckpt_fhtovp(&vnh->vnh_fh, vpp)) != 0)
                return error;
        /*
         * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
         */
        if ((error = fp_vpopen(*vpp, O_RDONLY, &fp)) != 0)
                return error;
        error = mmap_phdr(fp, phdr);
        fp_close(fp);
        TRACE_EXIT;
        return error;
}



becomes



static int
mmap_vp(struct vn_hdr *vnh)
{
        struct vnode *vp;
        Elf_Phdr *phdr;
        struct file *fp;
        int error;
        TRACE_ENTER;

        phdr = &vnh->vnh_phdr;

        if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0)
                return error;
        /*
         * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
         */
        if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) {
                vput(vp);
                return error;
        }
        error = mmap_phdr(fp, phdr);
        fp_close(fp);
        TRACE_EXIT;
        return error;
}



This is the part of the diff that interests me:

-	struct vnode **vpp, *vp;
+	struct vnode *vp;
 	Elf_Phdr *phdr;
 	struct file *fp;
 	int error;
 	TRACE_ENTER;
-	vpp = &vp;


I have a general idea about the use of pointers, but don't really
understand how they are used. What exactly does this syntax mean?

        struct vnode **vpp, *vp;
        vpp = &vp;

The other changes explain it a little to me: a degree of pointer
reference has been removed and the function is basicly a derivative
of the former. Is that what's happening?  Any other comments on what
that block of code does, or tries to do, are more than welcome.

// George


-- 
George Georgalis, systems architect, administrator Linux BSD IXOYE
http://galis.org/george/ cell:646-331-2027 mailto:george at xxxxxxxxx





More information about the Kernel mailing list