In Linux kernel code, object-oriented design concepts are used in many places. For example, in the file system design, there are four main objects: Super blocks, index nodes, directory items and files. In each object-defined struct, there is a struct pointing to object operations. All fields in this struct are pointers to functions, such as the thumbnail definition in the file structure:
Struct File
{
Struct file_operations * f_op;/* pointer to the file operation table */
Mode_t f_mode;/* Open Mode of the file */
Loff_t f_pos;/* current location of the file */
...
};
A group of functions for file operations are called file operation tables, which are described by the file_operations structure:
Struct file_operations {
Loff_t (* llseek) (struct file *, loff_t, INT );
Ssize_t (* read) (struct file *, char *, size_t, loff_t *);
Ssize_t (* write) (struct file *, const char *, size_t, loff_t *);
INT (* MMAP) (struct file *, struct vm_area_struct *);
INT (* open) (struct inode *, struct file *);
INT (* flush) (struct file *);
INT (* release) (struct inode *, struct file *);
INT (* fsync) (struct file *, struct dentry *, int datasync );
...
};
I always thought that this definition was taken for granted. One day, some people asked why this definition was made and where the source was? I just told the students to understand the object-oriented thinking, and then the data structure here is easy to understand. In fact, if an object-oriented idea is expressed in a structured language, it is difficult to understand it. On the IBM developerworks website, I saw the article "Tips: Program polymorphism in C Language", which illustrates the origins of this definition in visual language:
About polymorphism, about C
The word polymorphism originally originated from the Greek polumorphos, meaning that it has many forms or forms. In the field of program design, a widely accepted definition is "an ability to associate different special behaviors with a single generalized mark ". However, in people's intuitive perception, polymorphism is equivalent to "the same method can make correct processing for different types of input parameters, and give people the expected results. "Perhaps this reflects people's expectations for the effect of polymorphism: make the program more intelligent and easy to use, designers are more and more able to see the essence of the problem that the code is going to reach through a variety of appearances.
As a reader, you may have profound insights on Object-Oriented Programming, or you may have a deep understanding of the convenience and magic of polymorphism. At this time, you began to question the difference: "polymorphism, that is only the technology of object-oriented programming. C language is process-oriented !" What I want to talk about is that C, as a programming language, may not be designed for Object-Oriented Programming, but it does not mean that it cannot implement the functions that object-oriented programming can implement, for example, polymorphism.
In this article, we use a simple single-chain table as an example to demonstrate how c Represents polymorphism.
Struct: a story to be told
Many programmers who begin to write C code and gradually move to C ++ know that the class in C ++ is actually the structure in C language. Many books that introduce C ++ Based on the C language background will clearly show you when introducing the class chapter, how does the structure in a C language gradually become a typical C ++ class? Finally, I come to the conclusion that "structure is a class that all members are public." Of course, class is still a class, and it cannot be simply regarded as a complicated structure.
Let's take a look at how a single-chain table node in C defines a simple storage of integer data, and of course it uses a struct. Most people will define in the linklist. h file like me:
For details, see: http://www.ibm.com/developerworks/cn/linux/l-cn-cpolym/
-----------------------------------------------------------------------------
Note: This thinking process provided in this article will undoubtedly play a guiding role in how to combine the object-oriented thinking with the structured language. Although this article shows how to use C to realize the polymorphism of the linked list. But the kernel code list. the typical Implementation of linked lists in H (see my previous blog post) is not in the form of polymorphism, but its abstraction and the value of flexible application can be reflected after analysis and practice.