VxWorks source code analysis-Object
When I saw the source code of VxWorks for the first time, I realized how to use the C language to implement object-oriented functions, however, there is always no motivation to understand the actual principle. This idle time, we can see some clues only when we get the source code of VxWorks.
It is actually quite difficult to implement object-oriented in C language. After all, C language is a pure process-oriented language. process-oriented and object-oriented are two completely different ideas, but at least they have little in common. However, the two inherent features of C language have made it an object-oriented function, namely struct and pointer. If you are familiar with object-oriented users, you should know the similarities and differences between classes and struct. The two are actually very similar. You can even say that classes are derived from struct, with struct, implementing object-oriented is not a blank phrase. As we all know, classes are composed of attributes and methods. The attribute is equivalent to the struct field, and the method can be implemented using the function pointer in C language. In this way, the class is formed.
For example:
Typedef struct obj_class/* obj_class */
{
Obj_core objcore;/* object core of Class Object */
Struct mem_part * objpartid;/* Memory partition to allocate from */
Unsigned objsize;/* object size */
Unsigned objalloccnt;/* Number of allocated objects */
Unsigned objfreecnt;/* Number of released objects */
Unsigned objinitcnt;/* Number of initialized objects */
Unsigned objterminatecnt;/* Number of terminated objects */
Int coreoffset;/* offset from object start to objcore */
Funcptr creatertn;/* object creation routine */
Funcptr initrtn;/* object initialization routine */
Funcptr destroyrtn;/* object destroy routine */
Funcptr showrtn;/* object show routine */
Funcptr implements tn;/* object inst routine */
} Obj_class;
The above struct contains eight members and five function pointers, which are equivalent to eight attributes and five methods of the class. to gain a deeper understanding of obj_class, it is necessary to talk about the types of each member, first, let's take a look at obj_core. This type is also a struct. It only contains a field of the obj_class pointer type:
Typedef struct obj_core/* obj_core */
{
Struct obj_class * pobjclass;/* pointer to object's class */
} Obj_core;
I believe most people have already guessed the role of obj_core. It doesn't matter if you haven't guessed it. After seeing the initialization function, I believe everyone understands its role. Next is objpartid, which is a pointer to the mem_part object. Because the mem_part struct is very complex and will be analyzed later, this article will not be analyzed for the time being. You only need to know that this is a struct. The last noteworthy type is funcptr. Friends who have used VxWorks programming will certainly be familiar with this type, but take care of everyone and define it as follows:
Typedef int (* funcptr) ();/* PTR to function returning int */
After describing the definition of obj_core, let's look at several functions related to this struct. There are not many functions, mainly object creation and object destruction functions, similar to constructor and destructor in C ++, although object creation and destruction are implemented by calling the function pointer defined in the obj_core struct, it can also be seen that, after all, the C language is not an object-oriented language. To fully implement the Object-Oriented function, it is still very complicated and requires some helper classes. After talking for a long time, what are these functions? Four functions: classlibinit, classcreate, classinit, and classdestroy (not implemented at the moment). There are also some tool-class functions that will not be described here. The relationships between the above four functions (actually three functions) can be used to represent:
The classlibinit function is equivalent to the class and used to initialize a tool class object. The classcreate function is a function that actually creates a specific object. The work completed mainly consists of two parts: memory Allocation and member initialization. Memory Allocation is implemented by the objalloc function, while member Initialization is implemented by the classinit function. The classinit function is a specific member initialization function. Its responsibilities are very simple. It is sufficient to assign the provided parameters to all the members of obj_class. The role of obj_core has been mentioned above. Here we will describe it in detail. Before the description, let's take a look at the following code:
Status classinit
(
Obj_class * pobjclass,/* pointer to object class to initialize */
Unsigned objectsize,/* size of object */
Int coreoffset,/* offset from objcore to object start */
Funcptr creatertn,/* object creation routine */
Funcptr initrtn,/* object initialization routine */
Funcptr destroyrtn/* object destroy routine */
)
{
/* Default memory partition is system partition */
Omitted
/* Initialize object Methods */
Omitted
/* Initialize class as valid object */
Objcoreinit (& pobjclass-> objcore, classclassid );
Return (OK );
}
Void objcoreinit
(
Obj_core * pobjcore,/* pointer to object core to initialize */
Obj_class * pobjclass/* pointer to object class */
)
{
Pobjcore-> pobjclass = pobjclass;/* validate object */
Pobjclass-> objinitcnt ++;/* keep track of object count */
}
After reading the above Code, most people will be confused, because there is a variable that has not been introduced, this is the global variable classclassid:
/* Locals */
Local obj_class classclass;
/* Globals */
Class_id classclassid = & classclass;
From the above classinit function, we can see that as long as the object is created through classcreate, its objcore value all points to the same variable, why is this implementation required? The purpose of this implementation is to facilitate the implementation of Class Identifiers. Anyone familiar with object-oriented knowledge knows that it is too easy to judge whether two objects are of the same class. You can use =, equals, or define a compare method, you can also use the getclass method, but the C language only compares the address values. Therefore, it is more difficult to implement the class identification. Therefore, the global variable classclass is defined here. As long as the objcore and classclass of an object (obj_class variable) are equal, its type is obj_class (in VxWorks, there are many similar cases, for example, the fpclass that identifies a file also works ):
# Define obj_verify (objid, classid )/
(/
(/
(Obj_id) (objid)-> pobjclass = (struct obj_class *) (classid) |/
(/
(Obj_id) (objid)-> pobjclass &&/
(Obj_id) (objid)-> pobjclass =/
(Class_id) (struct obj_class *) (classid)-> initrtn ))/
)/
)/
? /
OK/
:/
(Errno = s_objlib_obj_id_error, error )/
)
The obj_class struct is the most basic part of all other VxWorks structs. To understand the implementation principle of VxWorks, this struct and the memory allocation mechanism must be understood. The VxWorks code is very delicate. I have limited skill and can only analyze it to this point. However, I believe that as long as I keep working hard, I will understand its essence sooner or later.
0 0
0
(Please comment on the article)