[patch-2.3.40-pre6] kzalloc() (ala kmem_zalloc() of SVR4)

Tigran Aivazian tigran en sco.COM
Vie Ene 21 01:06:17 CST 2000


Hi Linus,

a common code sequence of calling kmalloc() and then memset(p,0,size) can
be optimized into a single exported interface called kzalloc() like it is
done on other systems (kmem_zalloc()). This is a simple and harmless
optimization - please consider it:

  http://www.ocston.org/~tigran/patches/kzalloc-2.3.40-p6.patch

I only converted a couple of places (including BFS) to use kzallo() -
there is surely plenty more cases that can be improved.

Regards,
------
Tigran A. Aivazian           | http://www.sco.com
Escalations Research Group   | tel: +44-(0)1923-813796
Santa Cruz Operation Ltd     | http://www.ocston.org/~tigran

diff -urN -X dontdiff linux/fs/bfs/inode.c linux-kzalloc/fs/bfs/inode.c
--- linux/fs/bfs/inode.c	Tue Nov  9 18:02:33 1999
+++ linux-kzalloc/fs/bfs/inode.c	Thu Jan 20 15:34:27 2000
@@ -296,17 +296,15 @@
 			+ BFS_ROOT_INO - 1;
 
 	bmap_len = sizeof(struct bfs_bmap) * s->su_lasti;
-	s->su_bmap = kmalloc(bmap_len, GFP_KERNEL);
+	s->su_bmap = kzalloc(bmap_len, GFP_KERNEL);
 	if (!s->su_bmap)
 		goto out;
-	memset(s->su_bmap, 0, bmap_len);
 	imap_len = s->su_lasti/8 + 1;
-	s->su_imap = kmalloc(imap_len, GFP_KERNEL);
+	s->su_imap = kzalloc(imap_len, GFP_KERNEL);
 	if (!s->su_imap) {
 		kfree(s->su_bmap);
 		goto out;
 	}
-	memset(s->su_imap, 0, imap_len);
 	for (i=0; i<BFS_ROOT_INO; i++) {
 		s->su_bmap[i].start = s->su_bmap[i].end = 0;
 		set_bit(i, s->su_imap);
diff -urN -X dontdiff linux/fs/super.c linux-kzalloc/fs/super.c
--- linux/fs/super.c	Wed Jan 19 07:41:04 2000
+++ linux-kzalloc/fs/super.c	Thu Jan 20 15:36:39 2000
@@ -89,10 +89,9 @@
 	struct vfsmount *lptr;
 	char *tmp, *name;
 
-	lptr = (struct vfsmount *)kmalloc(sizeof(struct vfsmount), GFP_KERNEL);
+	lptr = (struct vfsmount *)kzalloc(sizeof(struct vfsmount), GFP_KERNEL);
 	if (!lptr)
 		goto out;
-	memset(lptr, 0, sizeof(struct vfsmount));
 
 	lptr->mnt_sb = sb;
 	lptr->mnt_dev = sb->s_dev;
@@ -517,10 +516,9 @@
 	/* Need a new one... */
 	if (nr_super_blocks >= max_super_blocks)
 		return NULL;
-	s = kmalloc(sizeof(struct super_block),  GFP_USER);
+	s = kzalloc(sizeof(struct super_block),  GFP_USER);
 	if (s) {
 		nr_super_blocks++;
-		memset(s, 0, sizeof(struct super_block));
 		INIT_LIST_HEAD(&s->s_dirty);
 		list_add (&s->s_list, super_blocks.prev);
 		init_waitqueue_head(&s->s_wait);
diff -urN -X dontdiff linux/kernel/ksyms.c linux-kzalloc/kernel/ksyms.c
--- linux/kernel/ksyms.c	Wed Jan 19 07:41:06 2000
+++ linux-kzalloc/kernel/ksyms.c	Thu Jan 20 15:34:02 2000
@@ -106,6 +106,7 @@
 EXPORT_SYMBOL(kmem_cache_alloc);
 EXPORT_SYMBOL(kmem_cache_free);
 EXPORT_SYMBOL(kmalloc);
+EXPORT_SYMBOL(kzalloc);
 EXPORT_SYMBOL(kfree);
 EXPORT_SYMBOL(kfree_s);
 EXPORT_SYMBOL(vmalloc);
diff -urN -X dontdiff linux/mm/slab.c linux-kzalloc/mm/slab.c
--- linux/mm/slab.c	Wed Jan 19 07:41:07 2000
+++ linux-kzalloc/mm/slab.c	Thu Jan 20 15:33:21 2000
@@ -1683,6 +1683,17 @@
 	return NULL;
 }
 
+void *
+kzalloc(size_t size, int flags)
+{
+	void * mem;
+
+	mem = kmalloc(size, flags);
+	if (mem)
+		memset(mem, 0, size);
+	return mem;	
+}
+
 void
 kfree(const void *objp)
 {




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



Más información sobre la lista de distribución Ayuda