9

I keep running across little conventions like __KERNEL__.

Are the __ in this case a naming convention used by kernel developers or is it a syntax specific reason for naming a macro this way?

There are many examples of this throughout the code.

For example some functions and variables begin with an _ or even __.

Is there a specific reason for this?

It seems pretty widely used and I just need some clarification as to whether these things have a syntactical purpose or is it simply a naming convention.

Furthermore I see lots of user declared types such as uid_t. Again I assume this is a naming convention telling the reader that it is a user-defined type?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
A.Smith
  • 427
  • 5
  • 14

2 Answers2

7

There are several cases:

  • In public facing headers, i.e. anything that libc will be taking over and putting under /usr/include/linux, the standards specify which symbols should be defined and any other symbols specific to the system shall start with underscore and capital letter or two underscores. That's the reason for __KERNEL__ in particular, because it is used in headers that are included both in kernel and in libc and some declarations are different.
  • In internal code, the convention usually is that symbol __something is workhorse for something excluding some management, often locking. That is a reason for things like __d_lookup . Similar convention for system calls is that sys_something is the system call entry point that handles context switch to and from kernel and calls do_something to do the actual work.
  • The _t suffix is standard library convention for typedefs. E.g. size_t, ptrdiff_t, foff_t and such. Kernel code follows this convention for it's internal types too.
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • 1
    Thanks for your answer. With all of the coding style documentation about the kernel, I'm surprised that I don't find a lot of documentation on the naming conventions in the kernel. Did you just pick up these things by doing kernel hacking? – KarateSnowMachine Nov 12 '13 at 22:19
  • 1
    @KarateSnowMachine: Well, the first and last point come from C standard. The middle point, mostly yes. I didn't do any serious kernel hacking, but I had an assignment in kernel back at school, so I had to read quite a bit of the code (especially to double-check the locking). – Jan Hudec Nov 13 '13 at 07:23
0

There are several __functions, as i.e. __alloc_pages_nodemask() that seems also be exported. Also, __functions are static, static inline, or globals too. Btw, observing __alloc_pages_nodemask() is called only from mm code, and in no other place, such functions may be meant "internal" of some kernel framekwork.