[kernel] Prefer fbl::count_of in C++ code

This also migrates some C++ code to use fbl::atomic, to help settle
some integer comparison differences.

Change-Id: Ief77949d571351a3990fe9a8251d56d35735422c
diff --git a/kernel/arch/x86/perf_mon.cpp b/kernel/arch/x86/perf_mon.cpp
index dbb10df..f4e64c5 100644
--- a/kernel/arch/x86/perf_mon.cpp
+++ b/kernel/arch/x86/perf_mon.cpp
@@ -47,6 +47,7 @@
 #include <assert.h>
 #include <dev/pci_common.h>
 #include <err.h>
+#include <fbl/algorithm.h>
 #include <fbl/alloc_checker.h>
 #include <fbl/macros.h>
 #include <fbl/ref_ptr.h>
@@ -397,7 +398,7 @@
         // See PCI spec 6.2.5.1.
         perfmon_mchbar_bar = bar & ~15u;
         perfmon_num_misc_events =
-            countof(static_cast<zx_x86_ipm_config_t*>(nullptr)->misc_ids);
+            static_cast<uint32_t>(fbl::count_of(static_cast<zx_x86_ipm_config_t*>(nullptr)->misc_ids));
     } else {
         TRACEF("perfmon: error %d reading mchbar\n", status);
     }
@@ -777,9 +778,9 @@
         const zx_x86_ipm_config_t* config,
         unsigned* out_num_used) {
     bool seen_last = false;
-    unsigned max_num_used = countof(config->misc_ids);
-    unsigned num_used = max_num_used;
-    for (unsigned i = 0; i < max_num_used; ++i) {
+    size_t max_num_used = fbl::count_of(config->misc_ids);
+    size_t num_used = max_num_used;
+    for (size_t i = 0; i < max_num_used; ++i) {
         cpuperf_event_id_t id = config->misc_ids[i];
         if (id != 0 && seen_last) {
             TRACEF("Active misc events not front-filled\n");
@@ -792,24 +793,24 @@
         }
         if (seen_last) {
             if (config->misc_flags[i] != 0) {
-                TRACEF("Unused |misc_flags[%u]| not zero\n", i);
+                TRACEF("Unused |misc_flags[%zu]| not zero\n", i);
                 return ZX_ERR_INVALID_ARGS;
             }
         } else {
             if (config->misc_flags[i] & ~IPM_CONFIG_FLAG_MASK) {
-                TRACEF("Unused bits set in |misc_flags[%u]|\n", i);
+                TRACEF("Unused bits set in |misc_flags[%zu]|\n", i);
                 return ZX_ERR_INVALID_ARGS;
             }
             // Currently we only support the MCHBAR events.
             // They cannot provide pc. We ignore the OS/USER bits.
             if (config->misc_flags[i] & IPM_CONFIG_FLAG_PC) {
-                TRACEF("Invalid bits (0x%x) in |misc_flags[%u]|\n",
+                TRACEF("Invalid bits (0x%x) in |misc_flags[%zu]|\n",
                        config->misc_flags[i], i);
                 return ZX_ERR_INVALID_ARGS;
             }
             if ((config->misc_flags[i] & IPM_CONFIG_FLAG_TIMEBASE) &&
                     config->timebase_id == CPUPERF_EVENT_ID_NONE) {
-                TRACEF("Timebase requested for |misc_flags[%u]|, but not provided\n", i);
+                TRACEF("Timebase requested for |misc_flags[%zu]|, but not provided\n", i);
                 return ZX_ERR_INVALID_ARGS;
             }
             switch (CPUPERF_EVENT_ID_EVENT(id)) {
@@ -817,13 +818,13 @@
             case id: break;
 #include <zircon/device/cpu-trace/skylake-misc-events.inc>
             default:
-                TRACEF("Invalid misc event id |misc_ids[%u]|\n", i);
+                TRACEF("Invalid misc event id |misc_ids[%zu]|\n", i);
                 return ZX_ERR_INVALID_ARGS;
             }
         }
     }
 
-    *out_num_used = num_used;
+    *out_num_used = static_cast<unsigned>(num_used);
     return ZX_OK;
 }
 
@@ -904,7 +905,7 @@
     memcpy(state->fixed_flags, config->fixed_flags,
            sizeof(state->fixed_flags));
 
-    for (unsigned i = 0; i < countof(state->fixed_hw_map); ++i) {
+    for (unsigned i = 0; i < fbl::count_of(state->fixed_hw_map); ++i) {
         state->fixed_hw_map[i] = x86_perfmon_lookup_fixed_counter(config->fixed_ids[i]);
     }
 }
diff --git a/kernel/dev/iommu/intel/iommu_impl.cpp b/kernel/dev/iommu/intel/iommu_impl.cpp
index a175937..b713179 100644
--- a/kernel/dev/iommu/intel/iommu_impl.cpp
+++ b/kernel/dev/iommu/intel/iommu_impl.cpp
@@ -147,7 +147,7 @@
             LTRACEF("desc scope %zu has no hops\n", i);
             return ZX_ERR_INVALID_ARGS;
         }
-        if (scopes[i].num_hops > countof(scopes[0].dev_func)) {
+        if (scopes[i].num_hops > fbl::count_of(scopes[0].dev_func)) {
             LTRACEF("desc scope %zu has too many hops\n", i);
             return ZX_ERR_INVALID_ARGS;
         }
@@ -192,7 +192,7 @@
                 LTRACEF("desc reserved memory entry scope %zu has no hops\n", i);
                 return ZX_ERR_INVALID_ARGS;
             }
-            if (scopes[i].num_hops > countof(scopes[0].dev_func)) {
+            if (scopes[i].num_hops > fbl::count_of(scopes[0].dev_func)) {
                 LTRACEF("desc reserved memory entry scope %zu has too many hops\n", i);
                 return ZX_ERR_INVALID_ARGS;
             }
diff --git a/kernel/kernel/mp.cpp b/kernel/kernel/mp.cpp
index 97a94f6..1a5d99c 100644
--- a/kernel/kernel/mp.cpp
+++ b/kernel/kernel/mp.cpp
@@ -12,6 +12,7 @@
 #include <assert.h>
 #include <debug.h>
 #include <err.h>
+#include <fbl/algorithm.h>
 #include <inttypes.h>
 #include <kernel/align.h>
 #include <kernel/dpc.h>
@@ -40,7 +41,7 @@
 void mp_init(void) {
     mutex_init(&mp.hotplug_lock);
     mp.ipi_task_lock = SPIN_LOCK_INITIAL_VALUE;
-    for (uint i = 0; i < countof(mp.ipi_task_list); ++i) {
+    for (uint i = 0; i < fbl::count_of(mp.ipi_task_list); ++i) {
         list_initialize(&mp.ipi_task_list[i]);
     }
 }
diff --git a/kernel/tests/sync_ipi_tests.cpp b/kernel/tests/sync_ipi_tests.cpp
index 3567fac..1437fed 100644
--- a/kernel/tests/sync_ipi_tests.cpp
+++ b/kernel/tests/sync_ipi_tests.cpp
@@ -10,6 +10,7 @@
 #include <arch/ops.h>
 #include <assert.h>
 #include <err.h>
+#include <fbl/algorithm.h>
 #include <kernel/event.h>
 #include <kernel/mp.h>
 #include <kernel/thread.h>
@@ -57,7 +58,7 @@
     event_t gate = EVENT_INITIAL_VALUE(gate, false, 0);
 
     thread_t* threads[5] = {0};
-    for (uint i = 0; i < countof(threads); ++i) {
+    for (uint i = 0; i < fbl::count_of(threads); ++i) {
         threads[i] = thread_create("sync_ipi_deadlock", deadlock_test_thread, &gate, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
         if (!threads[i]) {
             TRACEF("  failed to create thread\n");
@@ -69,7 +70,7 @@
     event_signal(&gate, true);
 
 cleanup:
-    for (uint i = 0; i < countof(threads); ++i) {
+    for (uint i = 0; i < fbl::count_of(threads); ++i) {
         if (threads[i]) {
             thread_join(threads[i], NULL, ZX_TIME_INFINITE);
         }
diff --git a/kernel/tests/thread_tests.cpp b/kernel/tests/thread_tests.cpp
index cde34a2..d3311eb 100644
--- a/kernel/tests/thread_tests.cpp
+++ b/kernel/tests/thread_tests.cpp
@@ -10,6 +10,7 @@
 #include <assert.h>
 #include <debug.h>
 #include <err.h>
+#include <fbl/algorithm.h>
 #include <fbl/mutex.h>
 #include <inttypes.h>
 #include <kernel/event.h>
@@ -90,13 +91,13 @@
 
     thread_t* threads[5];
 
-    for (uint i = 0; i < countof(threads); i++) {
+    for (uint i = 0; i < fbl::count_of(threads); i++) {
         threads[i] = thread_create("mutex tester", &mutex_thread, &m,
                                    get_current_thread()->base_priority, DEFAULT_STACK_SIZE);
         thread_resume(threads[i]);
     }
 
-    for (uint i = 0; i < countof(threads); i++) {
+    for (uint i = 0; i < fbl::count_of(threads); i++) {
         thread_join(threads[i], NULL, ZX_TIME_INFINITE);
     }
 
@@ -234,10 +235,10 @@
     threads[3] = thread_create("event waiter 2", &event_waiter, (void*)2, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
     threads[4] = thread_create("event waiter 3", &event_waiter, (void*)2, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
 
-    for (uint i = 0; i < countof(threads); i++)
+    for (uint i = 0; i < fbl::count_of(threads); i++)
         thread_resume(threads[i]);
 
-    for (uint i = 0; i < countof(threads); i++)
+    for (uint i = 0; i < fbl::count_of(threads); i++)
         thread_join(threads[i], NULL, ZX_TIME_INFINITE);
 
     thread_sleep_relative(ZX_SEC(2));
@@ -253,12 +254,12 @@
     threads[3] = thread_create("event waiter 2", &event_waiter, (void*)99, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
     threads[4] = thread_create("event waiter 3", &event_waiter, (void*)99, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
 
-    for (uint i = 0; i < countof(threads); i++)
+    for (uint i = 0; i < fbl::count_of(threads); i++)
         thread_resume(threads[i]);
 
     thread_sleep_relative(ZX_SEC(2));
 
-    for (uint i = 0; i < countof(threads); i++) {
+    for (uint i = 0; i < fbl::count_of(threads); i++) {
         thread_kill(threads[i]);
         thread_join(threads[i], NULL, ZX_TIME_INFINITE);
     }
@@ -376,11 +377,11 @@
     threads[7] = thread_create("atomic tester 2", &atomic_tester, (void*)-1, LOW_PRIORITY, DEFAULT_STACK_SIZE);
 
     /* start all the threads */
-    for (uint i = 0; i < countof(threads); i++)
+    for (uint i = 0; i < fbl::count_of(threads); i++)
         thread_resume(threads[i]);
 
     /* wait for them to all stop */
-    for (uint i = 0; i < countof(threads); i++) {
+    for (uint i = 0; i < fbl::count_of(threads); i++) {
         thread_join(threads[i], NULL, ZX_TIME_INFINITE);
     }
 
@@ -710,7 +711,7 @@
     printf("top of affinity tester %p\n", t);
 
     while (!state->shutdown) {
-        int which = rand() % countof(state->threads);
+        int which = rand() % static_cast<int>(fbl::count_of(state->threads));
         switch (rand() % 5) {
         case 0: // set affinity
             //printf("%p set aff %p\n", t, state->threads[which]);
diff --git a/kernel/tests/timer_tests.cpp b/kernel/tests/timer_tests.cpp
index ef10493..6a9a8fc 100644
--- a/kernel/tests/timer_tests.cpp
+++ b/kernel/tests/timer_tests.cpp
@@ -16,6 +16,8 @@
 #include <kernel/thread.h>
 #include <kernel/timer.h>
 
+#include <fbl/algorithm.h>
+#include <fbl/atomic.h>
 #include <zircon/types.h>
 
 static void timer_cb(timer_t* timer, zx_time_t now, void* arg) {
@@ -69,25 +71,25 @@
 }
 
 static void timer_cb2(timer_t* timer, zx_time_t now, void* arg) {
-    int* timer_count = (int*)arg;
-    atomic_add(timer_count, 1);
+    auto timer_count = static_cast<fbl::atomic<size_t>*>(arg);
+    timer_count->fetch_add(1);
     thread_preempt_set_pending();
 }
 
 static void timer_test_coalescing(enum slack_mode mode, uint64_t slack,
-                                  const zx_time_t* deadline, const int64_t* expected_adj, int count) {
+                                  const zx_time_t* deadline, const int64_t* expected_adj, size_t count) {
     printf("testing coalsecing mode %d\n", mode);
 
-    int timer_count = 0;
+    fbl::atomic<size_t> timer_count(0);
 
     timer_t* timer = (timer_t*)malloc(sizeof(timer_t) * count);
 
     printf("       orig         new       adjustment\n");
-    for (int ix = 0; ix != count; ++ix) {
+    for (size_t ix = 0; ix != count; ++ix) {
         timer_init(&timer[ix]);
         zx_time_t dl = deadline[ix];
         timer_set(&timer[ix], dl, mode, slack, timer_cb2, &timer_count);
-        printf("[%d] %" PRIu64 "  -> %" PRIu64 ", %" PRIi64 "\n",
+        printf("[%zu] %" PRIu64 "  -> %" PRIu64 ", %" PRIi64 "\n",
                ix, dl, timer[ix].scheduled_time, timer[ix].slack);
 
         if (timer[ix].slack != expected_adj[ix]) {
@@ -96,7 +98,7 @@
     }
 
     // Wait for the timers to fire.
-    while (atomic_load(&timer_count) != count) {
+    while (timer_count.load() != count) {
         thread_sleep(current_time() + ZX_MSEC(5));
     }
 
@@ -119,11 +121,11 @@
         when - (3u * off), // non-coalesced, same as [3], adjustment = 0
     };
 
-    const int64_t expected_adj[countof(deadline)] = {
+    const int64_t expected_adj[fbl::count_of(deadline)] = {
         0, 0, ZX_USEC(10), 0, -(int64_t)ZX_USEC(10), 0, ZX_USEC(10), 0};
 
     timer_test_coalescing(
-        TIMER_SLACK_CENTER, slack, deadline, expected_adj, countof(deadline));
+        TIMER_SLACK_CENTER, slack, deadline, expected_adj, fbl::count_of(deadline));
 }
 
 static void timer_test_coalescing_late(void) {
@@ -141,11 +143,11 @@
         when - (4u * off), // coalesced with [3], adjustment = 10u
     };
 
-    const int64_t expected_adj[countof(deadline)] = {
+    const int64_t expected_adj[fbl::count_of(deadline)] = {
         0, 0, ZX_USEC(20), 0, 0, 0, ZX_USEC(10)};
 
     timer_test_coalescing(
-        TIMER_SLACK_LATE, slack, deadline, expected_adj, countof(deadline));
+        TIMER_SLACK_LATE, slack, deadline, expected_adj, fbl::count_of(deadline));
 }
 
 static void timer_test_coalescing_early(void) {
@@ -163,11 +165,11 @@
         when - (2u * off), // coalesced with [3], adjustment = -10u
     };
 
-    const int64_t expected_adj[countof(deadline)] = {
+    const int64_t expected_adj[fbl::count_of(deadline)] = {
         0, -(int64_t)ZX_USEC(20), 0, 0, 0, -(int64_t)ZX_USEC(10), -(int64_t)ZX_USEC(10)};
 
     timer_test_coalescing(
-        TIMER_SLACK_EARLY, slack, deadline, expected_adj, countof(deadline));
+        TIMER_SLACK_EARLY, slack, deadline, expected_adj, fbl::count_of(deadline));
 }
 
 static void timer_far_deadline(void) {