[PATCH] kmem_cache_lookup(): allow looking up slab caches by name

Steve Dodd (dirk@loth.demon.co.uk)
Tue, 20 Jul 1999 03:49:57 +0100


Okay, this patch adds kmem_cache_find() and kmem_cache_lookup() to mm/slab.c.
kmem_cache_find() looks up a cache in the chain; kmem_cache_lookup() returns
an existing cache of the given name if it exists, otherwise it creates it.

This should fix some problems in the nfs client that I pointed out earlier.
I also remember Al Viro wanting to be able to access caches from modules --
was this when we were discussing lightweight inodes?

Now back to figuring out what horrid things CVS has managed to do to my
kernel tree -- again...

Index: kernel/ksyms.c
===================================================================
RCS file: /usr/local/cvsroot/linux/kernel/ksyms.c,v
retrieving revision 1.1.1.14
diff -u -r1.1.1.14 ksyms.c
--- kernel/ksyms.c 1999/07/18 11:36:12 1.1.1.14
+++ kernel/ksyms.c 1999/07/20 02:11:26
@@ -97,6 +97,7 @@
EXPORT_SYMBOL(kmem_cache_shrink);
EXPORT_SYMBOL(kmem_cache_alloc);
EXPORT_SYMBOL(kmem_cache_free);
+EXPORT_SYMBOL(kmem_cache_lookup);
EXPORT_SYMBOL(kmalloc);
EXPORT_SYMBOL(kfree);
EXPORT_SYMBOL(kfree_s);
Index: include/linux/slab.h
===================================================================
RCS file: /usr/local/cvsroot/linux/include/linux/slab.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 slab.h
--- include/linux/slab.h 1999/05/18 03:05:57 1.1.1.1
+++ include/linux/slab.h 1999/07/20 02:11:11
@@ -54,6 +54,9 @@
extern int kmem_cache_shrink(kmem_cache_t *);
extern void *kmem_cache_alloc(kmem_cache_t *, int);
extern void kmem_cache_free(kmem_cache_t *, void *);
+extern kmem_cache_t *kmem_cache_lookup(const char *, size_t, size_t, unsigned long,
+ void (*)(void *, kmem_cache_t *, unsigned long),
+ void (*)(void *, kmem_cache_t *, unsigned long));

extern void *kmalloc(size_t, int);
extern void kfree(const void *);
Index: mm/slab.c
===================================================================
RCS file: /usr/local/cvsroot/linux/mm/slab.c,v
retrieving revision 1.1.1.3
diff -u -r1.1.1.3 slab.c
--- mm/slab.c 1999/07/18 10:07:30 1.1.1.3
+++ mm/slab.c 1999/07/20 02:34:48
@@ -1934,3 +1934,38 @@
return len;
}
#endif /* CONFIG_PROC_FS */
+
+/* find an existing cache by name */
+static kmem_cache_t *
+kmem_cache_find(const char *name)
+{
+ kmem_cache_t *searchp, *cachep = NULL;
+
+ down(&cache_chain_sem);
+ searchp = &cache_cache;
+ do {
+ if (!strcmp(searchp->c_name, name)) {
+ cachep = searchp;
+ break;
+ }
+ searchp = searchp->c_nextp;
+ } while (searchp != &cache_cache);
+ up(&cache_chain_sem);
+
+ return cachep;
+}
+
+/* return an existing cache of the specified name, or create it if it does
+ not yet exist */
+kmem_cache_t *
+kmem_cache_lookup(const char *name, size_t size, size_t offset,
+ unsigned long flags, void (*ctor)(void*, kmem_cache_t *, unsigned long),
+ void (*dtor)(void*, kmem_cache_t *, unsigned long))
+{
+ kmem_cache_t *cachep;
+
+ if ((cachep = kmem_cache_find(name)))
+ return cachep;
+
+ return kmem_cache_create(strdup(name), size, offset, flags, ctor, dtor);
+}

-- 
Hail Eris!

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/