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

Lee Duhem <[email protected]>
Newsgroups gmane.comp.lang.smalltalk.gnu.general
Message-ID <CAOSer0BaQuBOcimG_k1ckxSBoQhfgkCcLWOC5QhOqqrXMJZ0gw@mail.gmail.com>
On Wed, Apr 26, 2017 at 7:19 PM, Lee Duhem <[email protected]> wrote:
> 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 4d1375c..5298b12 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 6e11fc6..30845ce 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 85aa9a0..ed68ef2 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

I'd like to point out that this definition is copied from this
StackOverflow question [1].

Is this OK? If not, do I have to rewrite it somehow?

[1] http://stackoverflow.com/questions/4415524/common-array-length-macro-for-c

> +
>  #include "ansidecl.h"
>  #include "mathl.h"
>  #include "socketx.h"
> diff --git a/libgst/sym.c b/libgst/sym.c
> index 57f62e7..cbd0d32 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.9.3
>
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.