Make universal_notifier_t use unique_ptr instead of raw pointers

This commit is contained in:
ridiculousfish 2017-01-21 16:56:45 -08:00
parent f0065cda13
commit 6f745762bb
3 changed files with 13 additions and 20 deletions

View file

@ -1357,22 +1357,22 @@ universal_notifier_t::notifier_strategy_t universal_notifier_t::resolve_default_
}
universal_notifier_t &universal_notifier_t::default_notifier() {
static universal_notifier_t *result =
static std::unique_ptr<universal_notifier_t> result =
new_notifier_for_strategy(universal_notifier_t::resolve_default_strategy());
return *result;
}
universal_notifier_t *universal_notifier_t::new_notifier_for_strategy(
std::unique_ptr<universal_notifier_t> universal_notifier_t::new_notifier_for_strategy(
universal_notifier_t::notifier_strategy_t strat, const wchar_t *test_path) {
switch (strat) {
case strategy_notifyd: {
return new universal_notifier_notifyd_t();
return make_unique<universal_notifier_notifyd_t>();
}
case strategy_shmem_polling: {
return new universal_notifier_shmem_poller_t();
return make_unique<universal_notifier_shmem_poller_t>();
}
case strategy_named_pipe: {
return new universal_notifier_named_pipe_t(test_path);
return make_unique<universal_notifier_named_pipe_t>(test_path);
}
default: {
debug(0, "Unsupported universal notifier strategy %d\n", strat);

View file

@ -135,8 +135,8 @@ class universal_notifier_t {
static notifier_strategy_t resolve_default_strategy();
virtual ~universal_notifier_t();
// Factory constructor. Free with delete.
static universal_notifier_t *new_notifier_for_strategy(notifier_strategy_t strat,
// Factory constructor.
static std::unique_ptr<universal_notifier_t> new_notifier_for_strategy(notifier_strategy_t strat,
const wchar_t *test_path = NULL);
// Default instance. Other instances are possible for testing.

View file

@ -532,12 +532,12 @@ static int test_iothread_thread_call(int *addr) {
static void test_iothread(void) {
say(L"Testing iothreads");
int *int_ptr = new int(0);
std::unique_ptr<int> int_ptr = make_unique<int>(0);
int iterations = 50000;
int max_achieved_thread_count = 0;
double start = timef();
for (int i = 0; i < iterations; i++) {
int thread_count = iothread_perform(test_iothread_thread_call, int_ptr);
int thread_count = iothread_perform(test_iothread_thread_call, int_ptr.get());
max_achieved_thread_count = std::max(max_achieved_thread_count, thread_count);
}
@ -552,8 +552,6 @@ static void test_iothread(void) {
say(L" (%.02f msec, with max of %d threads)", (end - start) * 1000.0,
max_achieved_thread_count);
delete int_ptr;
}
static parser_test_error_bits_t detect_argument_errors(const wcstring &src) {
@ -2553,7 +2551,7 @@ static void test_universal_callbacks() {
if (system("rm -Rf /tmp/fish_uvars_test")) err(L"rm failed");
}
bool poll_notifier(universal_notifier_t *note) {
bool poll_notifier(const std::unique_ptr<universal_notifier_t> &note) {
bool result = false;
if (note->usec_delay_between_polls() > 0) {
result = note->poll();
@ -2591,7 +2589,7 @@ static void trigger_or_wait_for_notification(universal_notifier_t::notifier_stra
static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy_t strategy) {
say(L"Testing universal notifiers with strategy %d", (int)strategy);
universal_notifier_t *notifiers[16];
std::unique_ptr<universal_notifier_t> notifiers[16];
size_t notifier_count = sizeof notifiers / sizeof *notifiers;
// Populate array of notifiers.
@ -2625,7 +2623,7 @@ static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy
if (!poll_notifier(notifiers[i])) {
err(L"Universal variable notifier (%lu) %p polled failed to notice changes, with "
L"strategy %d",
i, notifiers[i], (int)strategy);
i, notifiers[i].get(), (int)strategy);
}
}
@ -2648,11 +2646,6 @@ static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy
(int)strategy);
}
}
// Clean up.
for (size_t i = 0; i < notifier_count; i++) {
delete notifiers[i];
}
}
static void test_universal_notifiers() {