[PATCH v2 4/4] libgst: Define and use COUNT_OF

Lee Duhem <[email protected]>
Newsgroups gmane.comp.lang.smalltalk.gnu.general
Message-ID <[email protected]>
2017-04-26  Lee Duhem  <[email protected]>

       * gstpriv.h (COUNT_OF): New macro.
       * dict.c (_gst_init_dictionary, _gst_init_dictionary_on_image_load):
       Use COUNT_OF.
       * sym.c (_gst_init_symbols_pass1): Ditto.
---
 libgst/ChangeLog | 7 +++++++
 libgst/dict.c    | 8 +++-----
 libgst/gstpriv.h | 5 +++++
 libgst/sym.c     | 6 +++---
 4 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/libgst/ChangeLog b/libgst/ChangeLog
index 4d1375cc..5298b126 100644
--- a/libgst/ChangeLog
+++ b/libgst/ChangeLog
@@ -1,5 +1,12 @@
 2017-04-26  Lee Duhem  <[email protected]>
 
+	* gstpriv.h (COUNT_OF): New macro.
+	* dict.c (_gst_init_dictionary, _gst_init_dictionary_on_image_load):
+	Use COUNT_OF.
+	* sym.c (_gst_init_symbols_pass1): Ditto.
+
+2017-04-26  Lee Duhem  <[email protected]>
+
 	* callin.c (_gst_type_name_to_oop): Replace sprintf with snprintf.
 	(_gst_class_name_to_oop): Clean properly before return.
 	* cint.c (_gst_invoke_croutine): Clean properly before return.
diff --git a/libgst/dict.c b/libgst/dict.c
index 6e11fc68..30845cef 100644
--- a/libgst/dict.c
+++ b/libgst/dict.c
@@ -828,13 +828,13 @@ _gst_init_dictionary (void)
 
   _gst_init_symbols_pass1 ();
 
-  create_classes_pass1 (class_info, sizeof (class_info) / sizeof (class_info[0]));
+  create_classes_pass1 (class_info, COUNT_OF (class_info));
 
   init_proto_oops();
   _gst_init_symbols_pass2 ();
   init_smalltalk_dictionary ();
 
-  create_classes_pass2 (class_info, sizeof (class_info) / sizeof (class_info[0]));
+  create_classes_pass2 (class_info, COUNT_OF (class_info));
 
   init_runtime_objects ();
   _gst_tenure_all_survivors ();
@@ -1308,9 +1308,7 @@ _gst_init_dictionary_on_image_load (mst_Boolean prim_table_matches)
 
   _gst_restore_symbols ();
 
-  for (ci = class_info; 
-       ci < class_info + sizeof(class_info) / sizeof(class_definition);
-       ci++)
+  for (ci = class_info; ci < class_info + COUNT_OF (class_info); ci++)
     if (ci->reloadAddress)
       {
 	*ci->classVar = dictionary_at (_gst_smalltalk_dictionary,
diff --git a/libgst/gstpriv.h b/libgst/gstpriv.h
index 85aa9a05..ed68ef22 100644
--- a/libgst/gstpriv.h
+++ b/libgst/gstpriv.h
@@ -553,6 +553,11 @@ extern OOP _gst_nil_oop
 #define MIN(x, y) 		( ((x) > (y)) ? (y) : (x) )
 #endif
 
+#ifndef COUNT_OF
+#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
+		((size_t)(!(sizeof(x) % sizeof(0[x])))))
+#endif
+
 #include "ansidecl.h"
 #include "mathl.h"
 #include "socketx.h"
diff --git a/libgst/sym.c b/libgst/sym.c
index 57f62e74..cbd0d325 100644
--- a/libgst/sym.c
+++ b/libgst/sym.c
@@ -1568,7 +1568,7 @@ _gst_init_symbols_pass1 (void)
      to the hash table entries.  */
   for (bs = _gst_builtin_selectors_hash;
        bs - _gst_builtin_selectors_hash <
-	 sizeof (_gst_builtin_selectors_hash) / sizeof (_gst_builtin_selectors_hash[0]);
+	 COUNT_OF (_gst_builtin_selectors_hash);
        bs++)
     if (bs->offset != -1)
       {
@@ -1594,7 +1594,7 @@ _gst_init_symbols_pass2 (void)
      to the hash table entries.  */
   for (bs = _gst_builtin_selectors_hash;
        bs - _gst_builtin_selectors_hash <
-	 sizeof (_gst_builtin_selectors_hash) / sizeof (_gst_builtin_selectors_hash[0]);
+	 COUNT_OF (_gst_builtin_selectors_hash);
        bs++)
     if (bs->offset != -1)
       {
@@ -1633,7 +1633,7 @@ _gst_restore_symbols (void)
      to the hash table entries.  */
   for (bs = _gst_builtin_selectors_hash;
        bs - _gst_builtin_selectors_hash <
-	 sizeof (_gst_builtin_selectors_hash) / sizeof (_gst_builtin_selectors_hash[0]);
+	 COUNT_OF (_gst_builtin_selectors_hash);
        bs++)
     if (bs->offset != -1)
       {
-- 
2.13.5
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.