Remove unnecessary SendContext type

Also, add a few more comments.

Change-Id: Iaefb54706c6b218688d0ebfaac3c1484799b3a87
diff --git a/aos/events/event-loop.h b/aos/events/event-loop.h
index d1885bb..a605cf2 100644
--- a/aos/events/event-loop.h
+++ b/aos/events/event-loop.h
@@ -52,7 +52,7 @@
   class Message {
    public:
     Message(RawSender *sender)
-        : msg_(reinterpret_cast<T *>(sender->GetContext()), *sender) {
+        : msg_(reinterpret_cast<T *>(sender->GetMessage()), *sender) {
       msg_->Zero();
     }
 
@@ -67,8 +67,7 @@
     // if the message was successfully sent, and false otherwise.
     bool Send() {
       RawSender *sender = &msg_.get_deleter();
-      return sender->Send(
-          reinterpret_cast<RawSender::SendContext *>(msg_.release()));
+      return sender->Send(msg_.release());
     }
 
    private:
diff --git a/aos/events/raw-event-loop.h b/aos/events/raw-event-loop.h
index fd9ea8f..be605e9 100644
--- a/aos/events/raw-event-loop.h
+++ b/aos/events/raw-event-loop.h
@@ -47,21 +47,19 @@
 // Send() or Free().
 class RawSender {
  public:
-  class SendContext;
-
   RawSender() {}
   virtual ~RawSender() {}
 
-  virtual SendContext *GetContext() = 0;
+  virtual aos::Message *GetMessage() = 0;
 
-  virtual void Free(SendContext *context) = 0;
+  virtual void Free(aos::Message *msg) = 0;
 
-  virtual bool Send(SendContext *context) = 0;
+  virtual bool Send(aos::Message *msg) = 0;
 
   // Call operator that calls Free().
   template <typename T>
   void operator()(T *t) {
-    Free(reinterpret_cast<SendContext *>(t));
+    Free(t);
   }
 
   virtual const char *name() const = 0;
diff --git a/aos/events/shm-event-loop.cc b/aos/events/shm-event-loop.cc
index c9ff6fd..8662bc8 100644
--- a/aos/events/shm-event-loop.cc
+++ b/aos/events/shm-event-loop.cc
@@ -63,20 +63,19 @@
  public:
   explicit ShmSender(RawQueue *queue) : queue_(queue) {}
 
-  SendContext *GetContext() override {
-    return reinterpret_cast<SendContext *>(queue_->GetMessage());
+  aos::Message *GetMessage() override {
+    return reinterpret_cast<aos::Message *>(queue_->GetMessage());
   }
 
-  void Free(SendContext *context) override { queue_->FreeMessage(context); }
+  void Free(aos::Message *msg) override { queue_->FreeMessage(msg); }
 
-  bool Send(SendContext *msg) override {
+  bool Send(aos::Message *msg) override {
     assert(queue_ != NULL);
     {
-      ::aos::Message *aos_msg = reinterpret_cast<Message *>(msg);
       // TODO(austin): This lets multiple senders reorder messages since time
       // isn't acquired with a lock held.
-      if (aos_msg->sent_time == monotonic_clock::min_time) {
-        aos_msg->sent_time = monotonic_clock::now();
+      if (msg->sent_time == monotonic_clock::min_time) {
+        msg->sent_time = monotonic_clock::now();
       }
     }
     return queue_->WriteMessage(msg, RawQueue::kOverride);
diff --git a/aos/events/simulated-event-loop.cc b/aos/events/simulated-event-loop.cc
index 5a9960e..9410b96 100644
--- a/aos/events/simulated-event-loop.cc
+++ b/aos/events/simulated-event-loop.cc
@@ -39,23 +39,19 @@
       : queue_(queue), event_loop_(event_loop) {}
   ~SimulatedSender() {}
 
-  SendContext *GetContext() override {
-    return reinterpret_cast<SendContext *>(
-        RefCountedBuffer(queue_->size()).release());
+  aos::Message *GetMessage() override {
+    return RefCountedBuffer(queue_->size()).release();
   }
 
-  void Free(SendContext *context) override {
-    RefCountedBuffer(reinterpret_cast<aos::Message *>(context));
-  }
+  void Free(aos::Message *msg) override { RefCountedBuffer tmp(msg); }
 
-  bool Send(SendContext *context) override {
+  bool Send(aos::Message *msg) override {
     {
-      ::aos::Message *aos_msg = reinterpret_cast<Message *>(context);
-      if (aos_msg->sent_time == monotonic_clock::min_time) {
-        aos_msg->sent_time = event_loop_->monotonic_now();
+      if (msg->sent_time == monotonic_clock::min_time) {
+        msg->sent_time = event_loop_->monotonic_now();
       }
     }
-    queue_->Send(RefCountedBuffer(reinterpret_cast<aos::Message *>(context)));
+    queue_->Send(RefCountedBuffer(msg));
     return true;  // Maybe false instead? :)
   }
 
diff --git a/aos/events/simulated-event-loop.h b/aos/events/simulated-event-loop.h
index f2981c8..03b6b5a 100644
--- a/aos/events/simulated-event-loop.h
+++ b/aos/events/simulated-event-loop.h
@@ -12,13 +12,30 @@
 
 namespace aos {
 
+// This class manages allocation of queue messages for simulation.
+// Unfortunately, because the current interfaces all assume that we pass around
+// raw pointers to messages we can't use a std::shared_ptr or the such, and
+// because aos::Message's themselves to not have any sort of built-in support
+// for this, we need to manage memory for the Messages in some custom fashion.
+// In this case, we do so by allocating a ref-counter in the bytes immediately
+// preceding the aos::Message. We then provide a constructor that takes just a
+// pointer to an existing message and we assume that it was allocated using this
+// class, and can decrement the counter if the RefCountedBuffer we constructed
+// goes out of scope. There are currently no checks to ensure that pointers
+// passed into this class were actually allocated using this class.
 class RefCountedBuffer {
  public:
   RefCountedBuffer() {}
   ~RefCountedBuffer() { clear(); }
 
+  // Create a RefCountedBuffer for some Message that was already allocated using
+  // a RefCountedBuffer class. This, or some function like it, is required to
+  // allow us to let users of the simulated event loops work with raw pointers
+  // to messages.
   explicit RefCountedBuffer(aos::Message *data) : data_(data) {}
 
+  // Allocates memory for a new message of a given size. Does not initialize the
+  // memory or call any constructors.
   explicit RefCountedBuffer(size_t size) {
     data_ = reinterpret_cast<uint8_t *>(malloc(kRefCountSize + size)) +
             kRefCountSize;