Enforce channel message limits when building configs

We only support 2^16-1 messages/channel.  Enforce this when building the
configuration, rather than at runtime in the simulation.  This will
make the errors cleaner and more direct.

Change-Id: I368f6c6596a7b0eaa562a70cd385ca1b94e10b93
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/events/shm_event_loop.cc b/aos/events/shm_event_loop.cc
index 9159553..a43de78 100644
--- a/aos/events/shm_event_loop.cc
+++ b/aos/events/shm_event_loop.cc
@@ -104,7 +104,7 @@
 }
 
 ipc_lib::LocklessQueueConfiguration MakeQueueConfiguration(
-    const Channel *channel, std::chrono::seconds channel_storage_duration) {
+    const Configuration *configuration, const Channel *channel) {
   ipc_lib::LocklessQueueConfiguration config;
 
   config.num_watchers = channel->num_watchers();
@@ -112,7 +112,7 @@
   // The value in the channel will default to 0 if readers are configured to
   // copy.
   config.num_pinners = channel->num_readers();
-  config.queue_size = channel_storage_duration.count() * channel->frequency();
+  config.queue_size = configuration::QueueSize(configuration, channel);
   config.message_data_size = channel->max_size();
 
   return config;
@@ -120,9 +120,9 @@
 
 class MMappedQueue {
  public:
-  MMappedQueue(std::string_view shm_base, const Channel *channel,
-               std::chrono::seconds channel_storage_duration)
-      : config_(MakeQueueConfiguration(channel, channel_storage_duration)) {
+  MMappedQueue(std::string_view shm_base, const Configuration *config,
+               const Channel *channel)
+      : config_(MakeQueueConfiguration(config, channel)) {
     std::string path = ShmPath(shm_base, channel);
 
     size_ = ipc_lib::LocklessQueueMemorySize(config_);
@@ -240,10 +240,7 @@
                             const Channel *channel)
       : event_loop_(event_loop),
         channel_(channel),
-        lockless_queue_memory_(
-            shm_base, channel,
-            chrono::ceil<chrono::seconds>(chrono::nanoseconds(
-                event_loop->configuration()->channel_storage_duration()))),
+        lockless_queue_memory_(shm_base, event_loop->configuration(), channel),
         reader_(lockless_queue_memory_.queue()) {
     context_.data = nullptr;
     // Point the queue index at the next index to read starting now.  This
@@ -517,10 +514,7 @@
   explicit ShmSender(std::string_view shm_base, EventLoop *event_loop,
                      const Channel *channel)
       : RawSender(event_loop, channel),
-        lockless_queue_memory_(
-            shm_base, channel,
-            chrono::ceil<chrono::seconds>(chrono::nanoseconds(
-                event_loop->configuration()->channel_storage_duration()))),
+        lockless_queue_memory_(shm_base, event_loop->configuration(), channel),
         lockless_queue_sender_(VerifySender(
             ipc_lib::LocklessQueueSender::Make(
                 lockless_queue_memory_.queue(),
@@ -1219,10 +1213,7 @@
 
 int ShmEventLoop::NumberBuffers(const Channel *channel) {
   CheckCurrentThread();
-  return MakeQueueConfiguration(
-             channel, chrono::ceil<chrono::seconds>(chrono::nanoseconds(
-                          configuration()->channel_storage_duration())))
-      .num_messages();
+  return MakeQueueConfiguration(configuration(), channel).num_messages();
 }
 
 absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
diff --git a/aos/events/simulated_event_loop.cc b/aos/events/simulated_event_loop.cc
index 6bf3ec1..e0ed7a0 100644
--- a/aos/events/simulated_event_loop.cc
+++ b/aos/events/simulated_event_loop.cc
@@ -185,10 +185,8 @@
 
   // The number of messages we pretend to have in the queue.
   int queue_size() const {
-    return channel()->frequency() *
-           std::chrono::duration_cast<std::chrono::duration<double>>(
-               channel_storage_duration_)
-               .count();
+    return configuration::QueueSize(channel()->frequency(),
+                                    channel_storage_duration_);
   }
 
   std::chrono::nanoseconds channel_storage_duration() const {
@@ -197,10 +195,7 @@
 
   // The number of extra buffers (beyond the queue) we pretend to have.
   int number_scratch_buffers() const {
-    // We need to start creating messages before we know how many
-    // senders+readers we'll have, so we need to just pick something which is
-    // always big enough.
-    return 50;
+    return configuration::QueueScratchBufferSize(channel());
   }
 
   int number_buffers() const { return queue_size() + number_scratch_buffers(); }
@@ -256,12 +251,12 @@
   const Channel *channel() const { return channel_; }
 
   void CountSenderCreated() {
-    CheckBufferCount();
     if (sender_count_ >= channel()->num_senders()) {
       LOG(FATAL) << "Failed to create sender on "
                  << configuration::CleanedChannelToString(channel())
                  << ", too many senders.";
     }
+    CheckBufferCount();
     ++sender_count_;
   }