Re: [Yaffs] opaque

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Charles Manning
Date:  
To: yaffs
Subject: Re: [Yaffs] opaque
On Thursday 21 September 2006 17:42, Johann Kok (JP) wrote:
> Hi,
> 1) what is the purpose of the line "typedef struct __opaque yaffs_DIR"
> - more specifically what does the opaque keyword do? I'm not trying
> to turn this mailing list into a tutorial for C, but in my 7 years or
> so of C programming I've never encountered the __opaque keyword.
> I cannot find much info on it on the web or in C/C++ books.


Sorry I think I've fooled you by using an arbittrary name that looks like a
typical attribute.

You could change __opaque to any other valid C name and it would work.

eg.

    typedef struct dit_maak_nie_saak_nie  yaffs_DIR;


The purpose of doing this is to have an opaque pointer. What is that? It is a
bit like a void pointer, but is typed. We now havea typedef for a yaffs_DIR
that we can specify as a type pointer in the function definition, but we have
not exposed the true type of the pointer to the application.

Where the pointer is actually used we cast it to an internal type:
struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
{
    yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
......
}


Consider what would happen if we did not have an opaque pointer and had the
actual struct exposed to the application. Some "clever" programmer would
start to dereference the pointer.


>
> 2) I get the following warning at the end of this typedef:
> "yaffsfs.h:153: warning: useless keyword or type name in empty
> declaration"
> typedef struct yaffs_dirent
> {
>     long d_ino;                 /* inode number */
>     off_t d_off;                /* offset to this dirent */
>     unsigned short d_reclen;    /* length of this d_name */
>     char d_name [NAME_MAX+1];   /* file name (null-terminated) */
> };

>
> I cannot recall that I got this warning before but now I also cannot
> see what is causing it.


What conmpiler are you using?

Sometimes d_name and some similar things in C lib are implemented as macros
and when these are unwound thedefinitions can get messed up.

I had to change a whole lot of st_time and friends to yst_time in yaffs_guts
to get around a problem like this.

-- Charles