Datasheet

Mauerer runc01.tex V2 - 09/04/2008 4:13pm Page 23
Chapter 1: Introduction and Overview
It is essential that
kobject
s are not linked with other data structures by means of
pointers but are directly embedded. Managing the kernel object itself amounts to
managing the whole containing object this way. Since
struct kobject
is embedded
into many data structures of the kernel, the developers take care to keep it small.
Adding a single new element to this data structure results in a size increase of many
other data structures. Embedded kernel objects look as follows:
struct sample {
...
struct kobject kobj;
...
};
The meanings of the individual elements of
struct kobject
are as follows:
k_name
is a text name exported to userspace using
sysfs
. Sysfs is a virtual filesystem that allows
for exporting various properties of the system into userspace. Likewise
sd
supports this connec-
tion, and I will come back to this in Chapter 10.
kref
holds the general type
struct kref
designed to simplify reference management. I discuss
this below.
entry
is a standard list element used to group several
kobject
s in a list (known as a set in this
case).
kset
is required when an object is grouped with other objects in a set.
parent
is a pointer to the parent element and enables a hierarchical structure to be established
between
kobject
s.
ktype
provides more detailed information on the data structure in which a
kobject
is
embedded. Of greatest importance is the destructor function that returns the resources of the
embedding data structure.
The similarity between the name
kobject
and the object concept of, well, object-oriented languages
like C++ or Java is by no means coincidental: The
kobject
abstraction indeed allows for using object-
oriented techniques in the kernel, but without requiring all the extra mechanics (and bloat, and overhead)
of C++.
Table 1-1 lists the standard operations provided by the kernel to manipulate
kobject
instances, and
therefore effectively act on the embedding structure.
The layout of the
kref
structure used to manage references is as follows:
<kref.h>
struct kref {
atomic_t refcount;
};
refcount
is an atomic data type to specify the number of positions in the kernel at which an object is
currently being used. When the counter reaches 0, the object is no longer needed and can therefore be
removed from memory.
23