linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg KH <greg@kroah.com>
To: Cornelia Huck <cornelia.huck@de.ibm.com>,
	Kay Sievers <kay.sievers@vrfy.org>,
	Alan Stern <stern@rowland.harvard.edu>
Cc: Kernel development list <linux-kernel@vger.kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Randy Dunlap <randy.dunlap@oracle.com>
Subject: [RFC] kobject: add kobject_init_ng and kobject_init_and_add functions
Date: Fri, 30 Nov 2007 11:53:00 -0800	[thread overview]
Message-ID: <20071130195300.GB4659@kroah.com> (raw)
In-Reply-To: <20071130195150.GA4659@kroah.com>

This is what the kobject_init function is going to become.  Add it to
the kernel and then we can convert over the current kobject_init() users
before renaming it.

Also add a kobject_init_and_add function which bundles up what a lot of
the current callers want to do all at once, and it properly handles the
memory usages, unlike kobject_register();

Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 include/linux/kobject.h |    8 ++++
 lib/kobject.c           |   91 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)

--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -79,6 +79,14 @@ static inline const char * kobject_name(
 }
 
 extern void kobject_init(struct kobject *);
+extern int __must_check kobject_init_ng(struct kobject *kobj,
+					struct kobj_type *ktype,
+					struct kobject *parent,
+					const char *fmt, ...);
+extern int __must_check kobject_init_and_add(struct kobject *kobj,
+					     struct kobj_type *ktype,
+					     struct kobject *parent,
+					     const char *fmt, ...);
 extern void kobject_cleanup(struct kobject *);
 
 extern int __must_check kobject_add(struct kobject *);
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -347,6 +347,97 @@ int kobject_set_name(struct kobject *kob
 }
 EXPORT_SYMBOL(kobject_set_name);
 
+/*
+ * kobject_init_varg - the main kobject init function
+ *
+ * The kobject is initialized here with the needed fields.  After this
+ * function returns successfully, the kobject can not be freed directly,
+ * kobject_put must be called in order to clean it up properly.  If this
+ * function returns an error, the memory can be safely freed directly.
+ */
+static int kobject_init_varg(struct kobject *kobj, struct kobj_type *ktype,
+			     struct kobject *parent, const char *fmt,
+			     va_list vargs)
+{
+	va_list aq;
+	int retval;
+
+	if ((!kobj) || (!ktype))
+		return -EINVAL;
+
+	WARN_ON(atomic_read(&kobj->kref.refcount));
+	kref_init(&kobj->kref);
+	INIT_LIST_HEAD(&kobj->entry);
+	kobj->ktype = ktype;
+	kobj->parent = parent;
+
+	va_copy(aq, vargs);
+	retval = kobject_set_name_vargs(kobj, fmt, aq);
+	va_end(aq);
+
+	return retval;
+}
+
+/**
+ * kobject_init_ng - initialize a kobject structure
+ * @kobj: pointer to the kobject to initialize
+ * @ktype: pointer to the ktype for this kobject.
+ * @parent: pointer to the parent of this kobject.
+ * @fmt: the name of the kobject.
+ *
+ * This function will properly initialize a kobject such that it can then
+ * be passed to the kobject_add() call.
+ *
+ * If the function returns an error, the memory allocated by the kobject
+ * can be safely freed, no other functions need to be called.
+ */
+int kobject_init_ng(struct kobject *kobj, struct kobj_type *ktype,
+		    struct kobject *parent, const char *fmt, ...)
+{
+	va_list args;
+	int retval;
+
+	va_start(args, fmt);
+	retval = kobject_init_varg(kobj, ktype, parent, fmt, args);
+	va_end(args);
+
+	return retval;
+}
+EXPORT_SYMBOL(kobject_init_ng);
+
+/**
+ * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
+ * @kobj: pointer to the kobject to initialize
+ * @ktype: pointer to the ktype for this kobject.
+ * @parent: pointer to the parent of this kobject.
+ * @fmt: the name of the kobject.
+ *
+ * This function will properly initialize a kobject and then call
+ * kobject_add().
+ *
+ * If the function returns an error, the memory allocated by the kobject
+ * can be safely freed, no other functions need to be called.
+ */
+int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
+			 struct kobject *parent, const char *fmt, ...)
+{
+	va_list args;
+	int retval;
+
+	va_start(args, fmt);
+	retval = kobject_init_varg(kobj, ktype, parent, fmt, args);
+	va_end(args);
+	if (retval)
+		return retval;
+
+	retval = kobject_add(kobj);
+	if (retval)
+		kobject_put(kobj);
+
+	return retval;
+}
+EXPORT_SYMBOL(kobject_init_and_add);
+
 /**
  *	kobject_rename - change the name of an object
  *	@kobj:	object in question.

  reply	other threads:[~2007-11-30 19:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-30 19:51 [RFC] kobject_init changes Greg KH
2007-11-30 19:53 ` Greg KH [this message]
2007-11-30 19:54   ` [RFC] kobject: convert some users of kobject_init to the new functions Greg KH
2007-11-30 20:25   ` [RFC] kobject: add kobject_init_ng and kobject_init_and_add functions Alan Stern
2007-11-30 21:04     ` Greg KH
2007-11-30 21:07       ` Greg KH
2007-11-30 21:19       ` Alan Stern
2007-11-30 21:48         ` Greg KH
2007-11-30 22:10           ` Alan Stern
2007-11-30 22:26             ` Greg KH
2007-11-30 23:22               ` Alan Stern
2007-12-01  0:58                 ` Greg KH
2007-11-30 22:33             ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071130195300.GB4659@kroah.com \
    --to=greg@kroah.com \
    --cc=corbet@lwn.net \
    --cc=cornelia.huck@de.ibm.com \
    --cc=kay.sievers@vrfy.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=randy.dunlap@oracle.com \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).