Closed
Bug 654436
Opened 13 years ago
Closed 13 years ago
PR_ARRAY_SIZE macro: number of elements in array: sizeof(a)/sizeof(a[0])
Categories
(NSPR :: NSPR, enhancement, P3)
Tracking
(Not tracked)
RESOLVED
FIXED
4.8.9
People
(Reporter: grapvar, Assigned: grapvar)
Details
Attachments
(3 files, 3 obsolete files)
5.04 KB,
text/plain
|
Details | |
10.56 KB,
text/plain
|
Details | |
5.61 KB,
patch
|
Details | Diff | Splinter Review |
The number of elements in array is counted very often: 123 times in the NSS and 62 times in the NSPR sources (see attachments).
When there is no a standard macro, you have a dilemma: write your own macro or just inline [sizeof(a)/sizeof(*a)].
Mozilla is a big application, with many people working on different parts independently. People, who choose to write a macro, start to define similar, but different names.
NSS: NSS_CKCAPI_ARRAY_SIZE NSS_CKMK_ARRAY_SIZE NUM_ELEM NUM_SUITEINFOS
NSPR: countof
JS: JS_ARRAY_LENGTH
Other people ought to encumber the code by continuous [sizeof(longUnpleasantUnreadableName)/sizeof(*longUnpleasantUnreadableName)].
Let define a "standard" macro at the lowest possible level, and let it be adopted by NSPR, NSS, ...
Assignee | ||
Comment 1•13 years ago
|
||
Assignee | ||
Comment 2•13 years ago
|
||
Attachment #529709 -
Flags: review?(wtc)
The rest of the Mozilla client code uses NS_ARRAY_LENGTH, so I'd suggest making this PR_ARRAY_LENGTH to match.
Comment 4•13 years ago
|
||
Comment on attachment 529709 [details] [diff] [review]
PR_ARR_SIZE macro added
Thank you for the patch. I have some suggested changes below.
>+/** Number of elements in array.
>+ */
NSPR doesn't use a documentation generator that recognizes the
/** convention. So please use the normal /*.
>+#define PR_ARR_SIZE(a) (sizeof(a)/sizeof(*(a)))
The macro should be named PR_ARRAY_SIZE. It is not necessary to
save two characters.
Use (a)[0] instead of *(a).
Comment: please do NOT write a patch to change existing code to
use the PR_ARRAY_SIZE macro.
Attachment #529709 -
Flags: review?(wtc) → review-
Comment 5•13 years ago
|
||
(In reply to comment #3)
> The rest of the Mozilla client code uses NS_ARRAY_LENGTH, so I'd suggest making
> this PR_ARRAY_LENGTH to match.
Thank you for the suggestion. "array size" is also common. Windows has an
"ARRAYSIZE" macro. See also
http://www.boost.org/doc/libs/1_46_1/libs/preprocessor/doc/ref/array_size.html
Assignee | ||
Comment 6•13 years ago
|
||
(In reply to comment #4)
> Comment: please do NOT write a patch to change existing code to use the PR_ARRAY_SIZE macro.
Wan-Teh, what is Mozilla policy for the code refactoring ?
I have no patch for this particular case (I have for others), but a plenty of spots in NSS cry out for changes/enhancements insistently.
One of the bad consequences of not addressing these spots in time is: the new code inherits and multiplies annoying discouraged techniques.
Conversely, consistent refactoring gradually improves the overall quality of the code.
Attachment #529709 -
Attachment is obsolete: true
Attachment #529946 -
Flags: review?(wtc)
Assignee | ||
Updated•13 years ago
|
Summary: PR_ARR_SIZE macro: number of elements in array: sizeof(a)/sizeof(*a) → PR_ARRAY_SIZE macro: number of elements in array: sizeof(a)/sizeof(a[])
Updated•13 years ago
|
Priority: -- → P3
Target Milestone: 4.8.8 → 4.8.9
Comment 7•13 years ago
|
||
Comment on attachment 529946 [details] [diff] [review]
PR_ARRAY_SIZE macro added
r=wtc.
Please define this macro earlier in the prtypes.h file, after
the PR_ABS macro, imitating the format of existing macro
definitions and their comments.
Re: comment 6: You can write a patch to use PR_ARRAY_SIZE. The
reason I recommended against it is that it adds noise the the
diffs. (Some people read diffs to track down a regression or
assess the risk of an upgrade.) But we should have some NSPR
code that uses the new macro.
Attachment #529946 -
Flags: review?(wtc) → review+
Assignee | ||
Comment 8•13 years ago
|
||
According to comment 7: moved after PR_ABS; uses NSPR style comments.
Attachment #529946 -
Attachment is obsolete: true
Attachment #548424 -
Flags: review?(wtc)
Updated•13 years ago
|
Assignee: wtc → andreev
Status: NEW → ASSIGNED
Summary: PR_ARRAY_SIZE macro: number of elements in array: sizeof(a)/sizeof(a[]) → PR_ARRAY_SIZE macro: number of elements in array: sizeof(a)/sizeof(a[0])
Comment 9•13 years ago
|
||
I edited attachment 548424 [details] [diff] [review], replaced the 'countof' macro
in pr/tests/sprintf.c with PR_ARRAY_SIZE, and checked in
the patch on the NSPR trunk (NSPR 4.8.9).
Checking in pr/include/prtypes.h;
/cvsroot/mozilla/nsprpub/pr/include/prtypes.h,v <-- prtypes.h
new revision: 3.45; previous revision: 3.44
done
Checking in pr/tests/sprintf.c;
/cvsroot/mozilla/nsprpub/pr/tests/sprintf.c,v <-- sprintf.c
new revision: 3.8; previous revision: 3.7
done
Attachment #548424 -
Attachment is obsolete: true
Attachment #548424 -
Flags: review?(wtc)
Updated•13 years ago
|
Status: ASSIGNED → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
You need to log in
before you can comment on or make changes to this bug.
Description
•