ºìÁªLinuxÃÅ»§
Linux°ïÖú

FreeBSDϵͳÄں˶ÔÏó

·¢²¼Ê±¼ä:2006-04-01 00:41:46À´Ô´:ºìÁª×÷Õß:Î÷Î÷
Äں˶ÔÏó£¬Ò²¾ÍÊÇKobj£¬Îª²Ù×÷ϵͳÄÚºËÌṩÁËÒ»ÖÖÃæÏò¶ÔÏóµÄCÓïÑÔ±à³Ì·½Ê½¡£±»²Ù×÷µÄÊý¾ÝÒ²³ÐÔزÙ×÷ËüµÄ·½·¨¡£ÕâʹµÃÔÚ²»ÆÆ»µ¶þ½øÖƼæÈÝÐÔµÄÇ°ÌáÏ£¬Ä³Ò»¸ö½Ó¿ÚÄܹ»Ôö/¼õÏàÓ¦µÄ²Ù×÷¡£Kobj¹¤×÷ʱ£¬²úÉú·½·¨µÄÃèÊö¡£Ã¿¸öÃèÊöÓÐÒ»¸öΨһµÄ±êʶºÍÒ»¸öȱʡº¯Êý¡£Ä³¸öÃèÊöµÄµØÖ·±»ÓÃÀ´ÔÚÒ»¸öÀàµÄ·½·¨±íÀïΨһµÄ±êʶ·½·¨¡£ ¹¹½¨Ò»¸öÀ࣬¾ÍÊÇÒª½¨Á¢Ò»ÕÅ·½·¨±í£¬²¢½«ÕâÕűí¹ØÁªµ½Ò»¸ö»ò¶à¸öº¯Êý(·½·¨)£»ÕâЩº¯Êý(·½·¨)¶¼´øÓз½·¨ÃèÊö¡£Ê¹ÓÃÇ°£¬ÀàÒª±»±àÒë¡£±àÒëʱҪΪÕâ¸öÀà·ÖÅäһЩ»º´æ¡£ÔÚ·½·¨±íÖеÄÿ¸ö·½·¨ÃèÊö¶¼»á±»Ö¸ÅÉÒ»¸öΨһµÄ±êʶ£¬³ý·ÇÒѾ­±»ÆäËüÒýÓÃËüµÄÀàÔÚ±àÒëʱָÅÉÁ˱êʶ¡£¶ÔÓÚÿ¸ö½«Òª±»Ê¹Óõķ½·¨£¬¶¼»áÓɽű¾Éú³ÉÒ»¸öº¯Êý(·½·¨²éÕÒº¯Êý)£¬ÒÔ½âÎöÍâÀ´²ÎÊý£¬²¢ÔÚ±»²éѯʱ¸ø³ö·½·¨ÃèÊöµÄµØÖ·¡£±»Éú³ÉµÄº¯Êý(·½·¨²éÕÒº¯Êý)ƾ×ÅÄǸö·½·¨ÃèÊöµÄΨһ±êʶ°´HashµÄ·½·¨²éÕÒ¶ÔÏóµÄÀàµÄ»º´æ¡£Èç¹ûÕâ¸ö·½·¨²»ÔÚ»º´æÖУ¬º¯Êý»á²éÕÒʹÓÃÀàµÄ·½·¨±í¡£Èç¹ûÕâ¸ö·½·¨±»ÕÒµ½ÁË£¬ÀàÀïµÄÏà¹Øº¯Êý(Ò²¾ÍÊÇij¸ö·½·¨µÄʵÏÖ´úÂë)¾Í»á±»Ê¹Ó᣷ñÔò£¬Õâ¸ö·½·¨ÃèÊöµÄȱʡº¯Êý½«±»Ê¹ÓᣠÕâЩ¹ý³Ì¿É±»±íʾÈçÏ£º ¶ÔÏó->»º´æ<->Àà

ÈçºÎʹÓÃKobj
½á¹¹
struct kobj_method

º¯Êý
void kobj_class_compile(kobj_class_t cls);
void kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops);
void kobj_class_free(kobj_class_t cls);
kobj_t kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags);
void kobj_init(kobj_t obj, kobj_class_t cls);
void kobj_delete(kobj_t obj, struct malloc_type *mtype);

ºê
KOBJ_CLASS_FIELDS
KOBJ_FIELDS
DEFINE_CLASS(name, methods, size)
KOBJMETHOD(NAME, FUNC)

Í·Îļþ
½¨Á¢Ò»¸ö½Ó¿ÚµÄÄ£°å

ʹÓÃKobjµÄµÚÒ»²½Êǽ¨Á¢Ò»¸ö½Ó¿Ú¡£½¨Á¢½Ó¿Ú°üÀ¨½¨Á¢Ä£°åµÄ¹¤×÷¡£½¨Á¢Ä£°å¿ÉÓýű¾src/sys/kern/makeobjops.plÍê³É£¬Ëü»á²úÉúÉêÃ÷·½·¨µÄÍ·ÎļþºÍ´úÂ룬½Å±¾»¹»áÉú³É·½·¨²éÕÒº¯Êý¡£ ÔÚÕâ¸öÄ£°åÖÐÈçϹؼü´Ê»á±»Ê¹ÓÃ: #include, INTERFACE, CODE, METHOD, STATICMETHOD, ºÍ DEFAULT. #includeÓï¾äµÄÕûÐÐÄÚÈݽ«±»Ò»×Ö²»²îµÄ¸´ÖƵ½±»Éú³ÉµÄ´úÂëÎļþµÄÍ·²¿¡£

ÀýÈç: #include ¹Ø¼ü´ÊINTERFACEÓÃÀ´¶¨Òå½Ó¿ÚÃû¡£Õâ¸öÃû×Ö½«Óëÿ¸ö·½·¨Ãû½ÓºÏÔÚÒ»Æð£¬ÐÎ³É [interface name]_[method name]¡£Óï·¨ÊÇ£ºINTERFACE [½Ó¿ÚÃû]; ÀýÈç: INTERFACE foo; ¹Ø¼ü´ÊCODE»á½«ËüµÄ²ÎÊýÒ»×Ö²»²îµÄ¸´ÖƵ½´úÂëÎļþÖС£Óï·¨ÊÇCODE { [ÈκδúÂë] }; ÀýÈç:
CODE {
struct foo * foo_alloc_null(struct bar *)
{
return NULL;
}
};
¹Ø¼ü´ÊMETHODÓÃÀ´ÃèÊöÒ»¸ö·½·¨¡£Óï·¨ÊÇ: METHOD [·µ»ØÖµÀàÐÍ] [·½·¨Ãû] { [¶ÔÏó [, ²ÎÊýÈô¸É]] }; ÀýÈç:
METHOD int bar {
struct object *;
struct foo *;
struct bar;
};
¹Ø¼ü´ÊDEFAULT¸úÔڹؼü´ÊMETHODÖ®ºó£¬ÊǶԹؼü´ÊMETHODµÄ²¹³ä¡£Ëü¸øÕâ¸ö·½·¨²¹³äÉÏȱʡº¯Êý¡£Óï·¨ÊÇ£º METHOD [·µ»ØÖµÀàÐÍ] [·½·¨Ãû] { [¶ÔÏó; [ÆäËü²ÎÊý]] }DEFAULT [ȱʡº¯Êý]; ÀýÈç:
METHOD int bar {
struct object *;
struct foo *;
int bar;
} DEFAULT foo_hack;
¹Ø¼ü´ÊSTATICMETHODÀàËƹؼü´ÊMETHOD¡£¶ÔÓÚÿ¸öKobj¶ÔÏó£¬Ò»°ãÆäÍ·²¿¶¼ÓÐһЩKobjרÓеÄÊý¾Ý¡£METHOD¶¨ÒåµÄ·½·¨¾Í¼ÙÉèÕâЩרÓÐÊý¾ÝλÓÚ¶ÔÏóÍ·²¿£»¼ÙÈç¶ÔÏóÍ·²¿Ã»ÓÐÕâЩרÓÐÊý¾Ý£¬ÕâЩ·½·¨¶ÔÕâ¸ö¶ÔÏóµÄ·ÃÎʾͿÉÄܳö´í¡£¶øSTATICMETHOD¶¨ÒåµÄ¶ÔÏó¿ÉÒÔ²»ÊÜÕâ¸öÏÞÖÆ£ºÕâÑùÃèÊö³öµÄ·½·¨£¬Æä²Ù×÷µÄÊý¾Ý²»ÓÉÕâ¸öÀàµÄij¸ö¶ÔÏóʵÀý¸ø³ö£¬¶øÊÇÈ«¶¼Óɵ÷ÓÃÕâ¸ö·½·¨Ê±µÄ²Ù×÷Êý(ÒëÕß×¢:¼´²ÎÊý)¸ø³ö¡£ÕâÒ²¶ÔÓÚÔÚij¸öÀàµÄ·½·¨±íÖ®Íâµ÷ÓÃÕâ¸ö·½·¨ÓÐÓá£

ÆäËüÍêÕûµÄÀý×Ó:
src/sys/kern/bus_if.m
src/sys/kern/device_if.m
½¨Á¢Ò»¸öÀà

ʹÓÃKobjµÄµÚ¶þ²½Êǽ¨Á¢Ò»¸öÀà¡£Ò»¸öÀàµÄ×éÓÐÃû×Ö¡¢·½·¨±í£»¼ÙÈçʹÓÃÁËKobjµÄ¡°¶ÔÏó¹ÜÀí¹¤¾ß¡±(Object Handling Facilities)£¬ÀàÖл¹°üº¬¶ÔÏóµÄ´óС¡£½¨Á¢ÀàʱʹÓúêDEFINE_CLASS()¡£½¨Á¢·½·¨±íʱ£¬Ð뽨Á¢Ò»¸ökobj_method_tÊý×飬ÓÃNULLÏî½áβ¡£Ã¿¸ö·ÇNULLÏî¿ÉÓúêKOBJMETHOD()½¨Á¢¡£ ÀýÈç:
DEFINE_CLASS(fooclass, foomethods, sizeof(struct foodata));

kobj_method_t foomethods[] = {
KOBJMETHOD(bar_doo, foo_doo),
KOBJMETHOD(bar_foo, foo_foo),
{ NULL, NULL}
};
ÀàÐë±»¡°±àÒ롱¡£¸ù¾Ý¸ÃÀà±»³õʼ»¯Ê±ÏµÍ³µÄ״̬£¬½«ÒªÓõ½Ò»¸ö¾²Ì¬·ÖÅäµÄ»º´æºÍ¡°²Ù×÷Êý±í¡±(ops table£¬ÒëÕß×¢£º¼´¡°²ÎÊý±í¡±)¡£ÕâЩ²Ù×÷¿Éͨ¹ýÉùÃ÷Ò»¸ö½á¹¹Ìåstruct kobj_ops²¢Ê¹ÓÃkobj_class_compile_static()£¬»òÊÇֻʹÓÃkobj_class_compile()À´Íê³É¡£

½¨Á¢Ò»¸ö¶ÔÏó

ʹÓÃKobjµÄµÚÈý²½ÊǶ¨Òå¶ÔÏó¡£Kobj¶ÔÏó½¨Á¢³ÌÐò¼Ù¶¨KobjרÓÐÊý¾ÝÔÚÒ»¸ö¶ÔÏóµÄÍ·²¿¡£Èç¹û²»ÊÇÈç´Ë£¬Ó¦µ±ÏÈ×ÔÐзÖÅä¶ÔÏó£¬ÔÙʹÓÃkobj_init()³õʼ»¯¶ÔÏóÖеÄKobjרÓÐÊý¾Ý£»Æäʵ¿ÉÒÔʹÓÃkobj_create()·ÖÅä¶ÔÏ󣬲¢×Ô¶¯³õʼ»¯¶ÔÏóÖеÄKobjרÓÐÄÚÈÝ¡£kobj_init()Ò²¿ÉÒÔÓÃÀ´¸Ä±äÒ»¸ö¶ÔÏóËùʹÓõÄÀà¡£ ½«KobjµÄÊý¾Ý¼¯³Éµ½¶ÔÏóÖÐҪʹÓúêKOBJ_FIELDS¡£ ÀýÈç:
struct foo_data {
KOBJ_FIELDS;
foo_foo;
foo_bar;
};
µ÷Ó÷½·¨

ʹÓÃKobjµÄ×îºóÒ»²¿¾ÍÊÇͨ¹ýÉú³ÉµÄº¯Êýµ÷ÓöÔÏóÀàÖеķ½·¨¡£µ÷ÓÃʱ£¬½Ó¿ÚÃûÓë·½·¨ÃûÓÃ'_'½ÓºÏ£¬¶øÇÒÈ«²¿Ê¹Óôóд×Öĸ¡£ ÀýÈ磬½Ó¿ÚÃûΪfoo£¬·½·¨Îªbar£¬µ÷ÓþÍÊÇ: [·µ»ØÖµ = ] FOO_BAR(¶ÔÏó [, ÆäËü²ÎÊý]);

µ±Ò»¸öÓÃkobj_create()²»ÔÙÐèÒª±»Ê¹ÓÃʱ£¬¿É¶ÔÕâ¸ö¶ÔÏóµ÷ÓÃkobj_delete()¡£µ±Ò»¸öÀ಻ÔÙÐèÒª±»Ê¹ÓÃʱ£¬¿É¶ÔÕâ¸öÀàµ÷ÓÃkobj_class_free()¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ