aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@suse.de>2007-09-12 18:06:57 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2007-10-12 17:51:02 -0400
commitce2c9cb0259acd2aed184499ebe41ab00da13b25 (patch)
tree1c0b25cc5cff7f7778b3e8c7441c2f5d89f027f6
parent34980ca8faebfcce31094eba6ffbb0113950361f (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>
-rw-r--r--include/linux/kobject.h7
-rw-r--r--lib/kobject.c79
2 files changed, 42 insertions, 44 deletions
diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index c0fb535d7acd..8b45946e8506 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -63,7 +63,6 @@ extern const char *kobject_actions[];
63 63
64struct kobject { 64struct kobject {
65 const char * k_name; 65 const char * k_name;
66 char name[KOBJ_NAME_LEN];
67 struct kref kref; 66 struct kref kref;
68 struct list_head entry; 67 struct list_head entry;
69 struct kobject * parent; 68 struct kobject * parent;
@@ -188,18 +187,18 @@ extern struct kobject * kset_find_obj(struct kset *, const char *);
188 * Use this when initializing an embedded kset with no other 187 * Use this when initializing an embedded kset with no other
189 * fields to initialize. 188 * fields to initialize.
190 */ 189 */
191#define set_kset_name(str) .kset = { .kobj = { .name = str } } 190#define set_kset_name(str) .kset = { .kobj = { .k_name = str } }
192 191
193 192
194#define decl_subsys(_name,_type,_uevent_ops) \ 193#define decl_subsys(_name,_type,_uevent_ops) \
195struct kset _name##_subsys = { \ 194struct kset _name##_subsys = { \
196 .kobj = { .name = __stringify(_name) }, \ 195 .kobj = { .k_name = __stringify(_name) }, \
197 .ktype = _type, \ 196 .ktype = _type, \
198 .uevent_ops =_uevent_ops, \ 197 .uevent_ops =_uevent_ops, \
199} 198}
200#define decl_subsys_name(_varname,_name,_type,_uevent_ops) \ 199#define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
201struct kset _varname##_subsys = { \ 200struct kset _varname##_subsys = { \
202 .kobj = { .name = __stringify(_name) }, \ 201 .kobj = { .k_name = __stringify(_name) }, \
203 .ktype = _type, \ 202 .ktype = _type, \
204 .uevent_ops =_uevent_ops, \ 203 .uevent_ops =_uevent_ops, \
205} 204}
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)
255int kobject_set_name(struct kobject * kobj, const char * fmt, ...) 255int 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: 297done:
300 return error; 298 return error;
301} 299}
302
303EXPORT_SYMBOL(kobject_set_name); 300EXPORT_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);