diff options
| author | Patrick Mochel <mochel@osdl.org> | 2002-10-29 07:14:59 -0800 |
|---|---|---|
| committer | Patrick Mochel <mochel@osdl.org> | 2002-10-29 07:14:59 -0800 |
| commit | d2f8eca7b0d7ef4503e40f809fb8d12e9d1d5389 (patch) | |
| tree | 70668b6fc908d303ad7ebec42633f5422867e00e /lib | |
| parent | d8c084f9fd65a1bf948acc5bf08b066f85160cba (diff) | |
| parent | eda520259938bd3299486f12ab5058c29dcd5326 (diff) | |
Merge osdl.org:/home/mochel/src/kernel/devel/linux-2.5-virgin
into osdl.org:/home/mochel/src/kernel/devel/linux-2.5-kobject
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Makefile | 5 | ||||
| -rw-r--r-- | lib/kobject.c | 98 |
2 files changed, 101 insertions, 2 deletions
diff --git a/lib/Makefile b/lib/Makefile index 9bbb27efb10a..fa1a42d86abe 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -9,10 +9,11 @@ L_TARGET := lib.a export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o \ - crc32.o rbtree.o radix-tree.o + crc32.o rbtree.o radix-tree.o kobject.o obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o \ - bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o + bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \ + kobject.o obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o diff --git a/lib/kobject.c b/lib/kobject.c new file mode 100644 index 000000000000..48e950c9e4ce --- /dev/null +++ b/lib/kobject.c @@ -0,0 +1,98 @@ +/* + * kobject.c - library routines for handling generic kernel objects + */ + +#define DEBUG 1 + +#include <linux/kobject.h> +#include <linux/module.h> +#include <linux/stat.h> + +/** + * kobject_init - initialize object. + * @kobj: object in question. + */ + +void kobject_init(struct kobject * kobj) +{ + atomic_set(&kobj->refcount,1); + INIT_LIST_HEAD(&kobj->entry); +} + +/** + * kobject_register - register an object. + * @kobj: object in question. + * + * For now, fill in the replicated fields in the object's + * directory entry, and create a dir in sysfs. + * This stuff should go away in the future, as we move + * more implicit things to sysfs. + */ + +int kobject_register(struct kobject * kobj) +{ + pr_debug("kobject %s: registering\n",kobj->name); + if (kobj->parent) + kobject_get(kobj->parent); + return 0; +} + +/** + * kobject_unregister - unlink an object. + * @kobj: object going away. + * + * The device has been told to be removed, but may + * not necessarily be disappearing from the kernel. + * So, we remove the directory and decrement the refcount + * that we set with kobject_register(). + * + * Eventually (maybe now), the refcount will hit 0, and + * put_device() will clean the device up. + */ + +void kobject_unregister(struct kobject * kobj) +{ + pr_debug("kobject %s: unregistering\n",kobj->name); + kobject_put(kobj); +} + +/** + * kobject_get - increment refcount for object. + * @kobj: object. + */ + +struct kobject * kobject_get(struct kobject * kobj) +{ + struct kobject * ret = kobj; + if (atomic_read(&kobj->refcount) > 0) + atomic_inc(&kobj->refcount); + else + ret = NULL; + return ret; +} + +/** + * kobject_put - decrement refcount for object. + * @kobj: object. + * + * Decrement the refcount, and check if 0. If it is, then + * we're gonna need to clean it up, and decrement the refcount + * of its parent. + */ + +void kobject_put(struct kobject * kobj) +{ + struct kobject * parent = kobj->parent; + + if (!atomic_dec_and_test(&kobj->refcount)) + return; + pr_debug("kobject %s: cleaning up\n",kobj->name); + if (parent) + kobject_put(parent); +} + +EXPORT_SYMBOL(kobject_init); +EXPORT_SYMBOL(kobject_register); +EXPORT_SYMBOL(kobject_unregister); +EXPORT_SYMBOL(kobject_get); +EXPORT_SYMBOL(kobject_put); |
