diff options
author | Greg Kroah-Hartman <gregkh@suse.de> | 2007-11-29 18:32:47 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2008-01-24 23:40:09 -0500 |
commit | 663a47430b361f863b515752a97166a7a4b92d35 (patch) | |
tree | a2238877bd943666b465d704166ac3fadb1f3903 /lib/kobject.c | |
parent | 15f2bbb28e96e20149548926e5b08551ba140b14 (diff) |
kobject: fix up kobject_set_name to use kvasprintf
Kay pointed out that kobject_set_name was being very stupid, doing two
allocations for every call, when it should just be using the kernel
function kvasprintf() instead.
This change adds the internal kobject_set_name_vargs() function, which
other follow-on patches will be using.
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'lib/kobject.c')
-rw-r--r-- | lib/kobject.c | 71 |
1 files changed, 32 insertions, 39 deletions
diff --git a/lib/kobject.c b/lib/kobject.c index 9500339ae024..4a310e55a886 100644 --- a/lib/kobject.c +++ b/lib/kobject.c | |||
@@ -232,60 +232,53 @@ int kobject_register(struct kobject * kobj) | |||
232 | return error; | 232 | return error; |
233 | } | 233 | } |
234 | 234 | ||
235 | /** | ||
236 | * kobject_set_name_vargs - Set the name of an kobject | ||
237 | * @kobj: struct kobject to set the name of | ||
238 | * @fmt: format string used to build the name | ||
239 | * @vargs: vargs to format the string. | ||
240 | */ | ||
241 | static int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, | ||
242 | va_list vargs) | ||
243 | { | ||
244 | va_list aq; | ||
245 | char *name; | ||
246 | |||
247 | va_copy(aq, vargs); | ||
248 | name = kvasprintf(GFP_KERNEL, fmt, vargs); | ||
249 | va_end(aq); | ||
250 | |||
251 | if (!name) | ||
252 | return -ENOMEM; | ||
253 | |||
254 | /* Free the old name, if necessary. */ | ||
255 | kfree(kobj->k_name); | ||
256 | |||
257 | /* Now, set the new name */ | ||
258 | kobj->k_name = name; | ||
259 | |||
260 | return 0; | ||
261 | } | ||
235 | 262 | ||
236 | /** | 263 | /** |
237 | * kobject_set_name - Set the name of a kobject | 264 | * kobject_set_name - Set the name of a kobject |
238 | * @kobj: kobject to name | 265 | * @kobj: struct kobject to set the name of |
239 | * @fmt: format string used to build the name | 266 | * @fmt: format string used to build the name |
240 | * | 267 | * |
241 | * This sets the name of the kobject. If you have already added the | 268 | * This sets the name of the kobject. If you have already added the |
242 | * kobject to the system, you must call kobject_rename() in order to | 269 | * kobject to the system, you must call kobject_rename() in order to |
243 | * change the name of the kobject. | 270 | * change the name of the kobject. |
244 | */ | 271 | */ |
245 | int kobject_set_name(struct kobject * kobj, const char * fmt, ...) | 272 | int kobject_set_name(struct kobject *kobj, const char *fmt, ...) |
246 | { | 273 | { |
247 | int error = 0; | ||
248 | int limit; | ||
249 | int need; | ||
250 | va_list args; | 274 | va_list args; |
251 | char *name; | 275 | int retval; |
252 | 276 | ||
253 | /* find out how big a buffer we need */ | ||
254 | name = kmalloc(1024, GFP_KERNEL); | ||
255 | if (!name) { | ||
256 | error = -ENOMEM; | ||
257 | goto done; | ||
258 | } | ||
259 | va_start(args, fmt); | 277 | va_start(args, fmt); |
260 | need = vsnprintf(name, 1024, fmt, args); | 278 | retval = kobject_set_name_vargs(kobj, fmt, args); |
261 | va_end(args); | 279 | va_end(args); |
262 | kfree(name); | ||
263 | 280 | ||
264 | /* Allocate the new space and copy the string in */ | 281 | return retval; |
265 | limit = need + 1; | ||
266 | name = kmalloc(limit, GFP_KERNEL); | ||
267 | if (!name) { | ||
268 | error = -ENOMEM; | ||
269 | goto done; | ||
270 | } | ||
271 | va_start(args, fmt); | ||
272 | need = vsnprintf(name, limit, fmt, args); | ||
273 | va_end(args); | ||
274 | |||
275 | /* something wrong with the string we copied? */ | ||
276 | if (need >= limit) { | ||
277 | kfree(name); | ||
278 | error = -EFAULT; | ||
279 | goto done; | ||
280 | } | ||
281 | |||
282 | /* Free the old name, if necessary. */ | ||
283 | kfree(kobj->k_name); | ||
284 | |||
285 | /* Now, set the new name */ | ||
286 | kobj->k_name = name; | ||
287 | done: | ||
288 | return error; | ||
289 | } | 282 | } |
290 | EXPORT_SYMBOL(kobject_set_name); | 283 | EXPORT_SYMBOL(kobject_set_name); |
291 | 284 | ||