Reputation: 387
unix 6th source code. Very old.
conf.h
struct bdevsw {
int (*d_open)();
int (*d_close)();
int (*d_strategy)();
int *d_tab;
} bdevsw[];
conf.c
int (*bdevsw[])(){
&nulldev, &nulldev, &rkstrategy, &rktab,
&nodev, &nodev, &nodev, 0,
0
}
My question why not the initialization just reads,
bdevsw[] = {......}
Upvotes: 1
Views: 122
Reputation: 882566
All the information gathered for this answer is from my trusted copy of the Lions' Commentary book. It's a good resource for the very early UNIX code.
If you pump that monstrosity of a declaration into cdecl, you'll see that its purpose is to:
declare
bdevsw
as array of pointer to function returningint
.
Hence it's not code at all(a), rather it's the array definition for the functions, one per device, pretty much the same as your suggestion would be.
The reason it's not in the header file is probably for the following reasons.
First, the conf.c
file is actually auto-generated by mkconf
, as that's the file that contains the device details (block and character devices, held in bdevsw
and cdevsw
) for a given UNIX system.
As an autogenerated file, it makes sense to break apart the declarations of the data types (which are consistent across different systems) and the definitions of the arrays (which do change per system). The comment at the top of this file state that it, and low.s
, are the result of mkconf
.
Second, there are quite a few C files that include conf.h
. For example, bio.c
(block I/O), sys3.c
(filesystem calls), fio.c
(file calls), and alloc.c
(very early initialisation to read the root super block).
So, if the array was defined in the header file (presumably as static
to prevent double definition), each source file would basically have it's own copy, wasting precious space. By defining it in conf.c
, there's one copy shared amongst everyone.
(a) Your comment that:
the second parenthesis in the initialization stmt makes it look like a function
is understandable and, in this case, it does represent a function call. But only insofar that it's an array of pointers to functions, not an actual function definition.
Upvotes: 2