?????????????C/C++?????????????????????????????????????м??????????????????????????????????????????????????????????????C/C++???????????????????????????????з?????????STL??auto_ptr??Boost??Smart_ptr??QT??QPointer???壬?????????C?????????GTK+???????ü?????????????????Linux???????ν??????????????????C?????????????Linux?????????????ü???????????????????C++?????????????Boost??shared_ptr????????QT??QSharedPointer??
??????Linux????????ü???????? struct kref ???????????????????? kref ????????????????????????????????????????????豸?????????豸????????????????????豸???????????????????????豸???

???????????????????????????????豸???ζ??????м????????????????豸??????????????????????????????????????????????????????????????????豸?????ε??????????????豸?????????????????????????????????????е??????????????????????????????????????close?????????豸????????????????????kref??????????????????????
????kref????????????????????????????????
????struct kref {
????atomic_t refcount;
????};
????Linux????????????????????????????kref??
????void kref_init(struct kref *kref);
????void kref_get(struct kref *kref);
????int kref_put(struct kref *kref?? void (*release) (struct kref *kref));
????????????????α??????????????????kref??
????struct my_obj
????{
????int val;
????struct kref refcnt;
????};
????struct my_obj *obj;
????void obj_release(struct kref *ref)
????{
????struct my_obj *obj = container_of(ref?? struct my_obj?? refcnt);
????kfree(obj);
????}
????device_probe()
????{
????obj = kmalloc(sizeof(*obj)?? GFP_KERNEL);
????kref_init(&obj->refcnt);
????}
????device_disconnect()
????{
????kref_put(&obj->refcnt?? obj_release);
????}
????.open()
????{
????kref_get(&obj->refcnt);
????}
????.close()
????{
????kref_put(&obj->refcnt?? obj_release);
????}
????????δ??????????????obj_release?????????豸???????????????ü????0??????????????????????????????????????????????device_probe??????ü?????????1??????????????open??????ü????????1?????????豸???ε??device_disconnect???????????????????refcnt??????0???豸????obj????????????е?close?????????obj_release?????С?
????????α?????????????????????????????????????????н??????????豸????????????????ж???????????????kref??
????#include <linux/kernel.h>
????#include <linux/module.h>
????struct my_obj {
????int val;
????struct kref refcnt;
????};
????struct my_obj *obj;
????void obj_release(struct kref *ref)
????{
????struct my_obj *obj = container_of(ref?? struct my_obj?? refcnt);
????printk(KERN_INFO "obj_release ");
????kfree(obj);
????}
????static int __init kreftest_init(void)
????{
????printk(KERN_INFO "kreftest_init ");
????obj = kmalloc(sizeof(*obj)?? GFP_KERNEL);
????kref_init(&obj->refcnt);
????return 0;
????}
????static void __exit kreftest_exit(void)
????{
????printk(KERN_INFO "kreftest_exit ");
????kref_put(&obj->refcnt?? obj_release);
????return;
????}
????module_init(kreftest_init);
????module_exit(kreftest_exit);
????MODULE_LICENSE("GPL");
???????kbuild?????????????kref_test.ko??????????????????????????????ж????顣
????sudo insmod ./kref_test.ko
????sudo rmmod kref_test
?????????????????????????????
????kreftest_init
????kreftest_exit
????obj_release
??????????????????????
????????kref???ü???????????????д???????????????????????????????ɡ?
???????????????????????kref?????????