Browse Source

Introduce ARRAY_SIZE macro

Michał Kowalczyk 4 years ago
parent
commit
99677cdf72

+ 2 - 2
LibOS/shim/src/elf/shim_rtld.c

@@ -1448,7 +1448,7 @@ static int vdso_map_init(void) {
     __load_elf_object(NULL, addr, OBJECT_VDSO, NULL);
     vdso_map->l_name = "vDSO";
 
-    for (size_t i = 0; i < sizeof(vsyms) / sizeof(vsyms[0]); i++) {
+    for (size_t i = 0; i < ARRAY_SIZE(vsyms); i++) {
         ElfW(Sym)* sym = __do_lookup(vsyms[i].name, NULL, vdso_map);
         if (sym == NULL) {
             debug("vDSO: symbol value for %s not found\n", vsyms[i].name);
@@ -1473,7 +1473,7 @@ int vdso_map_migrate(void) {
         return -PAL_ERRNO;
 
     /* adjust funcs to loaded address for newly loaded libsysdb */
-    for (size_t i = 0; i < sizeof(vsyms) / sizeof(vsyms[0]); i++) {
+    for (size_t i = 0; i < ARRAY_SIZE(vsyms); i++) {
         **vsyms[i].func = vsyms[i].value;
     }
 

+ 2 - 2
LibOS/shim/src/fs/proc/info.c

@@ -63,7 +63,7 @@ retry:
     if (!str)
         return -ENOMEM;
 
-    for (size_t i = 0; i < sizeof(meminfo) / sizeof(meminfo[0]); i++) {
+    for (size_t i = 0; i < ARRAY_SIZE(meminfo); i++) {
         int ret = snprintf(str + len, max - len, meminfo[i].fmt, meminfo[i].val);
 
         if (len + ret == max)
@@ -150,7 +150,7 @@ retry:
     for (size_t n = 0; n < pal_control.cpu_info.cpu_num; n++) {
         cpuinfo[0].val = n;
         cpuinfo[6].val = n;
-        for (size_t i = 0; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]); i++) {
+        for (size_t i = 0; i < ARRAY_SIZE(cpuinfo); i++) {
             int ret = snprintf(str + len, max - len, cpuinfo[i].fmt, cpuinfo[i].val);
 
             if (len + ret == max)

+ 1 - 1
LibOS/shim/src/shim_parser.c

@@ -646,7 +646,7 @@ static void parse_clone_flags(va_list ap) {
 #undef FLG
 
     bool printed = false;
-    for (size_t i = 0; i < sizeof(all_flags) / sizeof(all_flags[0]); i++)
+    for (size_t i = 0; i < ARRAY_SIZE(all_flags); i++)
         if (flags & all_flags[i].flag) {
             if (printed)
                 PUTCH('|');

+ 11 - 17
Pal/lib/api.h

@@ -73,6 +73,14 @@ typedef ptrdiff_t ssize_t;
 #define ALIGN_UP_PTR(ptr, size) \
     ((__typeof__(ptr)) ALIGN_DOWN_PTR((uintptr_t)(ptr) + ((size) - 1), (size)))
 
+#define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
+#define IS_STATIC_ARRAY(a) (!SAME_TYPE(a, &*(a)))
+#define FORCE_STATIC_ARRAY(a) sizeof(int[IS_STATIC_ARRAY(a) - 1]) // evaluates to 0
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(a) (FORCE_STATIC_ARRAY(a) + sizeof(a) / sizeof(a[0]))
+#endif
+
 #ifndef container_of
 /**
  * container_of - cast a member of a structure out to the containing structure
@@ -89,10 +97,8 @@ typedef ptrdiff_t ssize_t;
 #define XSTRINGIFY(x) STRINGIFY(x)
 #define STRINGIFY(x) #x
 
-#define static_strlen(str) (sizeof(str) - 1)
-
-/* Turning off unused-parameter warning for keeping function signatures */
 #define __UNUSED(x) do { (void)(x); } while (0)
+#define static_strlen(str) (ARRAY_SIZE(str) - 1)
 
 /* Libc functions */
 
@@ -146,20 +152,8 @@ void *calloc(size_t nmemb, size_t size);
         __typeof__(src)* _s = &(src);                                           \
         __typeof__(dst)* _d = &(dst);                                           \
                                                                                 \
-        __typeof__((*_s)[0]) _s2[sizeof(*_s)/sizeof((*_s)[0])];                 \
-        __typeof__((*_d)[0]) _d2[sizeof(*_d)/sizeof((*_d)[0])];                 \
-                                                                                \
-        /* Causes a compiler warning if the array types mismatch. */            \
-        (void) (_s == _d);                                                      \
-                                                                                \
-        /* Causes a compiler warning if passed arrays are not fixed size        \
-         * arrays.                                                              \
-         */                                                                     \
-        (void) (_s == &_s2);                                                    \
-        (void) (_d == &_d2);                                                    \
-                                                                                \
-        /* Double check sizes. */                                               \
-        static_assert(sizeof(*_s) == sizeof(*_d), "sizes don't match");         \
+        static_assert(SAME_TYPE((*_s)[0], (*_d)[0]), "types must match");       \
+        static_assert(ARRAY_SIZE(*_s) == ARRAY_SIZE(*_d), "sizes must match");  \
                                                                                 \
         memcpy(*_d, *_s, sizeof(*_d));                                          \
     } while (0)

+ 2 - 4
Pal/regression/normalize_path.c

@@ -38,8 +38,6 @@ static const char* get_base_name_cases[][2] = {
     {"a/b/c", "c"},
 };
 
-#define ARR_LEN(x) (sizeof(x) / sizeof((x)[0]))
-
 #define print_err(name, i, ...)                                 \
     do {                                                        \
         pal_printf("%s: case %lu (\"%s\") ", name, i, cases[i][0]); \
@@ -78,7 +76,7 @@ static int run_test(void) {
 
 int main(void) {
     cases        = get_norm_path_cases;
-    cases_len    = ARR_LEN(get_norm_path_cases);
+    cases_len    = ARRAY_SIZE(get_norm_path_cases);
     func_to_test = get_norm_path;
     func_name = "get_norm_path";
     if (run_test()) {
@@ -86,7 +84,7 @@ int main(void) {
     }
 
     cases        = get_base_name_cases;
-    cases_len    = ARR_LEN(get_base_name_cases);
+    cases_len    = ARRAY_SIZE(get_base_name_cases);
     func_to_test = get_base_name;
     func_name = "get_base_name";
     if (run_test()) {

+ 1 - 1
Pal/src/db_exception.c

@@ -63,7 +63,7 @@ PAL_BOL
 DkSetExceptionHandler(PAL_EVENT_HANDLER handler, PAL_NUM event) {
     ENTER_PAL_CALL(DkSetExceptionHandler);
 
-    if (!handler || event == 0 || event >= sizeof(handlers) / sizeof(handlers[0])) {
+    if (!handler || event == 0 || event >= ARRAY_SIZE(handlers)) {
         _DkRaiseFailure(PAL_ERROR_INVAL);
         LEAVE_PAL_CALL_RETURN(PAL_FALSE);
     }

+ 4 - 10
Pal/src/db_rtld.c

@@ -649,12 +649,9 @@ void cache_elf_object (PAL_HANDLE handle, struct link_map * map)
     if (map->l_ld != map->l_real_ld) {
         obj->map.l_ld   = NULL;
         memcpy(obj->dyn, map->l_ld, sizeof(ElfW(Dyn)) * map->l_ldnum);
-        for (int i = 0 ;
-             i < sizeof(obj->map.l_info) / sizeof(obj->map.l_info[0]) ;
-             i++)
+        for (int i = 0; i < ARRAY_SIZE(obj->map.l_info); i++)
             if (obj->map.l_info[i])
-                obj->map.l_info[i] =
-                    (void *) obj->map.l_info[i] - (unsigned long) map->l_ld;
+                obj->map.l_info[i] = (void *)obj->map.l_info[i] - (unsigned long)map->l_ld;
     }
     obj->map.l_name = NULL;
     memcpy(obj->map_name, map->l_name, sizeof(obj->map_name));
@@ -749,12 +746,9 @@ struct link_map * check_cached_elf_object (PAL_HANDLE handle)
 
     if (!obj->map.l_ld) {
         obj->map.l_ld = obj->dyn;
-        for (int i = 0 ;
-             i < sizeof(obj->map.l_info) / sizeof(obj->map.l_info[0]) ;
-             i++)
+        for (int i = 0; i < ARRAY_SIZE(obj->map.l_info); i++)
             if (obj->map.l_info[i])
-                obj->map.l_info[i] =
-                    (void *) obj->map.l_info[i] + (unsigned long) obj->map.l_ld;
+                obj->map.l_info[i] = (void*)obj->map.l_info[i] + (unsigned long)obj->map.l_ld;
     }
 
     struct cached_loadcmd * l = obj->loadcmds;

+ 1 - 1
Pal/src/host/FreeBSD/db_exception2.c

@@ -392,7 +392,7 @@ void signal_setup (void)
         PAL_EVENT_RESUME,
     };
 
-    for (int e = 0 ; e < sizeof(events) / sizeof(events[0]) ; e++)
+    for (int e = 0; e < ARRAY_SIZE(events); e++)
         if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
             goto err;
 

+ 1 - 1
Pal/src/host/Linux-SGX/sgx_exception.c

@@ -80,7 +80,7 @@ static const int async_signals[] =
     SIGCONT,
 };
 
-static const int nasync_signals = sizeof(async_signals) / sizeof(async_signals[0]);
+static const int nasync_signals = ARRAY_SIZE(async_signals);
 
 int set_sighandler (int * sigs, int nsig, void * handler)
 {

+ 2 - 2
Pal/src/host/Linux/db_exception.c

@@ -80,7 +80,7 @@ static const int async_signals[] =
     SIGINT,
     SIGCONT,
 };
-static const int nasync_signals = sizeof(async_signals) / sizeof(async_signals[0]);
+static const int nasync_signals = ARRAY_SIZE(async_signals);
 
 
 int set_sighandler (int * sigs, int nsig, void * handler)
@@ -402,7 +402,7 @@ void signal_setup (void)
         PAL_EVENT_RESUME,
     };
 
-    for (size_t e = 0 ; e < sizeof(events) / sizeof(events[0]) ; e++)
+    for (size_t e = 0; e < ARRAY_SIZE(events); e++)
         if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
             goto err;