diff options
author | Greg Kroah-Hartman <gregkh@suse.de> | 2007-09-12 18:06:57 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2007-10-12 17:51:02 -0400 |
commit | ce2c9cb0259acd2aed184499ebe41ab00da13b25 (patch) | |
tree | 1c0b25cc5cff7f7778b3e8c7441c2f5d89f027f6 /lib | |
parent | 34980ca8faebfcce31094eba6ffbb0113950361f (diff) |
kobject: remove the static array for the name
Due to historical reasons, struct kobject contained a static array for
the name, and a dynamic pointer in case the name got bigger than the
array. That's just dumb, as people didn't always know which variable to
reference, even with the accessor for the kobject name.
This patch removes the static array, potentially saving a lot of memory
as the majority of kobjects do not have a very long name.
Thanks to Kay for the idea to do this.
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/kobject.c | 79 |
1 files changed, 39 insertions, 40 deletions
diff --git a/lib/kobject.c b/lib/kobject.c index 1326041213dd..10ae2ebeaf96 100644 --- a/lib/kobject.c +++ b/lib/kobject.c | |||
@@ -170,7 +170,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent) | |||
170 | if (!(kobj = kobject_get(kobj))) | 170 | if (!(kobj = kobject_get(kobj))) |
171 | return -ENOENT; | 171 | return -ENOENT; |
172 | if (!kobj->k_name) | 172 | if (!kobj->k_name) |
173 | kobj->k_name = kobj->name; | 173 | kobject_set_name(kobj, "NO_NAME"); |
174 | if (!*kobj->k_name) { | 174 | if (!*kobj->k_name) { |
175 | pr_debug("kobject attempted to be registered with no name!\n"); | 175 | pr_debug("kobject attempted to be registered with no name!\n"); |
176 | WARN_ON(1); | 176 | WARN_ON(1); |
@@ -181,7 +181,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent) | |||
181 | 181 | ||
182 | pr_debug("kobject %s: registering. parent: %s, set: %s\n", | 182 | pr_debug("kobject %s: registering. parent: %s, set: %s\n", |
183 | kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", | 183 | kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", |
184 | kobj->kset ? kobj->kset->kobj.name : "<NULL>" ); | 184 | kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" ); |
185 | 185 | ||
186 | if (kobj->kset) { | 186 | if (kobj->kset) { |
187 | spin_lock(&kobj->kset->list_lock); | 187 | spin_lock(&kobj->kset->list_lock); |
@@ -255,54 +255,50 @@ int kobject_register(struct kobject * kobj) | |||
255 | int kobject_set_name(struct kobject * kobj, const char * fmt, ...) | 255 | int kobject_set_name(struct kobject * kobj, const char * fmt, ...) |
256 | { | 256 | { |
257 | int error = 0; | 257 | int error = 0; |
258 | int limit = KOBJ_NAME_LEN; | 258 | int limit; |
259 | int need; | 259 | int need; |
260 | va_list args; | 260 | va_list args; |
261 | char * name; | 261 | char *name; |
262 | 262 | ||
263 | /* | 263 | /* find out how big a buffer we need */ |
264 | * First, try the static array | 264 | name = kmalloc(1024, GFP_KERNEL); |
265 | */ | 265 | if (!name) { |
266 | va_start(args,fmt); | 266 | error = -ENOMEM; |
267 | need = vsnprintf(kobj->name,limit,fmt,args); | 267 | goto done; |
268 | } | ||
269 | va_start(args, fmt); | ||
270 | need = vsnprintf(name, 1024, fmt, args); | ||
268 | va_end(args); | 271 | va_end(args); |
269 | if (need < limit) | 272 | kfree(name); |
270 | name = kobj->name; | 273 | |
271 | else { | 274 | /* Allocate the new space and copy the string in */ |
272 | /* | 275 | limit = need + 1; |
273 | * Need more space? Allocate it and try again | 276 | name = kmalloc(limit, GFP_KERNEL); |
274 | */ | 277 | if (!name) { |
275 | limit = need + 1; | 278 | error = -ENOMEM; |
276 | name = kmalloc(limit,GFP_KERNEL); | 279 | goto done; |
277 | if (!name) { | 280 | } |
278 | error = -ENOMEM; | 281 | va_start(args, fmt); |
279 | goto Done; | 282 | need = vsnprintf(name, limit, fmt, args); |
280 | } | 283 | va_end(args); |
281 | va_start(args,fmt); | 284 | |
282 | need = vsnprintf(name,limit,fmt,args); | 285 | /* something wrong with the string we copied? */ |
283 | va_end(args); | 286 | if (need >= limit) { |
284 | 287 | kfree(name); | |
285 | /* Still? Give up. */ | 288 | error = -EFAULT; |
286 | if (need >= limit) { | 289 | goto done; |
287 | kfree(name); | ||
288 | error = -EFAULT; | ||
289 | goto Done; | ||
290 | } | ||
291 | } | 290 | } |
292 | 291 | ||
293 | /* Free the old name, if necessary. */ | 292 | /* Free the old name, if necessary. */ |
294 | if (kobj->k_name && kobj->k_name != kobj->name) | 293 | kfree(kobj->k_name); |
295 | kfree(kobj->k_name); | ||
296 | 294 | ||
297 | /* Now, set the new name */ | 295 | /* Now, set the new name */ |
298 | kobj->k_name = name; | 296 | kobj->k_name = name; |
299 | Done: | 297 | done: |
300 | return error; | 298 | return error; |
301 | } | 299 | } |
302 | |||
303 | EXPORT_SYMBOL(kobject_set_name); | 300 | EXPORT_SYMBOL(kobject_set_name); |
304 | 301 | ||
305 | |||
306 | /** | 302 | /** |
307 | * kobject_rename - change the name of an object | 303 | * kobject_rename - change the name of an object |
308 | * @kobj: object in question. | 304 | * @kobj: object in question. |
@@ -477,13 +473,16 @@ void kobject_cleanup(struct kobject * kobj) | |||
477 | struct kobj_type * t = get_ktype(kobj); | 473 | struct kobj_type * t = get_ktype(kobj); |
478 | struct kset * s = kobj->kset; | 474 | struct kset * s = kobj->kset; |
479 | struct kobject * parent = kobj->parent; | 475 | struct kobject * parent = kobj->parent; |
476 | const char *name = kobj->k_name; | ||
480 | 477 | ||
481 | pr_debug("kobject %s: cleaning up\n",kobject_name(kobj)); | 478 | pr_debug("kobject %s: cleaning up\n",kobject_name(kobj)); |
482 | if (kobj->k_name != kobj->name) | 479 | if (t && t->release) { |
483 | kfree(kobj->k_name); | ||
484 | kobj->k_name = NULL; | ||
485 | if (t && t->release) | ||
486 | t->release(kobj); | 480 | t->release(kobj); |
481 | /* If we have a release function, we can guess that this was | ||
482 | * not a statically allocated kobject, so we should be safe to | ||
483 | * free the name */ | ||
484 | kfree(name); | ||
485 | } | ||
487 | if (s) | 486 | if (s) |
488 | kset_put(s); | 487 | kset_put(s); |
489 | kobject_put(parent); | 488 | kobject_put(parent); |