ntfs: remove redundant assignments
A message that is not in the archive (sent off-list or not yet received): 1117044875.9510.2.camel@localhost
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi Anton,
Quoted 42 lines · At some point in time, I wrote:
Index:
2.6-mm/fs/ntfs/super.cQuoted 13 lines
===================================================================
fs/ntfs/super.c +1
-1@@ -2283,7 +2283,7 @@ sb->s_flags |=MS_RDONLY |MS_NOATIME |MS_NODIRATIME ;#endif /* ! NTFS_RW */ /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */ - sb->s_fs_info =kmalloc (sizeof (ntfs_volume),GFP_NOFS ); + sb->s_fs_info =kcalloc (1 ,sizeof (ntfs_volume),GFP_NOFS ); vol =NTFS_SB (sb);if (!vol) {if (!silent)fs/ntfs/super.c line 2295
Quoted 5 lines
fs/ntfs/super.c +0
-1@@ -2292,28 +2292,9 @@return -ENOMEM ; }/* Initialize ntfs_volume structure. */ -memset (vol,0 ,sizeof (ntfs_volume)); vol->sb = sb;The above is fine, thanks.
Quoted 20 lines
fs/ntfs/super.c +0
-18- vol->upcase =NULL ; - vol->attrdef =NULL ; - vol->mft_ino =NULL ; - vol->mftbmp_ino =NULL ;init_rwsem (&vol->mftbmp_lock); -#ifdef NTFS_RW - vol->mftmirr_ino =NULL ; - vol->logfile_ino =NULL ; -#endif /* NTFS_RW */ - vol->lcnbmp_ino =NULL ;init_rwsem (&vol->lcnbmp_lock); - vol->vol_ino =NULL ; - vol->root_ino =NULL ; - vol->secure_ino =NULL ; - vol->extend_ino =NULL ; -#ifdef NTFS_RW - vol->quota_ino =NULL ; - vol->quota_q_ino =NULL ; -#endif /* NTFS_RW */ - vol->nls_map =NULL ;
Quoted 1 line · Anton Altaparmakov
This is not. memset(0) is not the same as = NULL IMO. I don't care if the compiler thinks it is the same. NULL does not have to be 0 so I prefer to initialize pointers explicitly to NULL. Even more so since this code is not performance critical at all so I prefer clarity here.
I kind of figured out you were doing it on purpose. The fact is, NULL is zero on _all_ Linux architectures. If it weren't, we'd have a lot of broken code. Let me play the devils advocate here: why do you memset() (now kcalloc()) in the first place?
Quoted 19 lines · At some point in time, I wrote:
Quoted 19 lines
Index:
2.6-mm/fs/ntfs/index.c
===================================================================fs/ntfs/index.c +1
-9@@ -40,16 +40,8 @@ ictx =kmem_cache_alloc (ntfs_index_ctx_cache,SLAB_NOFS );if (ictx) { +memset (ictx,0 ,sizeof (*ictx)); ictx->idx_ni = idx_ni; - ictx->entry =NULL ; - ictx->data =NULL ; - ictx->data_len =0 ; - ictx->is_in_root =0 ; - ictx->ir =NULL ; - ictx->actx =NULL ; - ictx->base_ni =NULL ; - ictx->ia =NULL ; - ictx->page =NULL ;
Quoted 1 line · Anton Altaparmakov
Again, as above, I prefer to have the explicit = NULL instead of a memset.
There's a simple reason why I don't like explicit assignments: it's way too easy to forget to initialize something.
Pekka
Quoted 10 lines · Pekka J Enberg
Quoted 4 lines · Anton Altaparmakov
This is not. memset(0) is not the same as = NULL IMO. I don't care if the compiler thinks it is the same. NULL does not have to be 0 so I prefer to initialize pointers explicitly to NULL. Even more so since this code is not performance critical at all so I prefer clarity here.
I kind of figured out you were doing it on purpose. The fact is, NULL is zero on _all_ Linux architectures. If it weren't, we'd have a lot of broken code. Let me play the devils advocate here: why do you
memset()(nowkcalloc()) in the first place?
Oh, come on...
ictx = kmalloc(sizeof(ntfs_index_context), GFP_NOFS);
if (ictx)
*ictx = (ntfs_index_context){.idx_ni = idx_ni};
return ictx;and be done with that. Let compiler do its job. And yes, that *will* give properly initialized pointers even for weird platforms. Not that we had the slightest chance of porting to any of them...
Quoted 2 lines
There's a simple reason why I don't like explicit assignments: it's way too easy to forget to initialize something.
So use the proper constructs. Variant above is guaranteed to do the right thing on any C99 compiler, provided that kmalloc() returns NULL or pointer to object sufficiently large and properly aligned for ntfs_index_context. All missing fields will be initialized the same way they would for initializer of a static object.
---- Original Message -----
From: "Al Viro" <[email protected]>
To: "Pekka J Enberg" <[email protected]>
Cc: "Anton Altaparmakov" <[email protected]>; <[email protected]>; <[email protected]>
Sent: Thursday, May 26, 2005 03:04
Subject: Re: ntfs: remove redundant assignments
Quoted 5 lines · Pekka J Enberg
Quoted 4 lines · Anton Altaparmakov
Quoted 3 lines
This is not. memset(0) is not the same as = NULL IMO. I don't care if the compiler thinks it is the same. NULL does not have to be 0 so I prefer to initialize pointers explicitly to NULL. Even more so since
this
Quoted 1 line
Quoted 1 line
Quoted 1 line
code is not performance critical at all so I prefer clarity here.
FWIW, a series of explicit assignments to zero puffs up the code on x86. Several GCC releases using the default kernel -O2 build are too dumb to zero EAX, or some other reg, and assign using it, so you're looking at 4 bytes of immediate zero plus opcode ModRm/SIB. If the locations being assigned happened to be statics you can often wind up with a 10 byte instruction to zero a single dword somewhere.
I have tried a few tricks trying to get GCC to not use this 4 byte immediate 0 in non speed critical areas all to no avail (ex. foo &= 0; the AND instruction can use a one byte sign extended operand, but GCC morphs something like " &= 0" into a straight up "MOV thing,imm32" instruction)
PATCH 1/1 ntfs: use struct initializers
In reply to Al ViroHi Anton,
This patch converts explicit NULL assignments to use struct initializers as suggested by Al Viro.
---
super.c | 40 ++++++++++++---------------------------- 1 files changed, 12 insertions(+), 28 deletions(-)
fs/ntfs/super.c +12 -28
@@ -2292,36 +2292,20 @@return -ENOMEM ; }/* Initialize ntfs_volume structure. */ -memset (vol,0 ,sizeof (ntfs_volume)); - vol->sb = sb; - vol->upcase =NULL ; - vol->attrdef =NULL ; - vol->mft_ino =NULL ; - vol->mftbmp_ino =NULL ; + *vol = (ntfs_volume ) { + .sb = sb, +/* +* Default is group and other don't have any access to files or +* directories while owner has full access. Further, files by +* default are not executable but directories are of course +* browseable. +*/ + .fmask =0177 , + .dmask =0077 , + + };init_rwsem (&vol->mftbmp_lock); -#ifdef NTFS_RW - vol->mftmirr_ino =NULL ; - vol->logfile_ino =NULL ; -#endif /* NTFS_RW */ - vol->lcnbmp_ino =NULL ;init_rwsem (&vol->lcnbmp_lock); - vol->vol_ino =NULL ; - vol->root_ino =NULL ; - vol->secure_ino =NULL ; - vol->extend_ino =NULL ; -#ifdef NTFS_RW - vol->quota_ino =NULL ; - vol->quota_q_ino =NULL ; -#endif /* NTFS_RW */ - vol->nls_map =NULL ; - -/* -* Default is group and other don't have any access to files or -* directories while owner has full access. Further, files by default -* are not executable but directories are of course browseable. -*/ - vol->fmask =0177 ; - vol->dmask =0077 ;unlock_kernel ();
Re: [PATCH] ntfs: use struct initializers
Hi Pekka,
Quoted 2 lines · Pekka J Enberg
This patch converts explicit NULL assignments to use struct initializers as suggested by Al Viro.
Thanks. I applied this (slightly modified since you went outside the 80 char width) as well as equivalent changes to attrib.c and index.c.
Best regards,
Anton
Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @) Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/
Quoted 14 lines · Al Viro
Quoted 10 lines · Pekka J Enberg
Quoted 4 lines · Anton Altaparmakov
This is not. memset(0) is not the same as = NULL IMO. I don't care if the compiler thinks it is the same. NULL does not have to be 0 so I prefer to initialize pointers explicitly to NULL. Even more so since this code is not performance critical at all so I prefer clarity here.
I kind of figured out you were doing it on purpose. The fact is, NULL is zero on _all_ Linux architectures. If it weren't, we'd have a lot of broken code. Let me play the devils advocate here: why do you
memset()(nowkcalloc()) in the first place?Oh, come on...
Quoted 8 lines
ictx = kmalloc(sizeof(ntfs_index_context), GFP_NOFS); if (ictx) *ictx = (ntfs_index_context){.idx_ni = idx_ni}; return ictx;and be done with that. Let compiler do its job. And yes, that *will* give properly initialized pointers even for weird platforms. Not that we had the slightest chance of porting to any of them...
Oh, cool. I didn't think gcc-2.95 did this but I just tried it with 2.95.2 and it worked.
Thanks,
Anton
Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @) Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/