source/*: Implement heap based userdata

This makes no immediate change to the external interface, but it will
enable us to do things like making copies of the event sources and
support for floating event source types.

Change-Id: Ida73e773eb0869f6f6f21c1e03c61e9ce2b5625c
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/source/signal.cpp b/test/source/signal.cpp
index d3b53a7..209f2a7 100644
--- a/test/source/signal.cpp
+++ b/test/source/signal.cpp
@@ -21,6 +21,7 @@
 
 using testing::DoAll;
 using testing::Return;
+using testing::ReturnPointee;
 using testing::SaveArg;
 using testing::SetArgPointee;
 
@@ -47,14 +48,8 @@
 
     void expect_destruct()
     {
-        {
-            testing::InSequence sequence;
-            EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source,
-                                                          SD_EVENT_OFF))
-                .WillOnce(Return(0));
-            EXPECT_CALL(mock, sd_event_source_unref(expected_source))
-                .WillOnce(Return(nullptr));
-        }
+        EXPECT_CALL(mock, sd_event_source_unref(expected_source))
+            .WillOnce(Return(nullptr));
         EXPECT_CALL(mock, sd_event_unref(expected_event))
             .WillOnce(Return(nullptr));
     }
@@ -71,9 +66,19 @@
                                           testing::_, nullptr))
         .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<3>(&handler),
                         Return(0)));
+    sd_event_destroy_t destroy;
     void* userdata;
-    EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
-        .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+    {
+        testing::InSequence seq;
+        EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
+                                                               testing::_))
+            .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
+        EXPECT_CALL(mock,
+                    sd_event_source_set_userdata(expected_source, testing::_))
+            .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+        EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
+            .WillRepeatedly(ReturnPointee(&userdata));
+    }
     int completions = 0;
     const struct signalfd_siginfo* return_si;
     Signal::Callback callback = [&](Signal&,
@@ -83,20 +88,21 @@
     };
     Signal signal(*event, sig, std::move(callback));
     EXPECT_FALSE(callback);
-    EXPECT_EQ(&signal, userdata);
+    EXPECT_NE(&signal, userdata);
     EXPECT_EQ(0, completions);
 
     const struct signalfd_siginfo* expected_si =
         reinterpret_cast<struct signalfd_siginfo*>(865);
-    EXPECT_EQ(0, handler(nullptr, expected_si, &signal));
+    EXPECT_EQ(0, handler(nullptr, expected_si, userdata));
     EXPECT_EQ(1, completions);
     EXPECT_EQ(expected_si, return_si);
 
     signal.set_callback(std::bind([]() {}));
-    EXPECT_EQ(0, handler(nullptr, expected_si, &signal));
+    EXPECT_EQ(0, handler(nullptr, expected_si, userdata));
     EXPECT_EQ(1, completions);
 
     expect_destruct();
+    destroy(userdata);
 }
 
 TEST_F(SignalTest, ConstructError)
@@ -120,6 +126,8 @@
 {
   protected:
     std::unique_ptr<Signal> signal;
+    sd_event_destroy_t destroy;
+    void* userdata;
 
     void SetUp()
     {
@@ -130,9 +138,17 @@
         EXPECT_CALL(mock, sd_event_add_signal(expected_event, testing::_, sig,
                                               testing::_, nullptr))
             .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
-        EXPECT_CALL(mock,
-                    sd_event_source_set_userdata(expected_source, testing::_))
-            .WillOnce(Return(nullptr));
+        {
+            testing::InSequence seq;
+            EXPECT_CALL(mock, sd_event_source_set_destroy_callback(
+                                  expected_source, testing::_))
+                .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
+            EXPECT_CALL(
+                mock, sd_event_source_set_userdata(expected_source, testing::_))
+                .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+            EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
+                .WillRepeatedly(ReturnPointee(&userdata));
+        }
         signal = std::make_unique<Signal>(
             *event, sig, [](Signal&, const struct signalfd_siginfo*) {});
     }
@@ -141,6 +157,7 @@
     {
         expect_destruct();
         signal.reset();
+        destroy(userdata);
     }
 };