nasmlib: Rename elements() macro to ARRAY_SIZE
authorCyrill Gorcunov <gorcunov@gmail.com>
Thu, 3 Jun 2010 18:04:36 +0000 (22:04 +0400)
committerCyrill Gorcunov <gorcunov@gmail.com>
Thu, 3 Jun 2010 19:17:21 +0000 (23:17 +0400)
ARRAY_SIZE is a well known name pointing out that
we're dealing with array in macro argument.

Also to be on a safe side prefix_name helper should
check the index been in bounds more precisely.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
nasmlib.c
nasmlib.h
output/outform.c
preproc.c

index 2ae8619450513a277bb804e521228aaca69c4043..460186e6fc2310550144bb1c89a818e40ac88314 100644 (file)
--- a/nasmlib.c
+++ b/nasmlib.c
@@ -570,7 +570,7 @@ static const char *prefix_names[] = {
 const char *prefix_name(int token)
 {
     unsigned int prefix = token-PREFIX_ENUM_START;
-    if (prefix > elements(prefix_names))
+    if (prefix >= ARRAY_SIZE(prefix_names))
        return NULL;
 
     return prefix_names[prefix];
index 02876c747429dac0f480eae047bcb20fe5eddd34..2c335e1175509a2ce41c3341c0d22054e1f93e39 100644 (file)
--- a/nasmlib.h
+++ b/nasmlib.h
@@ -239,8 +239,7 @@ void standard_extension(char *inname, char *outname, char *extension);
  * This is a useful #define which I keep meaning to use more often:
  * the number of elements of a statically defined array.
  */
-
-#define elements(x)     ( sizeof(x) / sizeof(*(x)) )
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
 /*
  * List handling
index f5b6739f9460afb0307e873c3e5d32d4023a026b..e386343ec270d1083922d7e26b420e2980784f06 100644 (file)
@@ -59,7 +59,7 @@ struct ofmt *ofmt_find(char *name)
     }
 
     /* lets walk thru aliases then */
-    for (i = 0; i < elements(ofmt_aliases); i++) {
+    for (i = 0; i < ARRAY_SIZE(ofmt_aliases); i++) {
         if (ofmt_aliases[i].shortname &&
             !nasm_stricmp(name, ofmt_aliases[i].shortname))
             return ofmt_aliases[i].ofmt;
@@ -92,7 +92,7 @@ void ofmt_list(struct ofmt *deffmt, FILE * fp)
     }
 
     /* lets walk through aliases then */
-    for (i = 0; i < elements(ofmt_aliases); i++) {
+    for (i = 0; i < ARRAY_SIZE(ofmt_aliases); i++) {
         if (!ofmt_aliases[i].shortname)
             continue;
         fprintf(fp, "    %-10s%s\n",
index 2e870e58825732785adff0654f9889ef8e6ca7b7..7bfe2144c74e22c9bcda005be26a2861230064c0 100644 (file)
--- a/preproc.c
+++ b/preproc.c
@@ -482,7 +482,7 @@ static char *check_tasm_directive(char *line)
 
     /* Binary search for the directive name */
     i = -1;
-    j = elements(tasm_directives);
+    j = ARRAY_SIZE(tasm_directives);
     q = nasm_skip_word(p);
     len = q - p;
     if (len) {
@@ -2005,7 +2005,7 @@ static int parse_size(const char *str) {
     static const int sizes[] =
         { 0, 1, 4, 16, 8, 10, 2, 32 };
 
-    return sizes[bsii(str, size_names, elements(size_names))+1];
+    return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
 }
 
 /*
@@ -3469,7 +3469,7 @@ static int find_cc(Token * t)
         return -1;
 
     i = -1;
-    j = elements(conditions);
+    j = ARRAY_SIZE(conditions);
     while (j - i > 1) {
         k = (j + i) / 2;
         m = nasm_stricmp(t->text, conditions[k]);