Reading BSD system code

I

istillshine

In string.h, I saw

__BEGIN_DECLS

....

__END_DECLS

What's the use of these labels? And why did they always put two
underscores before a name?

I also saw a function prototype declaration:

strlen(const char *) __pure;

Why adding __pure?
 
I

Ian Collins

In string.h, I saw

__BEGIN_DECLS

....

__END_DECLS

What's the use of these labels? And why did they always put two
underscores before a name?

I also saw a function prototype declaration:

strlen(const char *) __pure;

Why adding __pure?
Try searching for the definition of these symbols in the code, they will
probably be documented.

Alternatively as none of these are standard C, try as BSD group.
 
W

Walter Roberson

In string.h, I saw

What's the use of these labels?

I don't know; they aren't part of standard C.
And why did they always put two
underscores before a name?

Because two underscores indicates a macro or identifier whose name
is reserved for use by the implementation. If they had not put the
two underscores in (and had also not used one of the reserved
formats that starts with a single underscore) then they would have
risked using an identifier defined by the user.

I also saw a function prototype declaration:
strlen(const char *) __pure;
Why adding __pure?

Also not part of standard C. We could speculate, though, that to
that particular implementation, it means that the routine has no
side effects.
 
A

Antoninus Twink

I don't know; they aren't part of standard C.

I think a common use for macros like this is to facilitate sharing
headers between C and C++ by defining them something like this:

#ifdef __cplusplus
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif

The OP could check whether this is true in his case by grepping for
something like "#define *__BEGIN_DECLS" in his BSD headers.
Also not part of standard C. We could speculate, though, that to
that particular implementation, it means that the routine has no
side effects.

Specifically, this informs the compiler that it can optimize away calls
to strlen. If there's also
my_do_all_function(const char *filename);
and I have code like

char *s=get_input();
strlen(s); /* discard the return value */
my_do_all_function(s); /* discard the return value */

then the fact that strlen is pure tells the compiler it doesn't need to
include code for the strlen call, whereas it can't just get rid of the
call to my_do_all_function because it might have side effects.

You can also add __pure to your own function definitions, but for
maximum portability (other compilers may have a different syntax, or not
support it at all) use preprocessor macros like:

#ifdef __foo_bar_compiler
#define PURE __pure
#else
#define PURE
#endif

int simple_count(const char **s, size_t n) PURE
{
/* function with no side_effects */
}
 
R

Richard Tobin

In string.h, I saw

__BEGIN_DECLS

...

__END_DECLS

What's the use of these labels?

They are macros. If you look at their definitions (which is in the
file included just beforehand, sys/cdefs.h) you'll see that they
expand either to nothing, or if __cplusplus is defined, to 'extern "C"
{' and '}'. That is, they're wrapping the declarations in something
suitable for inclusion in C++ programs.
strlen(const char *) __pure;

Why adding __pure?

It relates to a gcc extension and is defined differently depending on
the version of gcc used. Again, see sys/cdefs.h for the definition.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top