Changeset: 9b3dc1ec8f1a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/9b3dc1ec8f1a
Modified Files:
        clients/mapiclient/dump.c
        clients/mapiclient/mhelp.c
        clients/mapilib/connect.c
        clients/mapilib/mapi.c
        clients/mapilib/mapi_intern.h
        clients/mapilib/msettings.c
        clients/odbc/driver/ODBCAttrs.c
        clients/odbc/driver/SQLGetFunctions.c
        clients/odbc/driver/SQLGetTypeInfo.c
        cmake/monetdb-defines.cmake
        common/utils/mwidth.c
        ctest/tools/monetdbe/test_helper.h
        gdk/gdk_atoms.c
        gdk/gdk_calc_convert.c
        gdk/gdk_logger.c
        monetdb5/modules/atoms/str.c
        monetdb_config.h.in
        sql/backends/monet5/sql_bincopyconvert.c
        sql/backends/monet5/sql_fround_impl.h
        sql/backends/monet5/sql_round_impl.h
        sql/backends/monet5/sql_time.c
        sql/server/rel_sequence.c
        sql/server/rel_statistics_functions.c
Branch: default
Log Message:

Use modern C features: countof operator.
Available in some implementations where stdcountof.h is available.  For
other systems we provide an alternative implementation.


diffs (truncated from 458 to 300 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -213,7 +213,7 @@ static const char *actions[] = {
        "SET NULL",
        "SET DEFAULT",
 };
-#define NR_ACTIONS     ((int) (sizeof(actions) / sizeof(actions[0])))
+#define NR_ACTIONS     ((int) countof(actions))
 
 static char *
 get_schema(Mapi mid)
@@ -858,7 +858,7 @@ dump_type(Mapi mid, stream *sqlf, const 
                int sub = atoi(c_type_digits);
 
                if (sub > 0 && (sub & 3) == 0 &&
-                   (sub >> 2) < (int) (sizeof(geomsubtypes) / 
sizeof(geomsubtypes[0])))
+                   (sub >> 2) < (int) countof(geomsubtypes))
                        geom = geomsubtypes[sub >> 2];
                if (geom) {
                        mnstr_printf(sqlf, "GEOMETRY(%s", geom);
diff --git a/clients/mapiclient/mhelp.c b/clients/mapiclient/mhelp.c
--- a/clients/mapiclient/mhelp.c
+++ b/clients/mapiclient/mhelp.c
@@ -1235,7 +1235,7 @@ sql_help(const char *pattern, stream *to
        for (i = 0; i < step; i++) {
                for (int j = 0; j < ncolumns; j++) {
                        size_t nextNum = i + j * step;
-                       if(nextNum < sizeof(sqlhelp1)/sizeof(sqlhelp1[0]) - 1) {
+                       if(nextNum < countof(sqlhelp1) - 1) {
                                sql_word(sqlhelp1[nextNum].command, j < 
ncolumns - 1 ? maxlen : 0, toConsole);
                        }
                }
diff --git a/clients/mapilib/connect.c b/clients/mapilib/connect.c
--- a/clients/mapilib/connect.c
+++ b/clients/mapilib/connect.c
@@ -780,7 +780,7 @@ mapi_handshake(Mapi mid)
                                        break;
                                case '^':
                                        r = mid->redirects;
-                                       m = NELEM(mid->redirects) - 1;
+                                       m = countof(mid->redirects) - 1;
                                        while (*r != NULL && m > 0) {
                                                m--;
                                                r++;
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -991,7 +991,7 @@ wsaerror(int err)
 {
        int i;
 
-       for (i = 0; i < NELEM(wsaerrlist); i++)
+       for (i = 0; i < countof(wsaerrlist); i++)
                if (wsaerrlist[i].e == err)
                        return wsaerrlist[i].m;
        return "Unknown error";
diff --git a/clients/mapilib/mapi_intern.h b/clients/mapilib/mapi_intern.h
--- a/clients/mapilib/mapi_intern.h
+++ b/clients/mapilib/mapi_intern.h
@@ -87,11 +87,6 @@ gmtime_r(const time_t *restrict timep, s
 #endif
 
 
-/* number of elements in an array */
-#define NELEM(arr)     (sizeof(arr) / sizeof(arr[0]))
-
-
-
 /* three structures used for communicating date/time information */
 /* these structs are deliberately compatible with the ODBC versions
    SQL_DATE_STRUCT, SQL_TIME_STRUCT, and SQL_TIMESTAMP_STRUCT */
diff --git a/clients/mapilib/msettings.c b/clients/mapilib/msettings.c
--- a/clients/mapilib/msettings.c
+++ b/clients/mapilib/msettings.c
@@ -46,7 +46,7 @@ int msetting_parse_bool(const char *text
                { "on", true },
                { "off", false },
        };
-       for (size_t i = 0; i < sizeof(variants) / sizeof(variants[0]); i++)
+       for (size_t i = 0; i < countof(variants); i++)
                if (strcasecmp(text, variants[i].word) == 0)
                        return variants[i].value;
        return -1;
@@ -92,7 +92,7 @@ by_name[] = {
 mparm
 mparm_parse(const char *name)
 {
-       int n = sizeof(by_name) / sizeof(by_name[0]);
+       int n = countof(by_name);
        // could use a binary search but this is not going to be a bottleneck
        for (int i = 0; i < n; i++)
                if (strcmp(by_name[i].name, name) == 0)
@@ -104,7 +104,7 @@ mparm_parse(const char *name)
 mparm
 mparm_enumerate(int i)
 {
-       int n = sizeof(by_name) / sizeof(by_name[0]);
+       int n = countof(by_name);
        if (i < 0 || i >= n)
                return MP_UNKNOWN;
        return by_name[i].parm;
diff --git a/clients/odbc/driver/ODBCAttrs.c b/clients/odbc/driver/ODBCAttrs.c
--- a/clients/odbc/driver/ODBCAttrs.c
+++ b/clients/odbc/driver/ODBCAttrs.c
@@ -51,7 +51,7 @@ const struct attr_setting attr_settings[
        { "MAPTOLONGVARCHAR", NULL, MP_MAPTOLONGVARCHAR },
 };
 
-const int attr_setting_count = sizeof(attr_settings) / 
sizeof(attr_settings[0]);
+const int attr_setting_count = countof(attr_settings);
 
 int
 attr_setting_lookup(const char *attr_name, bool allow_alt_name)
@@ -65,4 +65,3 @@ attr_setting_lookup(const char *attr_nam
        }
        return -1;
 }
-
diff --git a/clients/odbc/driver/SQLGetFunctions.c 
b/clients/odbc/driver/SQLGetFunctions.c
--- a/clients/odbc/driver/SQLGetFunctions.c
+++ b/clients/odbc/driver/SQLGetFunctions.c
@@ -161,7 +161,7 @@ static UWORD FuncImplemented[] = {
 #endif /* not yet implemented */
 };
 
-#define NFUNCIMPLEMENTED (sizeof(FuncImplemented)/sizeof(FuncImplemented[0]))
+#define NFUNCIMPLEMENTED countof(FuncImplemented)
 
 /* this table is a bit map compatible with
    SQL_API_ODBC3_ALL_FUNCTIONS, to be initialized from the table
diff --git a/clients/odbc/driver/SQLGetTypeInfo.c 
b/clients/odbc/driver/SQLGetTypeInfo.c
--- a/clients/odbc/driver/SQLGetTypeInfo.c
+++ b/clients/odbc/driver/SQLGetTypeInfo.c
@@ -1015,7 +1015,7 @@ ODBCGetTypeInfo(int concise_type,
 {
        const struct types *t;
 
-       for (t = types; t < &types[sizeof(types) / sizeof(types[0])]; t++) {
+       for (t = types; t < &types[countof(types)]; t++) {
                if (t->data_type == concise_type) {
                        if (data_type)
                                *data_type = t->data_type;
@@ -1146,7 +1146,7 @@ MNDBGetTypeInfo(ODBCStmt *stmt,
        i = snprintf(query, sizeof(query), "select * from (values ");
 
        bool first = true;
-       for (t = types; t < &types[sizeof(types) / sizeof(types[0])]; t++) {
+       for (t = types; t < &types[countof(types)]; t++) {
                assert(t == types || t->data_type >= (t-1)->data_type);
                if (DataType != SQL_ALL_TYPES && DataType != t->data_type)
                        continue;
diff --git a/cmake/monetdb-defines.cmake b/cmake/monetdb-defines.cmake
--- a/cmake/monetdb-defines.cmake
+++ b/cmake/monetdb-defines.cmake
@@ -28,6 +28,7 @@ function(monetdb_configure_defines)
   check_include_file("pwd.h" HAVE_PWD_H)
   check_include_file("semaphore.h" HAVE_SEMAPHORE_H)
   check_include_file("stdatomic.h" HAVE_STDATOMIC_H)
+  check_include_file("stdcountof.h" HAVE_STDCOUNTOF_H)
   check_include_file("strings.h" HAVE_STRINGS_H)
   check_include_file("stropts.h" HAVE_STROPTS_H)
   check_include_file("sys/file.h" HAVE_SYS_FILE_H)
diff --git a/common/utils/mwidth.c b/common/utils/mwidth.c
--- a/common/utils/mwidth.c
+++ b/common/utils/mwidth.c
@@ -203,7 +203,7 @@ charwidth(uint32_t c)
                return -1;                              /* control character or 
DELETE */
 
        size_t min = 0;
-       size_t max = (sizeof(intervals) / sizeof(intervals[0])) - 1;
+       size_t max = countof(intervals) - 1;
 
        if (c >= intervals[min].first && c <= intervals[max].last) {
                while (max >= min) {
diff --git a/ctest/tools/monetdbe/test_helper.h 
b/ctest/tools/monetdbe/test_helper.h
--- a/ctest/tools/monetdbe/test_helper.h
+++ b/ctest/tools/monetdbe/test_helper.h
@@ -102,5 +102,5 @@ extern bool check_column_timestamp (mone
 check_column_##TPE ( \
     result, \
     column_index, \
-    sizeof((TEST_TPE_ID(TPE)[]) COLUMN(__VA_ARGS__)) 
/sizeof(((TEST_TPE_ID(TPE)[]) COLUMN(__VA_ARGS__))[0]), \
+    countof((TEST_TPE_ID(TPE)[]) COLUMN(__VA_ARGS__)), \
     (TEST_TPE_ID(TPE)[]) COLUMN(__VA_ARGS__))
diff --git a/gdk/gdk_atoms.c b/gdk/gdk_atoms.c
--- a/gdk/gdk_atoms.c
+++ b/gdk/gdk_atoms.c
@@ -706,7 +706,7 @@ numFromStr(allocator *ma, const char *sr
                        do {
                                /* this calculation cannot overflow */
                                exp = exp * 10 + base10(*p);
-                               if (exp >= (int) (sizeof(maxdiv) / 
sizeof(maxdiv[0]))) {
+                               if (exp >= (int) countof(maxdiv)) {
                                        /* overflow */
                                        goto overflow;
                                }
diff --git a/gdk/gdk_calc_convert.c b/gdk/gdk_calc_convert.c
--- a/gdk/gdk_calc_convert.c
+++ b/gdk/gdk_calc_convert.c
@@ -1386,8 +1386,8 @@ convert_typeswitchloop(const void *src, 
                       oid candoff, bool *reduce,
                       uint8_t scale1, uint8_t scale2, uint8_t precision)
 {
-       assert(stp == TYPE_flt || stp == TYPE_dbl || scale1 < (uint8_t) 
(sizeof(scales) / sizeof(scales[0])));
-       assert(dtp == TYPE_flt || dtp == TYPE_dbl || scale2 < (uint8_t) 
(sizeof(scales) / sizeof(scales[0])));
+       assert(stp == TYPE_flt || stp == TYPE_dbl || scale1 < (uint8_t) 
countof(scales));
+       assert(dtp == TYPE_flt || dtp == TYPE_dbl || scale2 < (uint8_t) 
countof(scales));
        switch (stp) {
        case TYPE_msk:
                switch (dtp) {
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -1564,7 +1564,7 @@ log_read_transaction(logger *lg, BAT *id
 
                TRC_DEBUG_IF(WAL) {
                        if (l.flag > 0 && l.flag != LOG_CLEAR &&
-                           l.flag < (bte) (sizeof(log_commands) / 
sizeof(log_commands[0])))
+                           l.flag < (bte) countof(log_commands))
                                TRC_DEBUG_ENDIF(WAL, "%s %d", 
log_commands[(int) l.flag], l.id);
                        else
                                TRC_DEBUG_ENDIF(WAL, "%d %d", l.flag, l.id);
diff --git a/monetdb5/modules/atoms/str.c b/monetdb5/modules/atoms/str.c
--- a/monetdb5/modules/atoms/str.c
+++ b/monetdb5/modules/atoms/str.c
@@ -908,7 +908,7 @@ const uint32_t whitespace[] = {
        0x2029,                                         /* paragraph separator 
*/
 };
 
-#define NSPACES                (sizeof(whitespace) / sizeof(whitespace[0]))
+#define NSPACES                countof(whitespace)
 
 str
 str_strip(str *buf, size_t *buflen, const char *s)
diff --git a/monetdb_config.h.in b/monetdb_config.h.in
--- a/monetdb_config.h.in
+++ b/monetdb_config.h.in
@@ -82,6 +82,7 @@
 #cmakedefine HAVE_PWD_H 1
 #cmakedefine HAVE_SEMAPHORE_H 1
 #cmakedefine HAVE_STDATOMIC_H 1
+#cmakedefine HAVE_STDCOUNTOF_H 1
 #cmakedefine HAVE_STRINGS_H 1
 #cmakedefine HAVE_STROPTS_H 1
 #cmakedefine HAVE_SYS_FILE_H 1
@@ -105,6 +106,13 @@
 # include <sys/types.h>
 #endif
 
+#ifdef HAVE_STDCOUNTOF_H
+#include <stdcountof.h>
+#else
+/* note, we need parentheses around the operand, unlike the other branch */
+#define countof(X)     (sizeof(X) / sizeof((X)[0]))
+#endif
+
 #ifdef _MSC_VER
 
 /* Windows include files */
diff --git a/sql/backends/monet5/sql_bincopyconvert.c 
b/sql/backends/monet5/sql_bincopyconvert.c
--- a/sql/backends/monet5/sql_bincopyconvert.c
+++ b/sql/backends/monet5/sql_bincopyconvert.c
@@ -335,7 +335,7 @@ void init_insert_state(struct insert_sta
                .schratch_len = 0,
                .resume = 0,
        };
-       for (size_t i = 0; i < 
sizeof(st->singlechar)/sizeof(st->singlechar[0]); i++) {
+       for (size_t i = 0; i < countof(st->singlechar); i++) {
                st->singlechar[i] = BUN_NONE;
        }
 };
diff --git a/sql/backends/monet5/sql_fround_impl.h 
b/sql/backends/monet5/sql_fround_impl.h
--- a/sql/backends/monet5/sql_fround_impl.h
+++ b/sql/backends/monet5/sql_fround_impl.h
@@ -361,7 +361,7 @@ round_wrap(Client ctx, TYPE *res, const 
 
        if (is_bte_nil(rr))
                throw(MAL, "round", SQLSTATE(42000) "Number of digits cannot be 
NULL");
-       if ((size_t) abs(rr) >= sizeof(scales) / sizeof(scales[0]))
+       if ((size_t) abs(rr) >= countof(scales))
                throw(MAL, "round", SQLSTATE(42000) "Digits out of bounds");
        *res = (ISNIL(TYPE)(*v)) ? NIL(TYPE) : round_body(*v, rr);
        if (isinf(*res))
@@ -389,7 +389,7 @@ bat_round_wrap(Client cntxt, MalBlkPtr m
                msg = createException(MAL, "round", SQLSTATE(42000) "Number of 
digits cannot be NULL");
                goto bailout;
        }
-       if ((size_t) abs(r) >= sizeof(scales) / sizeof(scales[0])) {
+       if ((size_t) abs(r) >= countof(scales)) {
                msg = createException(MAL, "round", SQLSTATE(42000) "Digits out 
of bounds");
                goto bailout;
        }
@@ -504,7 +504,7 @@ bat_round_wrap_cst(Client cntxt, MalBlkP
                        if (is_bte_nil(r)) {
                                msg = createException(MAL, "round", 
SQLSTATE(42000) "Number of digits cannot be NULL");
                                goto bailout1;
-                       } else if ((size_t) abs(r) >= sizeof(scales) / 
sizeof(scales[0])) {
+                       } else if ((size_t) abs(r) >= countof(scales)) {
                                msg = createException(MAL, "round", 
SQLSTATE(42000) "Digits out of bounds");
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to